class Projects::ListMyQuery < ApplicationQuery

  attr_reader :params, :user, :is_current_admin_user

  # sort_columns :updated_on, :created_on, :forked_count, :praises_count, default_by: :updated_on, default_direction: :desc

  def initialize(params,user,is_current_admin_user)
    @params = params
    @user = user
    @is_current_admin_user = is_current_admin_user
  end

  def call
    if is_current_admin_user 
      projects = Project
    else 
      projects = Project.visible
    end

    if params[:category].blank?
      normal_projects = projects.members_projects(user.id).to_sql
      org_projects = projects.joins(team_projects: [team: :team_users]).where(team_users: {user_id: user.id}).to_sql
      watched_projects = projects.where.not(user_id: user.id).joins(:watchers).where(watchers: {watchable_type: "Project", user_id: user.id}).to_sql
      projects = Project.from("( #{ normal_projects} UNION #{ org_projects } UNION #{ watched_projects } ) AS projects")#.distinct
    elsif params[:category].to_s == "join"
      normal_projects = projects.where.not(user_id: user.id).members_projects(user.id).to_sql
      org_projects = projects.joins(team_projects: [team: :team_users]).where(team_users: {user_id: user.id}).to_sql
      projects = Project.from("( #{ normal_projects} UNION #{ org_projects } ) AS projects")#.distinct
    elsif params[:category].to_s == "manage"
      projects = projects.where(user_id: user.id)
    elsif params[:category].to_s == "watched"  #我关注的
      projects = projects.where.not(user_id: user.id).joins(:watchers).where(watchers: {watchable_type: "Project", user_id: user.id})
    elsif params[:category].to_s == 'only_watched'
      projects = projects.joins(:watchers).where(watchers: {watchable_type: "Project", user_id: user.id})
    elsif params[:category].to_s == "forked"  #我fork的
      fork_ids = user.fork_users.select(:id, :fork_project_id).pluck(:fork_project_id)
      projects = projects.where(id: fork_ids)
    elsif params[:category].to_s == "admin"
      normal_projects = projects.joins(members: :roles).where(members: {user_id: user.id}, roles: {name: %w(Manager)}).to_sql
      org_projects = projects.joins(team_projects: [team: :team_users]).where(teams: {authorize: %w(owner admin)},team_users: {user_id: user.id}).to_sql
      projects = Project.from("( #{ normal_projects} UNION #{ org_projects } ) AS projects")#.distinct
    # elsif params[:category].to_s == "public" 
    #   projects = projects.visible.joins(:members).where(members: { user_id: user.id })
    # elsif params[:category].to_s == "private"
    #   projects = projects.is_private.joins(:members).where(members: { user_id: user.id })
    elsif params[:category].to_s == "blockchain_token" # 所有钱包中有token的项目有哪些
      token_list, total_count = find_repo_with_token(user.id)
      project_names = token_list.map { |x| x['token_name']  }
      projects = projects.where(name: project_names)
      tokens = token_list.map { |x| x['balance']  }
      puts "pause"
    end

    if params[:is_public].present?
      projects = projects.is_private if params[:is_public].to_s == "private"
      projects = projects.visible if params[:is_public].to_s == "public"
    end

    if params[:project_type].to_s === "common"
      projects = projects.common
    elsif params[:project_type].to_s === "mirror"
      projects = projects.mirror
    elsif params[:project_type].to_s === "sync_mirror"
      projects = projects.sync_mirror
    end

    if params[:topic_name].present?
      projects = projects.with_project_topic_name(params[:topic_name].to_s.split(","))
    end

    if params[:topic_id].present?
      projects = projects.with_project_topic(params[:topic_id])
    end


    # 表情处理
    keywords = params[:search].to_s.each_char.select { |c| c.bytes.first < 240 }.join('')
    q = projects.ransack(name_or_identifier_cont: keywords)

    scope = q.result.includes(:project_category, :project_language,:owner, :repository, :has_pinned_users, :project_topics)

    sort = Project.column_names.include?(params[:sort_by]) ? params[:sort_by] : "updated_on"
    sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : "desc"

    @home_top_ids = scope.joins(:home_top_settings).where(home_top_settings: {user_id: user.id}).order("home_top_settings.created_at asc").pluck(:id)

    if params[:choosed].present? && params[:choosed].is_a?(Array)
      scope = scope.distinct.order("FIELD(projects.id, #{params[:choosed].reverse.join(",")}) desc")
    else
      if @home_top_ids.present?
        scope = scope.distinct.order("FIELD(projects.id, #{@home_top_ids.join(",")}) desc, projects.#{sort} #{sort_direction}")
      elsif params[:topic_name].present?
        scope = scope.distinct.order("project_topics.id asc, projects.#{sort} #{sort_direction}")
      else
        scope = scope.distinct.order("projects.#{sort} #{sort_direction}")
      end
    end

    return scope, @home_top_ids
  end
end
