class Api::Pm::ProjectsController < Api::Pm::BaseController
  before_action :require_login, except: [:convert]
  before_action :load_project, only: [:convert]
  def convert
    data = {
      owner: @project.owner.try(:login),
      identifier: @project.identifier,
      name: @project.name
    }
    render_ok(data: data)
  end

  def issues_count
    return tip_exception '参数错误' unless params[:pm_project_id].present?
    @issues = Issue.where(pm_project_id: params[:pm_project_id])
    case params[:category].to_s
    when 'closed'
      @issues = @issues.closed
    when 'opened'
      @issues = @issues.opened
    end
    @participant_category_count = {}
    if params[:participant_category].to_s == "authoredme" or params[:participant_category].to_s == "assignedme"
      issues_category = @issues.joins(:issue_participants).where(pm_issue_type: [1, 2, 3]).where(issue_participants: {participant_type: %w[authored assigned atme], participant_id: current_user&.id})
      @participant_category_count = issues_category.group(:pm_project_id, "issue_participants.participant_type").count
    end
    case params[:participant_category].to_s
    when 'aboutme' # 关于我的
      @issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: %w[authored assigned atme], participant_id: current_user&.id})
    when 'authoredme' # 我创建的
      @issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: 'authored', participant_id: current_user&.id})
    when 'assignedme' # 我负责的
      @issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: 'assigned', participant_id: current_user&.id})
    when 'atme' # @我的
      @issues = @issues.joins(:issue_participants).where(issue_participants: {participant_type: 'atme', participant_id: current_user&.id})
    end
    data = {}
    @issues_count = @issues.group(:pm_project_id).count
    # requirement 1 task 2 bug 3
    @issues_type_count = @issues.group(:pm_project_id, :pm_issue_type).count
    params[:pm_project_id].map(&:to_i).map do |project_id|
      data[project_id] = {
        total: @issues_count[project_id] || 0,
        requirement: @issues_type_count[[project_id, 1]] || 0,
        task: @issues_type_count[[project_id, 2]] || 0,
        bug: @issues_type_count[[project_id, 3]] || 0,
        authoredme: @participant_category_count[[project_id, 0]] || 0,
        assignedme: @participant_category_count[[project_id, 1]] || 0,
        atme: @participant_category_count[[project_id, 4]] || 0,
      }
    end
    render_ok(data: data)
  end


  def statistics
    return tip_exception '参数错误' if params[:pm_project_id].blank?
    @issues = Issue.where(pm_project_id: params[:pm_project_id], pm_issue_type:[1, 2, 3])
    @last_week_close_issues = @issues.where(status_id: 5).where("updated_on > ? and updated_on < ?", Time.now - 7.days, Time.now)
    last_week_close_type_count_data = @last_week_close_issues.group(:pm_issue_type).count
    type_count_data =  @issues.group(:pm_issue_type).count
    type_status = @issues.group(:pm_issue_type,:status_id).count
    type_status_data = {}
    IssueStatus.all.map do |e|
      # next if e.id == 5
      [1,2,3].map{ |type|
        next if type == 1 && [1, 6].include?(e.id)
        type_status_data[type] = {} if type_status_data[type].nil?
        if type_status[[type,e.id]].nil?
          type_status_data[type][e.id] = 0
        else
          type_status_data[type][e.id] = type_status[[type,e.id]]
        end
      }
    end
    open_data = {
      "1": type_status_data[1][1].to_i + type_status_data[1][2].to_i + type_status_data[1][3].to_i + type_status_data[1][6].to_i,
      "2": type_status_data[2][1].to_i + type_status_data[2][2].to_i + type_status_data[2][3].to_i + type_status_data[2][6].to_i,
      "3": type_status_data[3][1].to_i + type_status_data[3][2].to_i + type_status_data[3][3].to_i + type_status_data[3][6].to_i,
    }
    if type_count_data.keys.size < 3
      nedd_add = [1,2,3] - type_count_data.keys
      nedd_add.map{ |e| 
        type_count_data[e] = 0
      }
    end
    if last_week_close_type_count_data.keys.size < 3
      nedd_add = [1,2,3] - last_week_close_type_count_data.keys
      nedd_add.map{ |e| 
        last_week_close_type_count_data[e] = 0
      }
    end
    data = {
      pie_chart: type_count_data,
      bar_chart: type_status_data,
      open_data: open_data,
      last_week_close_data: last_week_close_type_count_data, 
    }
    render_ok(data: data)
  end

  def polyline
    return tip_exception '参数错误' if params[:pm_project_id].blank?
    time_line = (Time.current.beginning_of_day - 29.day) .. Time.current
    @create_issues = Issue.where(pm_project_id: params[:pm_project_id],created_on: time_line)
    @due_issues = Issue.where(pm_project_id: params[:pm_project_id],status_id:[3,5],due_date: time_line)
    @create_issues_count = @create_issues.group(:pm_issue_type,"DATE(created_on)").count
    @due_issues_count = @due_issues.group(:pm_issue_type,"DATE(due_date)").count
    data = {
      create_issues: {},
      due_issues: {}
    }
    30.times do |time|
      current_time = Date.current - time.day
      if @create_issues_count.present?
        data[:create_issues][current_time] = {
          "1": @create_issues_count[[1,current_time]] || 0,
          "2": @create_issues_count[[2,current_time]] || 0,
          "3": @create_issues_count[[3,current_time]] || 0
        }
      else
        data[:create_issues][current_time] = {
          "1": 0,
          "2": 0,
          "3": 0
        }
      end
      if @due_issues_count.present?
        data[:due_issues][current_time] = {
          "1": @due_issues_count[[1,current_time]] || 0,
          "2": @due_issues_count[[2,current_time]] || 0,
          "3": @due_issues_count[[3,current_time]] || 0
        }
      else
        data[:due_issues][current_time] = {
          "1": 0,
          "2": 0,
          "3": 0
        }
      end
    end
    render_ok(data: data)
  end

  def bind_project
    return  render_forbidden('您没有操作权限！') unless @project.member?(current_user) || current_user.admin?
    Issue.where(pm_project_id: params[:pm_project_id], user_id: current_user).update_all(project_id: params[:project_id])
  end

  private
  def load_project
    @project = Project.joins(:owner).find params[:project_id]
  end


end
