class ObRepositorySyncsController < ApplicationController
  before_action :require_login
  before_action :load_project
  before_action :load_ob_repository_sync, except: [:create]
  before_action :authenticate_user!

  def index
    render_ok(data: @ob_repository_sync)
  end


  def create
    tip_exception "参数错误" if params[:github_address].blank? && params[:gitee_address].blank?
    project_name ="#{@project.owner.name}:#{@project.identifier}"
    service = ObRepositorySync::ApiService.new(project_name)
    domain  = GiteaService.gitea_config[:domain]
    project_params = params.merge({ "gitlink_address": "#{domain}/#{@project.owner&.login}/#{@project.identifier}.git" })
    res = service.create_projects(project_params)
    tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200"
    sync_id = res["data"]["id"]
    ob_repository_sync = ObRepositorySync.find_or_initialize_by(project_id: @project.id)
    ob_repository_sync.project_id = @project.id
    ob_repository_sync.user_id = current_user.id
    ob_repository_sync.name = project_name
    ob_repository_sync.github_address = "#{params[:github_address]}"
    ob_repository_sync.gitee_address = "#{params[:gitee_address]}"
    ob_repository_sync.github_token = "#{params[:github_token]}"
    ob_repository_sync.gitee_token = "#{params[:gitee_token]}"
    ob_repository_sync.sync_id = sync_id
    ob_repository_sync.save!
    render_ok
  end

  def delete
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    res = service.delete_project @ob_repository_sync.sync_id
    tip_exception "删除失败: #{res["msg"]}" if res["code"].to_s != "200"
    if res["code"].to_s == "200"
      @ob_repository_sync.destroy!
    end
    render_ok
  end

  def jobs
    tip_exception "该项目未创建同步任务" if @ob_repository_sync.blank?
    page = params[:page] || 1
    limit = params[:limit] || 10
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    source = ""
    if params[:type] && params[:type].to_s.downcase == "github"
      source = "github_branch"
    elsif params[:type] && params[:type].to_s.downcase == "gitee"
      source = "gitee_branch"
    end
    res = service.get_projects_jobs(source, page, limit)
    data = res["data"]["list"]
    render_ok(count: res["data"]["total"], data: data)
  end

  def create_jobs
    tip_exception "必须配置一个分支" if params[:github_branch].blank? && params[:gitee_branch].blank? && params[:gitlink_branch].blank?
    ob_jobs = ObRepositorySyncJob.where(ob_repository_sync_id: @ob_repository_sync.id)
    ob_jobs = ob_jobs.where(job_type: params[:job_type]) if params[:job_type].present?
    ob_jobs = ob_jobs.where(github_branch: params[:github_branch]) if params[:github_branch].present?
    ob_jobs = ob_jobs.where(gitee_branch: params[:gitee_branch]) if params[:gitee_branch].present?
    ob_jobs = ob_jobs.where(gitlink_branch: params[:gitlink_branch]) if params[:gitlink_branch].present?
    tip_exception "该分支组合已配置，不能重复！" if ob_jobs.count > 0
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    res = service.create_projects_jobs(params)
    tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200"
    job_id = res["data"]["id"]
    job = ObRepositorySyncJob.new
    job.ob_repository_sync_id = @ob_repository_sync.id
    job.github_branch = "#{params[:github_branch]}"
    job.gitee_branch = "#{params[:gitee_branch]}"
    job.gitlink_branch = "#{params[:gitlink_branch]}"
    job.job_type = "#{params[:job_type]}"
    job.base = "#{params[:base]}"
    job.job_id = job_id
    job.save
    render_ok
  end


  def delete_job
    tip_exception "缺少参数job_id" if params[:job_id].blank?
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    res = service.delete_job params[:job_id]
    tip_exception "删除失败: #{res["msg"]}" if res["code"].to_s != "200"
    job = ObRepositorySyncJob.find_by(ob_repository_sync_id: @ob_repository_sync.id, job_id: params[:job_id])
    job.destroy! if job.present?
    render_ok
  end

  def start_job
    tip_exception "缺少参数job_id" if params[:job_id].blank?
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    res = service.start_job params[:job_id]
    tip_exception "启动错误: #{res["msg"]}" if res["code"].to_s != "200"
    render_ok
  end

  def stop_job
    tip_exception "缺少参数job_id" if params[:job_id].blank?
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    res = service.stop_job params[:job_id]
    tip_exception "停止错误: #{res["msg"]}" if res["code"].to_s != "200"
    render_ok
  end

  def job_logs
    tip_exception "该项目未创建同步任务" if @ob_repository_sync.blank?
    tip_exception "缺少参数job_id" if params[:job_id].blank?
    service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
    res = service.job_logs params[:job_id]
    tip_exception "请求错误: #{res["msg"]}" if res["code"].to_s != "200"
    render_ok(count: res["data"]["total"], data: res["data"]["list"])
  end

  private

  def load_ob_repository_sync
    @ob_repository_sync = ObRepositorySync.find_by(project_id: @project.id)
  end

  def authenticate_user!
    return if @project.member?(current_user) || current_user.admin?
    render_forbidden('你没有权限操作')
  end
end
