class SettingsController < ApplicationController
  def show
    get_navbar
    #site_page_deploy_domain
    get_add_menu
    get_common_menu
    #get_sub_competitions
    get_other_setting
    get_personal_menu
    get_third_party
    # get_third_party_new
    get_professional_fields
    get_top_system_notification
    get_user_apply_signature
  end

  def check_url
    url = params[:url]
    task_id = params[:task]
    term = params[:term]
    return normal_status(-1, "缺少url参数") unless url.present?
    return normal_status(-1, "缺少term参数") unless term.present?
    return normal_status(-1, "缺少task参数") unless task_id.present?
    glcc_mate = GlccMediumTermExamineMaterial.new(code_or_pr_url: url, task_id: task_id, term: term, created_on:Time.now)
    state = glcc_mate.check_pr_url
    errors = glcc_mate.gennerate_content(state)
    render_ok({ state:state, state_html: errors})
  end


  private
    def get_navbar 
      @navbar = default_laboratory.navbar.sort_by{|e| e["index"].to_i }
      # if User.current.logged?
      #   pernal_index = {"name"=>"个人主页", "link"=>get_site_url("url", "#{Rails.application.config_for(:configuration)['platform_url']}/current_user"), "hidden"=>false}
      #   @navbar << pernal_index
      # end
    end

    def site_page_deploy_domain
      @deploy_domain = EduSetting.find_by_name("site_page_deploy_domain").try(:value)
    end

    def get_add_menu
      @add = []
      Site.add.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site|
        hash = {}
        site.each {|k, v|
          hash.merge!("#{k}":  get_site_url(k, v))
        }
        @add << hash
      end
    end

  def get_sub_competitions
    @sub_competitions = []
    Site.competition.pluck(:key).uniq.each do |key|
      hash = {"identifier": "#{key.to_s}"}
      hash.merge!("list": Site.competition.where(key: key).select(:id, :name, :url, :key).to_a.map(&:serializable_hash))
      @sub_competitions << hash
    end
  end

    def get_common_menu
      @common = {}
      Site.common.select(:url, :key).each do |site|
        next if site["url"].to_s.include?("current_user") && !User.current.logged?
        @common.merge!("#{site["key"]}": append_http(reset_site_url(site["url"])))
      end
    end

    def get_other_setting
      # code here
      @other_setting={}
      Site.other.select(:url, :key).each do |site|
        @other_setting.merge!("#{site["key"]}": site["url"])
      end
    end

    def get_personal_menu
      @personal = []
      if User.current.logged?
        Site.personal.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site|
          hash = {}
          site.each {|k, v|
            hash.merge!("#{k}": get_site_url(k, v))
          }
          @personal << hash
        end
      end
    end

    def get_third_party_new
      @third_party_new = []
      @third_party_new << {
        name: 'educoder',
        url: EducoderOauth.oauth_url,
        method: 'get'
      }
      platform_url = Rails.application.config_for(:configuration)['platform_url']
      config = Rails.application.config_for(:configuration)
      (config.dig("oauth").keys - ["educoder", "wechat"]).each do |provider|
        @third_party_new << {
          name: provider,
          url: "#{platform_url}/auth/#{provider}",
          method: 'get'
        }
      end
    end

    def get_third_party
      return [] if EduSetting.get("is_local") == "true"
      @third_party = []
      @third_party << {
        name: 'GitLink',
        url: GitlinkOauth.oauth_url
      }
      @third_party << {
        name: 'Openkyin',
        url: OpenkylinOauth.oauth_url
      }
    end

    def get_professional_fields
      # @professional_fields = Site.dict

      if Site.dict.count == 0
        "自动化、计算机、机械、材料、航空航天、电子信息、电气、医学、建筑、理学、教育、管理、其他".split("、").each_with_index do |key,index|
          Site.dict.create!(name: key, url: index, key: "professional_field")
        end
      end
      @professional_fields = Site.get_dict_data("professional_field")
    end

    def get_top_system_notification
      @top_system_notification = SystemNotification.is_top.first
    end

    def get_user_apply_signature
      @user_apply_signature = (ApplySignature.where("created_at > ?", "2024-04-07".to_time).passed.where(user_id: current_user.id) - ApplySignature.passed.joins(:apply_signature_read_details).where(apply_signature_read_details: {user_id: current_user.id}) ).last
    end

    def get_site_url(key, value)
      key.to_s === "url" ? append_http(reset_site_url(value)) : reset_site_url(value)
    end

    def reset_site_url(url)
      return url unless url.to_s.include?("current_user")

      split_arr = url.split('current_user')
      split_arr.length > 1 ? split_arr.join(current_user&.login) : (split_arr << current_user&.login).join('')
    end

    def append_http(url)
      url.to_s.start_with?("http") ? url : [request.protocol, request.host_with_port, url].join('')
    end
end
