class Api::V1::Projects::CodecheckService < ApplicationService
  def initialize(project)
    @project = project
    @client = HuaweiCloud::CodeCheck.new
  end

  def create_task(branch)
    language = if @project.project_language.present?
                 data = Gitea::Repository::Languages::ListService.call(@project.owner.login,
                                                                @project.repository.identifier, @project.owner&.gitea_token)
                 project_language = data[:body].keys.map(&:upcase)

                 huawei_language = %w[PYTHON JAVA C++ CSS PHP JAVASCRIPT GO HTML C# TYPESCRIPT SHELL LUA RUST KOTLIN SCALA ARKTS SQL CANGJIE]
                 project_language & huawei_language
               else
                 ["TYPESCRIPT"]
               end
    git_url = Gitea::Repository::GetService.call(@project.owner, @project.repository.identifier)["clone_url"]
    @client.create_task(
      git_url: git_url,
      git_branch: branch,
      languages: language,
      username: @project.owner.login,
      access_token: "this is opensource"
    )
  end

  def run_task(branch,task_id)
    @client.run_task(task_id,git_branch: branch)
  end

  def query_task(task_id)
    code, result  = @client.query_task(task_id)
    if result["task_status"] == 2
      @project.update(huawei_code_check_task_id: task_id)
    end
    [code, result]
  end

  def defects_summary(query)
    @client.defects_summary(@project.huawei_code_check_task_id,query)
  end

  def defects_detail(query)
    @client.defects_detail(@project.huawei_code_check_task_id,query)
  end

  def defects_statistic
    @client.defects_statistic(@project.huawei_code_check_task_id)
  end

  def metrics_summary
    @client.metrics_summary(@project.huawei_code_check_task_id)
  end

  def defect_status(body)
    @client.defect_status(@project.huawei_code_check_task_id,body)
  end

  def issue_status(body) #更新代码问题状态  post
    @client.issue_status(@project.huawei_code_check_task_id,body)
  end

  def issue_list_by_filter(body)  #获取代码问题状态
    @client.issue_list_by_filter(@project.huawei_code_check_task_id,body)
  end

  def issue_filter(body)  #获取代码问题状态
    @client.issue_filter(@project.huawei_code_check_task_id,body)
  end

  def defect_metric_trend(query)
    @client.defect_metric_trend(@project.huawei_code_check_task_id,query)
  end

  def criterion_rule(query)
    @client.criterion_rule(query)
  end

  # =  生成报告
  def generate_pdf
    @client.generate_pdf(@project.huawei_code_check_task_id)
  end

  def query_generate_pdf(async_job_id)
    @client.query_generate_pdf(@project.huawei_code_check_task_id,async_job_id)
  end

  def download_pdf(query)
    @client.download_pdf(@project.huawei_code_check_task_id,query)
  end


  def measure_total(body)
    @client.measure_total(@project.huawei_code_check_task_id,body)
  end

  def related_duplicate_blocks(query)
    @client.related_duplicate_blocks(@project.huawei_code_check_task_id,query)
  end

  def measure_list(query)
    @client.measure_list(@project.huawei_code_check_task_id,query)
  end

end