# encoding: utf-8
class Panel::Ask::BackEnd::AskRepliesController < OrbitBackendController

  include AdminHelper
  include OrbitControllerLib::DivisionForDisable

  before_filter :for_app_manager

  def initialize
    super
    @app_title = 'ask_replies'
  end

  def create
    @ask_question = AskQuestion.find(params[:ask_question_id])
    @ask_question.ask_reply = AskReply.new(params[:ask_reply])
    @ask_reply = @ask_question.ask_reply
    @ask_question.save

    if @ask_question.ask_reply.send_email?
      Resque.enqueue(SendAskReplyMail, @ask_reply.ask_question.id)
    end

    status_stack = []
    new_status = ""
    params[:ask_reply][:depts].each do |dns|
      # host = dns.split(":").first # local test

      begin
        status_stack << send_request(dns, @ask_reply.ask_question, @ask_reply) # local test, switch host to dns if on server
      rescue
        status_stack << "fail"
      end
    end if params[:ask_reply][:status] == "轉介其他單位"

    success_count   = status_stack.count("success")
    failed_count    = status_stack.count("fail")

    if failed_count == 0 && success_count > 0
      new_status = "轉介其他單位"
    elsif failed_count > 0 && success_count > 0
      new_status = "部份轉介失敗"
    elsif failed_count > 0 && success_count == 0
      new_status = "轉介失敗"
    elsif failed_count == 0 && success_count == 0 &&  @ask_reply.status == "待處理"
      new_status = "待處理"
    elsif failed_count == 0 && success_count == 0 &&  @ask_reply.status == "已處理"
      new_status = "已處理"
    elsif failed_count == 0 && success_count == 0 &&  @ask_reply.status == "轉介其他單位"
      new_status = "轉介失敗"
    end

    @ask_reply.update_attributes(status: new_status)

    redirect_to panel_ask_back_end_ask_questions_path, notice: t('ask.reply_success')
  end

  def update

    @ask_reply = AskReply.find(params[:id])
    @ask_reply.update_attributes(params[:ask_reply])

    if @ask_reply.send_email?
      Resque.enqueue(SendAskReplyMail, @ask_reply.ask_question.id)
    end

    status_stack = []
    new_status = ""
    params[:ask_reply][:depts].each do |dns|
      # host = dns.split(":").first # local test

      begin
        status_stack << send_request(dns, @ask_reply.ask_question, @ask_reply) # local test, switch host to dns if on server
      rescue
        status_stack << "fail"
      end
    end if params[:ask_reply][:status] == "轉介其他單位"

    success_count   = status_stack.count("success")
    failed_count    = status_stack.count("fail")

    if failed_count == 0 && success_count > 0
      new_status = "轉介其他單位"
    elsif failed_count > 0 && success_count > 0
      new_status = "部份轉介失敗"
    elsif failed_count > 0 && success_count == 0
      new_status = "轉介失敗"
    elsif failed_count == 0 && success_count == 0 &&  @ask_reply.status == "待處理"
      new_status = "待處理"
    elsif failed_count == 0 && success_count == 0 &&  @ask_reply.status == "已處理"
      new_status = "已處理"
    elsif failed_count == 0 && success_count == 0 &&  @ask_reply.status == "轉介其他單位"
      new_status = "轉介失敗"
    end

    @ask_reply.update_attributes(status: new_status)

    redirect_to panel_ask_back_end_ask_questions_path, notice: t('ask.reply_success')
  end

  private

  def send_request(dep_dns, current_question, current_reply)
    require 'net/http'
    require 'uri'
    require 'json'

    uri_path = "/panel/ask/back_end/ask_requests/transferred_request"
    http = Net::HTTP.new(dep_dns, 80) # port 4000 for local test, switch to port 80 for deployment
    req = Net::HTTP::Post.new(uri_path)

    body = {
      'name' => current_question.name,
      'title' => current_question.title,
      'identity' => current_question.identity,
      'email' => current_question.email,
      'phone' => current_question.phone,
      'tax' => current_question.tax,
      'content' => current_question.content
    }

    status = ""
    req.set_form_data(body)
    req.add_field 'X-Auth-Token', '3kjlfksjDFJ'
    response = http.request(req)

    if response.code == "200" 
      status = "success" 
    else
      status = "fail"
    end 

    return status
  end
end
