class Webhook::IssueClient 

  include Webhook::Client

  attr_accessor :webhook, :issue, :sender, :event, :changes
  attr_accessor :webhook_task

  def initialize(webhook, issue, sender, event, changes={})
    @webhook = webhook 
    @issue = issue.reload
    @sender = sender.reload
    @event = event
    @changes = changes

    # 构建client参数
    super({
      uuid: SecureRandom.uuid, 
      event: @event, 
      webhook: @webhook, 
      payload_content: payload_content
    })
  end

  def payload_content
    case @event
    when "issues"
      issue_payload_content
    when "issue_assign"
      issue_assign_payload_content
    when "issue_label"
      issue_label_payload_content
    end
  end

  def issue_payload_content
    if @changes.blank?
      {
        "action": "opened",
        "number": @issue.project_issues_index,
        "issue": JSON.parse(@issue.to_builder.target!),
        "project": JSON.parse(@issue.project.to_builder.target!),
        "sender": JSON.parse(@sender.to_builder.target!)
      }
    else
      if @changes.has_key?("status_id")
        before_status = IssueStatus.find_by_id(@changes["status_id"][0])
        after_status = IssueStatus.find_by_id(@changes["status_id"][1])
        @changes["status"] = []
        @changes["status"].append(before_status.present? ? JSON.parse(before_status.to_builder.target!) : {})
        @changes["status"].append(after_status.present? ? JSON.parse(after_status.to_builder.target!) : {})
        @changes.delete("status_id")
      end
      if @changes.has_key?("fixed_version_id")
        before_milestone = Version.find_by_id(@changes["fixed_version_id"][0])
        after_milestone = Version.find_by_id(@changes["fixed_version_id"][1])
        @changes["milestone"] = []
        @changes["milestone"].append(before_milestone.present? ? JSON.parse(before_milestone.to_builder.target!) : {})
        @changes["milestone"].append(after_milestone.present? ? JSON.parse(after_milestone.to_builder.target!) : {})
        @changes.delete("fixed_version_id")
      end
      if @changes.has_key?("priority_id")
        before_priority = IssuePriority.find_by_id(@changes["priority_id"][0])
        after_priority = IssuePriority.find_by_id(@changes["priority_id"][1])
        @changes["priority"] = []
        @changes["priority"].append(before_priority.present? ? JSON.parse(before_priority.to_builder.target!) : {})
        @changes["priority"].append(after_priority.present? ? JSON.parse(after_priority.to_builder.target!) : {})
        @changes.delete("priority_id")
      end
      {
        "action": "edited",
        "number": @issue.project_issues_index,
        "changes": @changes,
        "issue": JSON.parse(@issue.to_builder.target!),
        "project": JSON.parse(@issue.project.to_builder.target!),
        "sender": JSON.parse(@sender.to_builder.target!)
      }
    end
  end

  def issue_assign_payload_content
    if @changes.has_key?("assigner_ids")
      before_assigners = User.where(id: @changes["assigner_ids"][0])
      after_assigners = User.where(id: @changes["assigner_ids"][1])
      @changes["assigners"] = []
      @changes["assigners"].append(before_assigners.blank? ? [] : before_assigners.map{|a|JSON.parse(a.to_builder.target!)})
      @changes["assigners"].append(after_assigners.blank? ? [] : after_assigners.map{|a|JSON.parse(a.to_builder.target!)})
      @changes.delete("assigner_ids")
    end
    {
      "action": @changes["assigners"].blank? ? "unassigned" : "assigned",
      "number": @issue.project_issues_index,
      "changes": @changes,
      "issue": JSON.parse(@issue.to_builder.target!),
      "project": JSON.parse(@issue.project.to_builder.target!),
      "sender": JSON.parse(@sender.to_builder.target!)
    }
  end 

  def issue_label_payload_content
    if @changes.has_key?("issue_tag_ids")
      before_issue_tags = IssueTag.where(id: @changes["issue_tag_ids"][0])
      after_issue_tags = IssueTag.where(id: @changes["issue_tag_ids"][1])
      @changes["issue_tags"] = []
      @changes["issue_tags"].append(before_issue_tags.blank? ? [] : before_issue_tags.map{|t|JSON.parse(t.to_builder.target!)})
      @changes["issue_tags"].append(after_issue_tags.blank? ? [] : after_issue_tags.map{|t|JSON.parse(t.to_builder.target!)})
      @changes.delete("issue_tag_ids")
    end
    {
      "action": @changes["issue_tags"].blank? ? "label_cleared" : "label_updated",
      "changes": @changes,
      "number": @issue.project_issues_index,
      "issue": JSON.parse(@issue.to_builder.target!),
      "project": JSON.parse(@issue.project.to_builder.target!),
      "sender": JSON.parse(@sender.to_builder.target!)
    }
  end 
end