class Api::V1::IssuesController < Api::V1::BaseController 
  before_action :require_login, except: [:index, :show, :show_by_id]
  before_action :require_public_and_member_above, only: [:index, :show, :show_by_id, :create, :update, :destroy] 
  before_action :require_operate_above, only: [:batch_update, :batch_destroy]

  def index 
    IssueTag.init_data(@project.id) unless $redis_cache.hget("project_init_issue_tags", @project.id)
    @object_result = Api::V1::Issues::ListService.call(@project, query_params, current_user)
    @total_issues_count = @object_result[:total_issues_count]
    @opened_issues_count = @object_result[:opened_issues_count]
    @closed_issues_count = @object_result[:closed_issues_count]
    if params[:only_name].present? 
      @issues = kaminary_select_paginate(@object_result[:data].select(:id, :subject, :project_issues_index, :updated_on, :created_on))
    else 
      @issues = kaminari_paginate(@object_result[:data])
    end
  end

  def create 
    @object_result = Api::V1::Issues::CreateService.call(@project, issue_params, current_user)
  end

  before_action :load_issue, only: [:show, :update, :destroy]
  before_action :check_issue_operate_permission, only: [:update, :destroy]
  before_action :load_issue_by_id, only: [:show_by_id]

  def show_by_id 
    @issue.associate_attachment_container
    @user_permission = current_user.present? && current_user.logged? && (@project.member?(current_user) || current_user.admin? || @issue.user == current_user)
  end

  def show
    @issue.associate_attachment_container
    @user_permission = current_user.present? && current_user.logged? && (@project.member?(current_user) || current_user.admin? || @issue.user == current_user)
  end

  def update 
    @object_result = Api::V1::Issues::UpdateService.call(@project, @issue, issue_params, current_user)
  end

  def destroy 
    @object_result = Api::V1::Issues::DeleteService.call(@project, @issue, current_user)
    if @object_result 
      render_ok 
    else 
      render_error("删除疑修失败！")
    end
  end
  
  before_action :load_issues, only: [:batch_update, :batch_destroy]

  def batch_update
    @object_result = Api::V1::Issues::BatchUpdateService.call(@project, @issues, batch_issue_params, current_user)
    if @object_result 
      render_ok 
    else 
      render_error("批量更新疑修失败！")
    end
  end

  def batch_destroy 
    @object_result = Api::V1::Issues::BatchDeleteService.call(@project, @issues, current_user)
    if @object_result 
      render_ok 
    else 
      render_error("批量删除疑修失败！")
    end
  end

  private

  def load_issue 
    @issue = @project.issues.issue_issue.where(project_issues_index: params[:index]).where.not(id: params[:index]).take || @project.issues.issue_issue.find_by_id(params[:index])
    if @issue.blank?
      render_not_found("疑修不存在!")
    end
  end

  def load_issue_by_id 
    @issue = Issue.find_by_id(params[:index])
    if @issue.blank? 
      render_not_found("疑修不存在!")
    end
  end

  def load_issues 
    return render_error("请输入正确的ID数组！") unless params[:ids].is_a?(Array)
    params[:ids].each do |id|
      @issue = Issue.find_by_id(id)
      if @issue.blank?
        return render_not_found("ID为#{id}的疑修不存在!")
      end
    end
    @issues = Issue.where(id: params[:ids])
  end

  def check_issue_operate_permission 
    return render_forbidden("您没有操作权限！") unless @project.member?(current_user) || current_user.admin? || @issue.user == current_user
  end 

  def query_params
    params.permit(
      :only_name,
      :category,
      :participant_category, 
      :keyword, :author_id, 
      :milestone_id, :assigner_id,
      :status_id, 
      :begin_date, :end_date,
      :sort_by, :sort_direction, 
      :issue_tag_ids)
  end

  def issue_params 
    params.permit(
      :status_id, :priority_id, :milestone_id,
      :branch_name, :start_date, :due_date, 
      :subject, :description, :blockchain_token_num,
      :issue_tag_ids => [], 
      :assigner_ids => [], 
      :attachment_ids => [],
      :receivers_login => [])
  end

  def batch_issue_params 
    params.permit(
      :status_id, :priority_id, :milestone_id,
      :issue_tag_ids => [], 
      :assigner_ids => [])
  end
end