# == Schema Information
#
# Table name: glcc_medium_term_examine_material
#
#  id                :integer          not null, primary key
#  student_reg_id    :integer          not null
#  task_id           :integer          not null
#  defence_video_url :string(1000)     not null
#  code_or_pr_url    :string(1000)
#  PPT_attachment_id :integer          not null
#  term              :integer
#  created_on        :datetime
#  updated_on        :datetime
#  is_delete         :boolean          default("0"), not null
#  round             :integer          default("1"), not null
#

class GlccMediumTermExamineMaterial < ActiveRecord::Base
    self.table_name = "glcc_medium_term_examine_material"
    belongs_to :glcc_student, :class_name => :GlccRegistrationStudent, :foreign_key => "student_reg_id"
    belongs_to :task, :class_name => :GlccRegistrationTask, :foreign_key => "task_id"

    def check_pr_url
        state = []
        # code_or_pr_url =  "https://www.gitlink.org.cn/Gitlink/forgeplus/pulls/337"
        url_array = code_or_pr_url.split("/")
        gitlink_index = url_array.index("www.gitlink.org.cn") || url_array.index("gitlink.org.cn") || url_array.index("testforgeplus.trustie.net")
        pull_index = url_array.index("pulls") 

        #发送没有在gitlink上提交PR的邮件
        unless gitlink_index.present?
            state << 1
        end
            
        # 输入的地址有问题邮件内容
        unless pull_index == 5
            state << 2
        end
        user = Owner.find_by(login: url_array[3])
        project = Project.find_by(identifier: url_array[4], user_id:user.try(:id))
        pr = PullRequest.where(project:project, gitea_number:url_array[6])

        unless pr.present?
            state << 3
        end
        if white_list #特殊处理  白名单考核不处理
            state = []
        end
        state
    end


    def white_list
        # 全局设置白名单 key
        key = "glcc_white_task_#{self.created_on.year}"
        white_task = EduSetting.find_by_name(key)
        if white_task
            task_ids = white_task.value.split(",")
            task_ids.map(&:to_i).include?(self.task_id)
        else
            false
        end
    end

    def send_mail
        gcs = glcc_student
        mail = gcs.mail
        return if mail.nil? || code_or_pr_url.nil?

        state = check_pr_url
        return unless state.present?
        title = "#{self.created_on.year}年GitLink确实开源GLCC开源夏令营#{term == 1 ? "中期考核" : "结项考核"}提醒"
        content = gennerate_content(state)
        UserMailer.glcc_pr_check_email(mail,title, gcs.student_name, content).deliver_now
    end

    def state_to_html
        gcs = glcc_student
        mail = gcs.mail
        return "数据异常，PR链接为空" if mail.nil? || code_or_pr_url.nil?
        state = check_pr_url
        gennerate_content(state)
    end

    def gennerate_content(state)
        content = ""
        state.map{|e| 
            content = content + number_to_content(e) 
        }
        content
    end

    def number_to_content(num) 
        case num
        when 1
            "<p> PR链接为非GitLink平台链接 </p>"
        when 2
            "<p> PR链接为GitLink平台链接， 但非PR链接 </p>"
        when 3
            "<p> PR链接中的PR不存在 </p>"
        end
    end
end
