class Api::V1::Issues::Journals::CreateService < ApplicationService
  include ActiveModel::Model
  include Api::V1::Issues::Concerns::Checkable 
  include Api::V1::Issues::Concerns::Loadable 

  attr_reader :issue, :current_user, :notes, :parent_id, :reply_id, :attachment_ids, :receivers_login
  attr_accessor :created_journal, :atme_receivers

  validates :notes, :issue, :current_user, presence: true

  def initialize(issue, params, current_user=nil)
    @issue = issue 
    @notes = params[:notes]
    @parent_id = params[:parent_id]
    @reply_id = params[:reply_id]
    @attachment_ids = params[:attachment_ids]
    @receivers_login = params[:receivers_login]
    @current_user = current_user
  end

  def call 
    raise Error, errors.full_messages.join(", ") unless valid?
    raise Error, "请输入回复的评论ID" if parent_id.present? && !reply_id.present?
    ActiveRecord::Base.transaction do 
      check_parent_journal(parent_id) if parent_id.present? 
      check_parent_journal(reply_id) if reply_id.present? 
      check_attachments(attachment_ids) unless attachment_ids.nil?
      check_atme_receivers(receivers_login) unless receivers_login.nil?
      load_attachments(attachment_ids) unless attachment_ids.nil?
      load_atme_receivers(receivers_login) unless receivers_login.nil?

      try_lock("Api::V1::Issues::Journals::CreateProjectService:#{@issue.id}")
      @created_journal = @issue.journals.new(journal_attributes)

      build_comment_participants 
      build_atme_participants if @atme_receivers.present?
      @created_journal.attachments = @attachments unless attachment_ids.blank?
      
      @created_journal.save!
      @issue.save!

      push_activity_2_blockchain("issue_comment_create", @created_journal) if Site.has_blockchain? && @issue.project&.use_blockchain
      ProjectActions::IssueCommentEvent.build(@issue.project, current_user, @issue, @created_journal)
      # @信息发送
      AtmeService.call(current_user, @atme_receivers, @created_journal) unless receivers_login.blank?
      TouchWebhookJob.set(wait: 5.seconds).perform_later('IssueComment', @issue&.id, @current_user.id, @created_journal.id, 'created', {})
      unlock("Api::V1::Issues::Journals::CreateProjectService:#{@issue.id}")

      @created_journal
    end
  end 

  private 

  def journal_attributes 
    journal_attributes = {
      notes: notes,
      user_id: current_user.id
    }

    journal_attributes.merge!({parent_id: parent_id}) if parent_id.present?
    journal_attributes.merge!({reply_id: reply_id}) if reply_id.present?
    
    journal_attributes
  end

  def build_comment_participants
    @issue.issue_participants.new({participant_type: "commented", participant_id: current_user.id}) unless @issue.issue_participants.exists?(participant_type: "commented", participant_id: current_user.id)
  end

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