class ObRepositorySync::ApiService < ApplicationService
  attr_reader :project_name

  def initialize(project_name)
    @project_name = project_name
  end

  def call
    true
  end

  def create_projects(params = {})
    projects_body = {
      "name": "#{@project_name}",
      "github_address": "#{params[:github_address]}",
      "gitee_address": "#{params[:gitee_address]}",
      "github_token": "#{params[:github_token]}",
      "gitee_token": "#{params[:gitee_token]}",
      "gitlink_address": "#{params[:gitlink_address]}",
      "gitlink_token": "#{params[:gitlink_token]}"
    }
    url = URI("#{domain}/cerobot/projects")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Post.new(url)
    request["Content-Type"] = "application/json"
    request.body = JSON.dump(projects_body)

    response = http.request(request)
    # Rails.logger.info "cerobot/projects response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def delete_project sync_id
    url = URI("#{domain}/cerobot/projects?id=#{sync_id}")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Delete.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    # Rails.logger.info "delete project response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def get_projects_jobs(source, pageNum = 1, pageSize = 100)
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs?pageSize=#{pageSize}&pageNum=#{pageNum}&source=#{source}")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Get.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "cerobot/projects response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def create_projects_jobs(params = {})
    job_body = {
      "github_branch": "#{params[:github_branch]}",
      "gitee_branch": "#{params[:gitee_branch]}",
      "gitlink_branch": "#{params[:gitlink_branch]}",
      "type": "#{params[:job_type]}",
      "base": "#{params[:base]}"
    }
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Post.new(url)
    request["Content-Type"] = "application/json"
    request.body = JSON.dump(job_body)

    response = http.request(request)
    Rails.logger.info "cerobot/projects response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def delete_job job_id
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs?id=#{job_id}")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Delete.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "delete job response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def start_job job_id
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/start")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Put.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "start job response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def stop_job job_id
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/stop")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Put.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "stop job response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def set_commit job_id, commit_id
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/set_commit?commit=#{commit_id}")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Put.new(url)
    response = http.request(request)
    Rails.logger.info "set_commit job response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def job_logs job_id
    url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/logs")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Get.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "set_commit job response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    res
  end

  def pull_requests
    url = URI("#{domain}/cerobot/projects/#{@project_name}/pullrequests")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Get.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "pull_requests response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    if res["code"].to_s == "200"
      res["data"]
    else
      []
    end
  end

  def pull_requests_sync
    url = URI("#{domain}/cerobot/projects/#{@project_name}/pullrequests/sync")
    http = Net::HTTP.new(url.host, url.port)
    request = Net::HTTP::Get.new(url)
    request["Content-Type"] = "application/json"
    response = http.request(request)
    Rails.logger.info "pull_requests_sync response.read_body======#{response.read_body}"
    res = JSON.parse(response.body)
    if res["code"].to_s == "200"
      res["data"]
    else
      []
    end
  end

  def domain
    EduSetting.get("ob_repository_sync_api_domain") || "http://106.75.110.152:50087"
  end

end
