#coding=utf-8

class SyllabusesService

  include ApplicationHelper
  include CoursesHelper

  def judge_can_setting(sy,user)
    sy[:can_setting] = sy[:user_id] == user.id ? true : false

    sy[:can_setting] = false if sy[:id].nil?

    sy.courses.each do |c|
      c[:can_setting] = false

      member = c.members.where("user_id=#{user.id} and course_id=#{c.id}")[0]
      roleName = member.roles[0].name if member

      if roleName && (roleName == "TeachingAsistant" || roleName == "Teacher" )
        c[:can_setting] = true
      end

      if c.tea_id == user.id
        c[:can_setting] = true
      end
    end

    sy
  end
  #获取指定用户的课程大纲
  def user_syllabus(user)
    # courses = CoursesService.new.user_courses_list(user)
    #
    # other = Syllabus.new(title: '未命名课程',user_id: user.id)
    #
    # courses.each do |c|
    #   other.courses << c[:course] unless c[:course].syllabus
    # end
    result = []
    i = 0

    courses = user.courses.not_deleted
    syllabus_ids = courses.empty? ? '(-1)' : "(" + courses.map{|course| !course.syllabus_id.nil? && course.syllabus_id}.join(",") + ")"
    syllabuses = Syllabus.where("id in #{syllabus_ids} or user_id = #{user.id}").order("updated_at desc")

    syllabuses.each do |syllabus|
      tmp_courses = courses.where("syllabus_id = #{syllabus.id}").select("courses.*,(SELECT MAX(updated_at)  FROM `course_activities` WHERE course_activities.course_id = courses.id) AS updatetime").order("updatetime desc")
      result[i] = {:title=>syllabus.title, :id=>syllabus.id , :user_id=>syllabus.user_id,:courses=>tmp_courses }
      i = i + 1
    end
    # syllabuses.to_a
    # syllabuses
    result
  end

  def after_create_course(course, user)
      #unless User.current.admin?
      r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
      m = Member.new(:user => user, :roles => [r])
      m.project_id = -1
      course_info = CourseInfos.new(:user_id => user.id, :course_id => course.id)
      #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
      course.members << m
      course.course_infos << course_info
  end

  def send_wechat_create_class_notice user,course
    count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Course' and shield_id=#{course.id}").count
    if count == 0
      ws = WechatService.new
      title = "恭喜您创建班级成功。"
      name = course.syllabus.nil? ? course.name : course.syllabus.title+" • "+course.name
      ws.create_class_notice user.id, "create_course_notice", course.id,title, name, user.show_name, 1, "点击查看班级详情。"
    end
  end

  #创建大纲
  # params {title: '大纲名称', [{course}, {course}]}
  def create(user, title, courses = [])
    sy = Syllabus.new(title: title, user_id: user.id)
    ActiveRecord::Base.transaction() do
      sy.save!

      courses.each do |course|
        if ::Course === course
          course.syllabus_id = sy.id
          course.save!
          send_wechat_create_class_notice user,course
        elsif Hash === course
          c = ::Course.new(course)
          c.tea_id = user.id
          c.syllabus_id = sy.id
          c.update_default_value
          c.is_public = 0
          c.save!
          after_create_course(c, user)
          send_wechat_create_class_notice user,c
        end
      end

    end
    sy[:can_setting] = true
    sy
  end

  #修改课程大纲的名称、班级名称、新增班级
  def edit(user, option)
    courses = []
    status = -1
    syllabus_id = option[:id]

    sy = Syllabus.where("id=?",option[:id]).first

    if sy && sy.user_id == user.id
      syllabus_title = option[:title]

      sy.title = syllabus_title
      sy.save!
      #修改班级名称
      modify_courses = option[:modify_courses]
      modify_courses.each do |c|
        course = Course.where("id=?",c.id).first

        if course && course.tea_id == user.id
          course.name = c.name
          !course.save
        end
      end

      #新增班级
      add_courses = option[:add_courses]
      add_courses.each do |c|
        course = Course.new()
        course.name = c
        course.tea_id = user.id
        course.syllabus_id = sy.id
        course.update_default_value
        course.is_public = 0
        course.save!
        after_create_course(course, user)
        send_wechat_create_class_notice user,course
      end
      status = 0
    end
    status
  end

end
