class Api::Pm::Issues::UpdateService < ApplicationService
  include ActiveModel::Model 
  include Api::V1::Issues::Concerns::Checkable 
  include Api::V1::Issues::Concerns::Loadable 

  attr_reader :project, :issue, :current_user, :operate_by
  attr_reader :status_id, :priority_id, :milestone_id, :branch_name, :start_date, :due_date, :subject, :description, :blockchain_token_num,:status_msg
  attr_reader :target_pm_project_id, :pm_sprint_id, :pm_issue_type, :root_id, :time_scale
  attr_reader :issue_tag_ids, :assigner_ids, :attachment_ids, :receivers_login, :before_issue_tag_ids, :before_assigner_ids, :project_id
  attr_accessor :add_assigner_ids, :previous_issue_changes, :updated_issue, :atme_receivers

  validates :project, :issue, :current_user, presence: true
  validates :blockchain_token_num, numericality: {greater_than: 0}, allow_blank: true

  def initialize(project, issue, params, current_user = nil, operate_by='Issue') 
    @project = project 
    @issue = issue
    @current_user = current_user
    @operate_by = operate_by
    @status_id = params[:status_id]
    @status_msg = params[:status_msg]
    @priority_id = params[:priority_id]
    @milestone_id = params[:milestone_id]
    @branch_name = params[:branch_name]
    @start_date = params[:start_date]
    @due_date = params[:due_date]
    @subject = params[:subject]
    @description = params[:description]
    @blockchain_token_num = params[:blockchain_token_num]
    @issue_tag_ids = params[:issue_tag_ids]
    @assigner_ids = params[:assigner_ids]
    @before_issue_tag_ids = issue.issue_tags.pluck(:id)
    @before_assigner_ids = issue.assigners.pluck(:id)
    @attachment_ids = params[:attachment_ids]
    @receivers_login = params[:receivers_login]
    @target_pm_project_id = params[:target_pm_project_id]
    @pm_sprint_id = params[:pm_sprint_id]
    @pm_issue_type = params[:pm_issue_type]
    @root_id = params[:root_id]
    @time_scale = params[:time_scale]
    @project_id = params[:project_id]
    @add_assigner_ids = []
    @previous_issue_changes = {}
  end

  def call 
    raise Error, errors.full_messages.join(", ") unless valid? 
    ActiveRecord::Base.transaction do 
      check_issue_status(status_id) if status_id.present?
      check_issue_priority(priority_id) if priority_id.present?
      check_milestone(milestone_id) if milestone_id.present?
      check_root_issue(issue, root_id) if root_id.present?
      check_issue_tags(issue_tag_ids) unless issue_tag_ids.nil?
      check_assigners(assigner_ids) unless assigner_ids.nil?
      check_attachments(attachment_ids) unless attachment_ids.nil?
      check_atme_receivers(receivers_login) unless receivers_login.nil?
      check_blockchain_token_num(issue.author_id, project.id, blockchain_token_num, (@issue.blockchain_token_num || 0)) if blockchain_token_num.present? && current_user.id == @issue.author_id && !PullAttachedIssue.exists?(issue_id: @issue, fixed: true)
      load_assigners(assigner_ids)
      load_attachments(attachment_ids)
      load_issue_tags(issue_tag_ids)
      load_atme_receivers(receivers_login) unless receivers_login.nil?

      try_lock("Api::Pm::Issues::UpdateService:#{project.id}:#{issue.id}")
      @updated_issue = @issue
      issue_load_attributes
      build_assigner_issue_journal_details unless assigner_ids.nil?# 操作记录
      build_attachment_issue_journal_details unless attachment_ids.nil?
      build_issue_tag_issue_journal_details unless issue_tag_ids.nil?
      build_assigner_participants unless assigner_ids.nil? # 负责人
      build_edit_participants 
      build_atme_participants if @atme_receivers.present?
      unless assigner_ids.nil?
        @previous_issue_changes.merge!(assigned_to_id: [@updated_issue.assigners.pluck(:id),  @assigners.pluck(:id)])
        @updated_issue.assigners = @assigners || User.none 
      end
      @updated_issue.attachments = @attachments || Attachment.none unless attachment_ids.nil?
      @updated_issue.issue_tags_relates.destroy_all & @updated_issue.issue_tags = @issue_tags || IssueTag.none unless issue_tag_ids.nil?
      @updated_issue.issue_tags_value = @issue_tags.order("id asc").pluck(:id).join(",") unless issue_tag_ids.nil?

      #Pm相关
      @updated_issue.pm_project_id = @target_pm_project_id unless @target_pm_project_id.nil?
      @updated_issue.pm_sprint_id = @pm_sprint_id unless @pm_sprint_id.nil?
      if @updated_issue.children_issues.count == 0 && @updated_issue.parent_id.nil?
        @updated_issue.pm_issue_type = @pm_issue_type unless @pm_issue_type.nil?
      end
      @updated_issue.root_id = @root_id unless @root_id.nil? #不为 nil的时候更新
      @updated_issue.root_id = nil if @root_id.try(:zero?) #为 0 的时候设置为 nil
      @updated_issue.time_scale = @time_scale unless @time_scale.nil?
      if @project_id.present? && @updated_issue.project_id.to_i == 0
        add_project = Project.find_by(id: @project_id)
        @updated_issue.project_issues_index = add_project.get_last_project_issues_index + 1 if add_project.present?
      end
      @updated_issue.project_id = @project_id unless @project_id.nil?
      @updated_issue.updated_on = Time.now
      @updated_issue.changer_id = @current_user.id
      @updated_issue.save!

      build_after_issue_journal_details if @updated_issue.previous_changes.present? # 操作记录
      build_previous_issue_changes
      build_issue_project_trends if status_id.present? # 开关时间记录
      build_cirle_blockchain_token if blockchain_token_num.present?
      unless @project.id.zero?
        # @信息发送
        AtmeService.call(current_user, @atme_receivers, @issue) unless receivers_login.blank?
        # 消息发送
        if Site.has_notice_menu?
          SendTemplateMessageJob.perform_later('IssueChanged', current_user.id, @issue&.id, previous_issue_changes) unless previous_issue_changes.blank?
          SendTemplateMessageJob.perform_later('IssueAssigned', current_user.id, @issue&.id, add_assigner_ids) unless add_assigner_ids.blank?
        end
        # 触发webhook
        Rails.logger.info "################### 触发webhook"
        TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueUpdate', @updated_issue&.id, current_user.id, previous_issue_changes.except(:issue_tags_value, :assigned_to_id))
        TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueLabel', @issue&.id, current_user.id, {issue_tag_ids: [before_issue_tag_ids, issue_tag_ids]}) unless issue_tag_ids.nil?
        TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueAssign', @issue&.id, current_user.id, {assigner_ids: [before_assigner_ids, assigner_ids]}) unless assigner_ids.nil?
      end

      unlock("Api::Pm::Issues::UpdateService:#{project.id}:#{issue.id}")
      return @updated_issue
    end
  end
 
  private

  def issue_load_attributes 
    if current_user.id == @updated_issue.author_id && !PullAttachedIssue.exists?(issue_id: @updated_issue, fixed: true)
      @updated_issue.blockchain_token_num = blockchain_token_num unless blockchain_token_num.nil?
    end
    @updated_issue.status_id = status_id if status_id.present?
    @updated_issue.priority_id = priority_id if priority_id.present?
    @updated_issue.fixed_version_id = milestone_id unless milestone_id.nil? 
    @updated_issue.branch_name = branch_name unless branch_name.nil?
    @updated_issue.start_date = start_date unless start_date.nil? 
    @updated_issue.due_date = due_date unless due_date.nil? 
    @updated_issue.subject = subject if subject.present? 
    @updated_issue.description = description unless description.nil?
  end

  def build_assigner_participants
    if assigner_ids.blank? 
      @updated_issue.issue_participants.where(participant_type: "assigned").each(&:destroy!)
    else
      @updated_issue.issue_participants.where(participant_type: "assigned").where.not(participant_id: assigner_ids).each(&:destroy!)
      assigner_ids.each do |aid|
        next if @updated_issue.issue_participants.exists?(participant_type: "assigned", participant_id: aid)
        @updated_issue.issue_participants.new({participant_type: "assigned", participant_id: aid})
        @add_assigner_ids << aid
      end
    end
  end

  def build_edit_participants 
    @updated_issue.issue_participants.new({participant_type: "edited", participant_id: current_user.id}) unless @updated_issue.issue_participants.exists?(participant_type: "edited", participant_id: current_user.id)
  end

  def build_atme_participants
    @atme_receivers.each do |receiver|
      next if @updated_issue.issue_participants.exists?(participant_type: "atme", participant_id: receiver.id)
      @updated_issue.issue_participants.new({participant_type: "atme", participant_id: receiver.id})
    end
  end

  def build_previous_issue_changes
    @previous_issue_changes.merge!(@updated_issue.previous_changes.slice("status_id", "priority_id", "fixed_version_id", "issue_tags_value", "branch_name", "subject").symbolize_keys)
    if @updated_issue.previous_changes[:start_date].present? 
      @previous_issue_changes.merge!(start_date: [@updated_issue.previous_changes[:start_date][0].to_s,  @updated_issue.previous_changes[:start_date][1].to_s])
    end
    if @updated_issue.previous_changes[:due_date].present? 
      @previous_issue_changes.merge!(due_date: [@updated_issue.previous_changes[:due_date][0].to_s,  @updated_issue.previous_changes[:due_date][1].to_s])
    end
  end

  def build_cirle_blockchain_token
    if @updated_issue.previous_changes["blockchain_token_num"].present? 
      unlock_balance_on_blockchain(@updated_issue&.author_id.to_s, @updated_issue.project_id.to_s, @updated_issue.previous_changes["blockchain_token_num"][0].to_i) if @updated_issue.previous_changes["blockchain_token_num"][0].present?
      lock_balance_on_blockchain(@updated_issue&.author_id.to_s, @updated_issue.project_id.to_s, @updated_issue.previous_changes["blockchain_token_num"][1].to_i) if @updated_issue.previous_changes["blockchain_token_num"][1].present?
    end
  end

  def build_issue_project_trends
    if @updated_issue.previous_changes["status_id"].present? && @updated_issue.previous_changes["status_id"][1] == 5
      @updated_issue.project_trends.create!({user_id: current_user.id, project_id: @project.id, action_type: ProjectTrend::CLOSE})
    end
    if @updated_issue.previous_changes["status_id"].present? && @updated_issue.previous_changes["status_id"][0] == 5
      @updated_issue.project_trends.where(action_type: ProjectTrend::CLOSE).each(&:destroy!)
    end
  end

  def build_after_issue_journal_details
    begin
      # 更改标题
      if @updated_issue.previous_changes["subject"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "subject", old_value: @updated_issue.previous_changes["subject"][0], value: @updated_issue.previous_changes["subject"][1]})
      end

      # 更改描述
      if @updated_issue.previous_changes["description"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "description", old_value: @updated_issue.previous_changes["description"][0], value: @updated_issue.previous_changes["description"][1]})
      end

      # 修改状态
      if @updated_issue.previous_changes["status_id"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: @updated_issue.pm_issue_type_string,
                                         prop_key: "status_id",
                                         status_msg: @status_msg,
                                         old_value: @updated_issue.previous_changes["status_id"][0],
                                         value: @updated_issue.previous_changes["status_id"][1]}
        )
      end

      # 修改优先级
      if @updated_issue.previous_changes["priority_id"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "priority_id", old_value: @updated_issue.previous_changes["priority_id"][0], value: @updated_issue.previous_changes["priority_id"][1]})
      end

      # 修改工作项类型
      if @updated_issue.previous_changes["pm_issue_type"].present? 
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "pm_issue_type", old_value: @updated_issue.previous_changes["pm_issue_type"][0], value: @updated_issue.previous_changes["pm_issue_type"][1]})
      end

      # 修改迭代
      if @updated_issue.previous_changes["pm_sprint_id"].present? 
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "pm_sprint_id", old_value: @updated_issue.previous_changes["pm_sprint_id"][0], value: @updated_issue.previous_changes["pm_sprint_id"][1]})
      end

      # 修改代码库
      if @updated_issue.previous_changes["project_id"].present? 
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "project_id", old_value: @updated_issue.previous_changes["project_id"][0], value: @updated_issue.previous_changes["project_id"][1]})
      end

      # 修改里程碑
      if @updated_issue.previous_changes["fixed_version_id"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "fixed_version_id", old_value: @updated_issue.previous_changes["fixed_version_id"][0], value: @updated_issue.previous_changes["fixed_version_id"][1]})
      end

      # 更改分支
      if @updated_issue.previous_changes["branch_name"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "branch_name", old_value: @updated_issue.previous_changes["branch_name"][0], value: @updated_issue.previous_changes["branch_name"][1]})
      end

      # 更改开始时间
      if @updated_issue.previous_changes["start_date"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "start_date", old_value: @updated_issue.previous_changes["start_date"][0], value: @updated_issue.previous_changes["start_date"][1]})
      end

      # 更改结束时间
      if @updated_issue.previous_changes["due_date"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "due_date", old_value: @updated_issue.previous_changes["due_date"][0], value: @updated_issue.previous_changes["due_date"][1]})
      end

      # 更改预估工时
      if @updated_issue.previous_changes["time_scale"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attr", prop_key: "time_scale", old_value: @updated_issue.previous_changes["time_scale"][0], value: @updated_issue.previous_changes["time_scale"][1]})
      end

      # 更改父工作项
      if @updated_issue.previous_changes["root_id"].present?
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "root_id", old_value: @updated_issue.previous_changes["root_id"][0], value: @updated_issue.previous_changes["root_id"][1]})

        # 更改子工作项
        before_parent_issue = Issue.find_by_id(@updated_issue.previous_changes["root_id"][0])
        if before_parent_issue.present? 
          journal = before_parent_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
          journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "tag_leaf_issue", old_value: @updated_issue.id.to_s})
        end

        after_parent_issue = Issue.find_by_id(@updated_issue.previous_changes["root_id"][1])
        if after_parent_issue.present? 
          journal = after_parent_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
          journal.journal_details.create!({property: @updated_issue.pm_issue_type_string, prop_key: "tag_leaf_issue", value: @updated_issue.id.to_s})
        end
      end
    rescue
      raise Error, "创建操作记录失败!"
    end
  end

  def build_assigner_issue_journal_details
    begin
      # 更改负责人
      new_assigner_ids = @assigner_ids
      new_assigner_ids = [] if @assigner_ids.nil?
      now_assigner_ids = @updated_issue.assigners.pluck(:id)
      if !(now_assigner_ids.sort == new_assigner_ids.sort)
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "assigner", prop_key: "#{new_assigner_ids.size}", old_value: now_assigner_ids.join(","), value: new_assigner_ids.join(",")})
      end

    rescue
      raise Error, "创建操作记录失败!"
    end
  end

  def build_issue_tag_issue_journal_details
    begin
      # 更改标记
      new_issue_tag_ids = @issue_tag_ids
      new_issue_tag_ids = [] if @issue_tag_ids.nil?
      now_issue_tag_ids = @updated_issue.issue_tags.pluck(:id)
      if !(now_issue_tag_ids.sort == new_issue_tag_ids.sort)
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "issue_tag", prop_key: "#{new_issue_tag_ids.size}", old_value: now_issue_tag_ids.join(","), value: new_issue_tag_ids.join(",")})
      end
    rescue
      raise Error, "创建操作记录失败!"
    end
  end


  def build_attachment_issue_journal_details
    begin
      # 更改附件
      new_attachment_ids = @attachment_ids
      new_attachment_ids = [] if @attachment_ids.nil?
      now_attachment_ids = @updated_issue.attachments.pluck(:id)
      if !(now_attachment_ids.sort == new_attachment_ids.sort)
        journal = @updated_issue.journals.create!({user_id: current_user.id, operate_by: @operate_by})
        journal.journal_details.create!({property: "attachment", prop_key: "#{new_attachment_ids.size}", old_value: now_attachment_ids.join(","), value: new_attachment_ids.join(",")})
      end
    rescue
      raise Error, "创建操作记录失败!"
    end
  end
 
end