namespace :batch_add_issues do
  desc "batch_add_issues"
  task gitee: :environment do
    project_id = ENV['project_id']
    puts "project_id=================#{project_id}"
    next if project_id.blank?
    project = Project.find project_id
    count = 0
    if ENV['count'].present?
      count = ENV['count'].to_i
    end

    total_count = 2066 + 499 + 16675 + 451
    puts "total_count==========#{total_count}"
    if total_count > 100
      total_page =  (total_count / 100) + 1
      total_page.times do |i|
        add_issues_to_project(project, i + 1 + count)
      end
    else
      add_issues_to_project(project, 1)
    end


  end

  def add_issues_to_project(project,page)
    # curl -X GET --header 'Content-Type: application/json;charset=UTF-8' 'https://gitee.com/api/v5/repos/mindspore/mindspore/issues?access_token=5ccebd935915fb6cfcae634b161047a2&state=open&sort=created&direction=desc&page=1&per_page=10'
    api_url = "https://gitee.com/api/v5/repos/mindspore/mindspore/issues?access_token=96a637aa055f15056e77e3cf11a67525&state=all&sort=created&direction=desc&page=#{page}&per_page=100"
    uri = URI.parse(api_url)
    response = Net::HTTP.get_response(uri)
    puts "gitee api response.code ===== #{response.code}"
    lists = JSON.parse(response.body)
    puts "lists.size =====#{lists.size}"

    # "1" => "新增",
    # "2" => "正在解决",
    # "3" => "已解决",
    # "5" => "关闭",
    # "6" => "拒绝"
    # Issue的状态: open（开启的）, progressing(进行中), closed（关闭的）, rejected（拒绝的）。 默认: open
    lists.each do |issue|
      created_issue = Issue.find_by(project_id: project.id, subject: issue['title'])
      unless created_issue.present?
        priority = [1, 2, 3, 4].include?(issue['priority'].to_i) ? issue['priority'].to_i : 2
        issue_status = ["", "open", "progressing", "", "", "closed", "rejected"].index(issue['state'])
        issue_status = 1 if issue_status.nil?
        user_login = issue['user']['login']
        user_login = user_login[0..20] if user_login.size > 29
        issue_created_at = issue['created_at']
        issue_updated_at = issue['updated_at']
        user = User.find_by(login: user_login)
        unless user.present?
          username = user_login
          email = "#{username}@gitlink.org.cn"
          phone = ""
          password = "a12345678"
          user = User.new(nickname: user_login, login: username, mail: email, password: password, type: 'User', phone: phone)
          interactor = Gitea::RegisterInteractor.call({ username: username, email: email, password: password })
          gitea_user = interactor.result
          result = Gitea::User::GenerateTokenService.call(username, password)
          user.gitea_token = result['sha1']
          user.gitea_uid = gitea_user[:body]['id']
          user.created_on = issue_created_at
          user.updated_on = issue_created_at
          user.is_test = true
          user.save!
          UserExtension.create!(user_id: user.id)
          puts "import_user batch success: phone #{phone} email: #{email}"
        end
        title = issue['title']
        title = title[0..190] if title.size > 190
        issue_params = {
          :status_id => issue_status,
          :priority_id => priority,
          # :milestone_id,
          # :branch_name,
          # :start_date,
          # :due_date,
          :subject => title,
          :description => issue['body'],
          # :blockchain_token_num,
          :issue_tag_ids => [],
          :assigner_ids => [],
          :attachment_ids => [],
          :receivers_login => []
        }
        created_issue = Api::V1::Issues::CreateService.call(project, issue_params, user)
        created_issue.update_columns(created_on: issue_created_at, updated_on: issue_updated_at)
      end

      issue_number = issue['number']
      comment_api_url = "https://gitee.com/api/v5/repos/mindspore/mindspore/issues/#{issue_number}/comments?access_token=96a637aa055f15056e77e3cf11a67525&page=1&per_page=100&order=asc"
      comment_uri = URI.parse(comment_api_url)
      comment_response = Net::HTTP.get_response(comment_uri)
      comment_lists = JSON.parse(comment_response.body)

      comment_lists.each do |comment|
        next if Journal.find_by(journalized_id: created_issue.id, journalized_type: 'Issue', notes: comment['body']).present?
        user_login = comment['user']['login']
        next if user_login.size >29
        comment_created_at = comment['created_at']
        comment_updated_at = comment['updated_at']
        comment_user = User.find_by(login: user_login)
        unless comment_user.present?
          username = user_login
          email = "#{username}@gitlink.org.cn"
          phone = ""
          password = "a12345678"
          comment_user = User.new(nickname: user_login, login: username, mail: email, password: password, type: 'User', phone: phone)
          interactor = Gitea::RegisterInteractor.call({ username: username, email: email, password: password })
          gitea_user = interactor.result
          result = Gitea::User::GenerateTokenService.call(username, password)
          comment_user.gitea_token = result['sha1']
          comment_user.gitea_uid = gitea_user[:body]['id']
          comment_user.created_on = comment_created_at
          comment_user.updated_on = comment_created_at
          comment_user.save!
          UserExtension.create!(user_id: comment_user.id)
        end

        journal_params = {:notes => comment['body'],
                          :attachment_ids => [],
                          :receivers_login => []}

        object_result = Api::V1::Issues::Journals::CreateService.call(created_issue, journal_params, comment_user)
        object_result.update_columns(created_on: comment_created_at, updated_on: comment_updated_at)
      end
    end
  end


  task github: :environment do
    project_id = ENV['project_id']
    puts "project_id=================#{project_id}"
    next if project_id.blank?
    project = Project.find project_id
    count = 0
    if ENV['count'].present?
      count = ENV['count'].to_i
    end

    total_count = 3000
    puts "total_count==========#{total_count}"
    if total_count > 100
      total_page =  (total_count / 100) + 1
      total_page.times do |i|
        sleep 1.seconds
        add_github_issues_to_project(project, i + 1 + count)
      end
    else
      add_github_issues_to_project(project, 1)
    end
  end

  def add_github_issues_to_project(project,page)
    # curl -X GET --header 'Content-Type: application/json;charset=UTF-8' 'https://gitee.com/api/v5/repos/mindspore/mindspore/issues?access_token=5ccebd935915fb6cfcae634b161047a2&state=open&sort=created&direction=desc&page=1&per_page=10'
    # api_url = "https://gitee.com/api/v5/repos/mindspore/mindspore/issues?access_token=96a637aa055f15056e77e3cf11a67525&state=all&sort=created&direction=desc&page=#{page}&per_page=100"
    api_url = "https://api.github.com/repos/OpenXiangShan/XiangShan/issues?sort=created&direction=desc&per_page=100&state=all&page=#{page}"
    uri = URI.parse(api_url)
    # response = Net::HTTP.get_response(uri)
    http = Net::HTTP.new(uri.hostname, uri.port)
    http.use_ssl = true
    response = http.send_request('GET', uri, nil, {'Content-Type' => 'application/json','authorization' => "Bearer #{ENV['token']}"})

    puts "gitee api response.code ===== #{response.code}"
    lists = JSON.parse(response.body)
    puts "lists.size =====#{lists.size}"

    # "1" => "新增",
    # "2" => "正在解决",
    # "3" => "已解决",
    # "5" => "关闭",
    # "6" => "拒绝"
    # Issue的状态: open（开启的）, progressing(进行中), closed（关闭的）, rejected（拒绝的）。 默认: open
    lists.each do |issue|
      next if issue['pull_request'].present?
      title = issue['title']
      title = title[0..190] if title.size > 190
      created_issue = Issue.find_by(project_id: project.id, subject: title)
      issue_created_at = issue['created_at']
      issue_updated_at = issue['updated_at']
      unless created_issue.present?
        priority = [1, 2, 3, 4].include?(issue['priority'].to_i) ? issue['priority'].to_i : 2
        issue_status = ["", "open", "progressing", "", "", "closed", "rejected"].index(issue['state'])
        issue_status = 1 if issue_status.nil?
        user_login = issue['user']['login']
        user_login = user_login[0..20] if user_login.size > 29
        username = "gh-#{user_login}"
        user = User.find_by(login: username)
        unless user.present?
          email = "#{username}@gitlink.org.cn"
          phone = ""
          password = "a12345678"
          user = User.new(nickname: username, login: username, mail: email, password: password, type: 'User', phone: phone)
          interactor = Gitea::RegisterInteractor.call({ username: username, email: email, password: password })
          gitea_user = interactor.result
          result = Gitea::User::GenerateTokenService.call(username, password)
          user.gitea_token = result['sha1']
          user.gitea_uid = gitea_user[:body]['id']
          user.created_on = issue_created_at
          user.updated_on = issue_created_at
          user.is_test = true
          user.save!
          UserExtension.create!(user_id: user.id)
          puts "import_user batch success: phone #{phone} email: #{email}"
        end
        issue_tags_value = []
        if issue['labels'].present?
          issue['labels'].each do |tag|
            label = project.issue_tags.find_or_create_by!(name: tag['name'], description: tag['description'], color: "##{tag['color']}")
            issue_tags_value.push(label.id)
          end
        end

        issue_params = {
          :status_id => issue_status,
          :priority_id => priority,
          # :milestone_id,
          # :branch_name,
          # :start_date,
          # :due_date,
          :subject => title,
          :description => issue['body'],
          # :blockchain_token_num,
          :issue_tag_ids => issue_tags_value,
          :assigner_ids => [],
          :attachment_ids => [],
          :receivers_login => []
        }
        created_issue = Api::V1::Issues::CreateService.call(project, issue_params, user)
      end

      issue_number = issue['number']
      sleep 1.seconds
      # comment_api_url = "https://gitee.com/api/v5/repos/mindspore/mindspore/issues/#{issue_number}/comments?access_token=96a637aa055f15056e77e3cf11a67525&page=1&per_page=100&order=asc"
      comment_api_url = "https://api.github.com/repos/OpenXiangShan/XiangShan/issues/#{issue_number}/comments?page=1&per_page=100"
      comment_uri = URI.parse(comment_api_url)
      # comment_response = Net::HTTP.get_response(comment_uri)
      http = Net::HTTP.new(comment_uri.hostname, comment_uri.port)
      http.use_ssl = true
      comment_response = http.send_request('GET', comment_uri, nil, {'Content-Type' => 'application/json','authorization' => "Bearer #{ENV['token']}"})
      comment_lists = JSON.parse(comment_response.body)

      comment_lists.each do |comment|
        next if Journal.find_by(journalized_id: created_issue.id, journalized_type: 'Issue', notes: comment['body']).present?
        user_login = comment['user']['login']
        next if user_login.size >29
        comment_created_at = comment['created_at']
        comment_updated_at = comment['updated_at']
        username = "gh-#{user_login}"
        comment_user = User.find_by(login: username)
        unless comment_user.present?
          email = "#{username}@gitlink.org.cn"
          phone = ""
          password = "a12345678"
          comment_user = User.new(nickname: username, login: username, mail: email, password: password, type: 'User', phone: phone)
          interactor = Gitea::RegisterInteractor.call({ username: username, email: email, password: password })
          gitea_user = interactor.result
          result = Gitea::User::GenerateTokenService.call(username, password)
          comment_user.gitea_token = result['sha1']
          comment_user.gitea_uid = gitea_user[:body]['id']
          comment_user.created_on = comment_created_at
          comment_user.updated_on = comment_created_at
          comment_user.save!
          UserExtension.create!(user_id: comment_user.id)
        end

        journal_params = {:notes => comment['body'],
                          :attachment_ids => [],
                          :receivers_login => []}

        object_result = Api::V1::Issues::Journals::CreateService.call(created_issue, journal_params, comment_user)
        object_result.update_columns(created_on: comment_created_at, updated_on: comment_updated_at)
        created_issue.update_columns(created_on: issue_created_at, updated_on: issue_updated_at)
      end
    end
  end
end
