# encoding: utf-8 

namespace :build_new_member do
  task :set_gender => :environment do
    af = AttributeField.first({conditions:{key: "gender"}})
    af.markup_value = {"1"=>{"en"=>"Male", "zh_tw"=>"男性"}, "2"=>{"en"=>"Female", "zh_tw"=>"女性"}, "3"=>{"en"=>"Other", "zh_tw"=>"其他"}, "3"=>{"en"=>"Not public", "zh_tw"=>"不公開"}}
    af.markup = "select"
    af.locale = true
    af.save
  end

  task :setup_status_options => :environment do
    data_set = {
      :teacher=>{:status=>{:markup=>"select",:markup_value=>{"1"=>{"en"=>"Fulltime", "zh_tw"=>"全職"}, "2"=>{"en"=>"Adjunct", "zh_tw"=>"兼職"}, "3"=>{"en"=>"Co Hiring", "zh_tw"=>"合聘"}, "3"=>{"en"=>"Distinguished", "zh_tw"=>"特聘"}}}},
      :student=>{:status=>{:markup=>"select",:markup_value=>{"1"=>{"en"=>"Studying", "zh_tw"=>"在學"}, "2"=>{"en"=>"Drop-out", "zh_tw"=>"休學"}, "3"=>{"en"=>"Alumi", "zh_tw"=>"校友"}, "3"=>{"en"=>"Suspended", "zh_tw"=>"未完成"}}}},
      :staff=>{:staff=>{:markup=>"select",:markup_value=>nil}}
      }
      data_set.each do |key,data|
        role = Role.first({conditions:{key: key}})
        role.sub_roles.each do |sr|
          sr.attribute_fields.each do |af|
            if af.key == "status"
                        # binding.pry
              af.markup = data[:status][:markup]
              af.markup_value = data[:status][:markup_value]
              af.save
              # binding.pry
            end
          end
        end
      end

  end

  task :sub_role_tags_generate => :environment do
    SubRoleTag.destroy_all
    role_keys = %w{teacher staff student}
    role_keys.each do |role_key|
      role = Role.first({conditions:{key: role_key}})
      puts "Starting with Role with key: #{role_key}" 
      role.sub_roles.each do |sr|
        puts "Role : #{role_key} => #{sr.title}"
        for i in 1..7 
          sr.tags.build(:en =>  "Students-#{sr.title_translations['en']}_#{i}" , :zh_tw =>  "在校學生-#{sr.title_translations['zh_tw']}_#{i}" , :key =>  "#{role_key}_#{sr.title_translations['en'].parameterize}_#{i}" )
        end
        sr.save
      end
    end
  end

  task :fields_for_teacher => :environment do
    sub_role_attribute_fields_template = [
      {key: 'status', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"狀態", "en"=>"Status"}, neutral_title: nil },
      {key: 'other_position', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"其他職位", "en"=>"Other Position"}, neutral_title: nil },
      {key: 'research_domain', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"研究領域", "en"=>"Research Domain"}, neutral_title: nil },
      {key: 'teaching_domain', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"授課領域", "en"=>"Teaching Domain"}, neutral_title: nil }
    ]
    role = Role.first({conditions:{key: 'teacher'}})
    role.sub_roles.each{|sb|
      sb.attribute_fields.destroy_all
      sub_role_attribute_fields_template.each{|att_f|
        sb.attribute_fields.build att_f 
      }
     sb.save 
    }
  end

  task :fields_for_student => :environment do
    sub_role_attribute_fields_template = [
      {key: 'status', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"狀態", "en"=>"Status"}, neutral_title: nil },
      {key: 'entrance_year', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"入學年", "en"=>"Entrance Year"}, neutral_title: nil },
      {key: 'graduation_date', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"屆別", "en"=>"Graduation Date"}, neutral_title: nil },
      {key: 'actual_graduation', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"實際畢業年", "en"=>"Actual Graduation"}, neutral_title: nil }
    ]
    role = Role.first({conditions:{key: 'student'}})
    role.sub_roles.each{|sb|
      sb.attribute_fields.destroy_all
      sub_role_attribute_fields_template.each{|att_f|
        sb.attribute_fields.build att_f 
      }
     sb.save 
    }
  end

  task :fields_for_staff => :environment do
    sub_role_attribute_fields_template = [
      {key: 'status', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"狀態", "en"=>"Status"}, neutral_title: nil },
      {key: 'responsibilities', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"負責業務", "en"=>"Responsibilities"}, neutral_title: nil }
    ]
    role = Role.first({conditions:{key: 'staff'}})
    role = Role.create!({key: "staff", built_in: true, disabled: false, title_translations: {"en"=>"Staff", "zh_tw"=>"行政人員"}}) if role.nil?
    role.sub_roles.each{|sb|
      sb.attribute_fields.destroy_all
      sub_role_attribute_fields_template.each{|att_f|
        sb.attribute_fields.build att_f 
      }
     sb.save 
    }
  end

  task :matt_data => :environment do
    b = User.first({conditions:{email: 'matt@rulingcom.com'}})
    b.attribute_values.destroy_all

    data = [{:key=> 'last_name',:en=>'Fu',:zh_tw=>'傅'},
    {:key=> 'first_name',:en=>'Matt',:zh_tw=>'儒淵'},
    {:key=> 'nick_name',:en=> 'Matt K.',:zh_tw=> 'Matt K.'},
    {:key=> 'addr',:en=>'Earth',:zh_tw=>'302新竹縣竹北市嘉豐南路二段101號'},
    {:key=> 'phone',:en=>'035508881',:zh_tw=>'035508881'},
    {:key=> 'gender',:en=>'Male',:zh_tw=>'男'},
    {:key=> 'birthday',:en=>'1986',:zh_tw=>'1986'},
    {:key=> 'email',:en=>'matt@rulingcom.com',:zh_tw=>'matt@rulingcom.com'}]

    data_hash = Hash[data.map{|t| [t[:key],t] }]
    Info.first.attribute_fields.each do |profile_field|
       b.attribute_values.build(data_hash.fetch(profile_field.key).merge({:attribute_field_id=>profile_field.id}))
    end

    #==================================================================
    b.roles = []
    b.roles <<  Role.first({conditions:{key: 'teacher'}})
    b.roles <<  Role.first({conditions:{key: 'student'}})
    #==================================================================
    b.sub_roles = []
    sr_1 = b.roles.first.sub_roles.last
    sr_2 = b.roles.last.sub_roles.last
    b.sub_roles << sr_1 #use teacher data
    b.sub_roles << sr_2 #use student data
    #==================================================================
    # to get field for fast fill up:   sr_2.attribute_fields.map{|t| Hash[t.key,nil]} 
    {"status"=>"兼職","research_domain"=>"神經網絡","teaching_domain"=>"平行運算"}.each do |key, value|
      field = sr_1.attribute_fields.where({key: key}).first
      b.attribute_values.build({key=>value}.merge({:key=>field.key,:attribute_field_id=>field.id}))
    end

    {"status"=>"在學中","entrance_year"=>2010}.each do |key, value|
      field = sr_2.attribute_fields.where({key: key}).first
      b.attribute_values.build({key=>value}.merge({:key=>field.key,:attribute_field_id=>field.id}))
    end

    # b.attribute_values.build :key=> 'last_name',:en=>'Fu',:zh_tw=>'傅'
    # b.attribute_values.build :key=> 'first_name',:en=>'Matt',:zh_tw=>'儒淵'
    # b.attribute_values.build :key=> 'nick_name',:en=> 'Matt K.',:zh_tw=> 'Matt K.'
    # b.attribute_values.build :key=> 'addr',:en=>'Earth',:zh_tw=>'302新竹縣竹北市嘉豐南路二段101號'
    # b.attribute_values.build :key=> 'phone',:en=>'035508881',:zh_tw=>'035508881'
    # b.attribute_values.build :key=> 'gender',:en=>'Male',:zh_tw=>'男'
    # b.attribute_values.build :key=> 'birthday',:en=>'1986',:zh_tw=>'1986'
    # b.attribute_values.build :key=> 'email',:en=>'matt@rulingcom.com',:zh_tw=>'matt@rulingcom.com'
    b.save
  end

  task :chris_data => :environment do
    b = User.first({conditions:{email: 'chris@rulingcom.com'}})
    b.attribute_values.destroy_all
    b.attribute_values.build :key=> 'last_name',:en=>'Vilayphiou',:zh_tw=>'林'
    b.attribute_values.build :key=> 'first_name',:en=>'Christophe',:zh_tw=>'順發'
    b.attribute_values.build :key=> 'nick_name',:en=> 'Chris',:zh_tw=> 'Chris'
    b.attribute_values.build :key=> 'addr',:en=>'asdsaddasdasd',:zh_tw=>'302新竹縣竹北市嘉豐南路二段101號'
    b.attribute_values.build :key=> 'phone',:en=>'035508881',:zh_tw=>'035508881'
    b.attribute_values.build :key=> 'gender',:en=>'Male',:zh_tw=>'男'
    b.attribute_values.build :key=> 'birthday',:en=>'1990',:zh_tw=>'1990'
    b.attribute_values.build :key=> 'email',:en=>'chris@rulingcom.com',:zh_tw=>'chris@rulingcom.com'
    b.save
  end

  task :module_app => :environment do
    ModuleApp.create!(:key=>"new_member",
                              :title=>"new_member",
                              :version=> "0.1", 
                              :organization => "Rulingcom",
                              :author => "RD dep", 
                              :intro => "New interface for member menagement", 
                              :enable_frontend => true)
  end

  task :sub_role => :environment do
    teacher_role = Role.first({conditions:{key: 'teacher'}})
    teacher_role.sub_roles.destroy_all
    teacher_sub_roles ={
      :professor => {"zh_tw"=>"教授", "en"=>"Professor"}, 
      :lecturer => {"zh_tw"=>"講師", "en"=>"Lecturer"}, 
      :associate_professor => {"zh_tw"=>"副教授", "en"=>"Associate Professor"}, 
      :assistant_professor => {"zh_tw"=>"助理教授", "en"=>"Assistant Professor"}
    }

    teacher_sub_roles.each do |key,title|
      teacher_role.sub_roles.build :key=>key,:title_translations=>title,:built_in=>true
      puts "=SubRole key:#{key}\t Title:#{title}"
    end
    teacher_role.save
    p "==Finished building sub_roles for Teachers"

    student_role = Role.first({conditions:{key: 'student'}})
    student_role.sub_roles.destroy_all
    student_sub_roles ={
      :ph_d => {"zh_tw"=>"博士班", "en"=>"Ph.D"}, 
      :master => {"zh_tw"=>"碩士班", "en"=>"Master"}, 
      :bachelor => {"zh_tw"=>"學士班", "en"=>"Bachelor"}, 
    }

    student_sub_roles.each do |key,title|
      student_role.sub_roles.build :key=>key,:title_translations=>title,:built_in=>true
      puts "=SubRole key:#{key}\t Title:#{title}"
    end
    student_role.save
    p "==Finished building sub_roles for Students"

    staff_role = Role.first({conditions:{key: 'staff'}})
    staff_role.sub_roles.destroy_all
    staff_sub_roles ={
      :general => {"zh_tw"=>"一般", "en"=>"General"}
    }

    staff_sub_roles.each do |key,title|
      staff_role.sub_roles.build :key=>key,:title_translations=>title,:built_in=>true
      puts "=SubRole key:#{key}\t Title:#{title}"
    end
    staff_role.save
    p "==Finished building sub_roles for Staff"

    staff_role = Role.first({conditions:{key: 'staff'}})
    staff_role.sub_roles.destroy_all
    staff_sub_roles ={
      :general => {"zh_tw"=>"一般", "en"=>"General"}
    }

    staff_sub_roles.each do |key,title|
      staff_role.sub_roles.build :key=>key,:title_translations=>title,:built_in=>true
      puts "=SubRole key:#{key}\t Title:#{title}"
    end
    staff_role.save
    p "==Finished building sub_roles for Staff"

  end


  # task :status => :environment do
  #   teacher_role = Role.first({conditions:{key: 'teacher'}})
  #   teacher_role.statuses.destroy_all
  #   teacher_status ={
  #     :fulltime => {"en"=>"Full Time", "zh_tw"=>"全職"},
  #     :adjunct => {"en"=>"Adjunct", "zh_tw"=>"兼職"},
  #     :co_hiring => {"en"=>"Co Hiring", "zh_tw"=>"合聘"},
  #     :distinguished => {"en"=>"Distinguished", "zh_tw"=>"特聘"}
  #   }
  #   teacher_status.each do |key,title|
  #     teacher_role.statuses.build :key=>key,:title_translations=>title
  #     puts "=Status key:#{key}\t Title:#{title}"
  #   end
  #   teacher_role.save

  #   p "==Finished building status for Teachers"

  #   student_role = Role.first({conditions:{key: 'student'}})
  #   student_status.statuses.destroy_all
  #   student_status ={
  #     :studying => {"en"=>"Studying", "zh_tw"=>"在學"},
  #     :drop_out => {"en"=>"Drop-out", "zh_tw"=>"休學"},
  #     :alumi => {"en"=>"Alumi", "zh_tw"=>"校友"},
  #     :suspended => {"en"=>"Suspended", "zh_tw"=>"未完成"}
  #   }
  #   student_status.each do |key,title|
  #     student_role.statuses.build :key=>key,:title_translations=>title
  #     puts "=Status key:#{key}\t Title:#{title}"
  #   end
  #   student_role.save

  #   p "==Finished building status for Students"

  # end
  task  :build_sub_role_for_staff_1023 => :environment do
    role = Role.first(conditions: {key: 'staff'})
    staff_sub_roles ={
      :secretary => {"en"=>"Secretary", "zh_tw"=>"秘書"},
      :assistant => {"en"=>"Assistant", "zh_tw"=>"行政助理"},
      :research_assistant => {"en"=>"Research Assistant", "zh_tw"=>"研究助理"},
      :ta => {"en"=>"TA", "zh_tw"=>"助教"},
      :technical_specialist => {"en"=>"Technical Specialist", "zh_tw"=>"技士"}
    }
    staff_sub_roles.each do |key,item|
      role.sub_roles.build(:key=>key,:title_translations=> item)
    end
    role.save
  end

  task :build_staff_attribute_field_1023 => :environment do
    sub_role_attribute_fields_template = [
      {key: 'status', markup: "select", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"狀態", "en"=>"Status"}, neutral_title: nil },
      {key: 'responsibilities', markup: "text_field", locale: true, list_options: nil, built_in: true, disabled: false, locale_title_translations: {"zh_tw"=>"負責業務", "en"=>"Responsibilities"}, neutral_title: nil }
    ]
    role = Role.first({conditions:{key: 'staff'}})
    role.sub_roles.each{|sb|
      sb.attribute_fields.destroy_all
      sub_role_attribute_fields_template.each{|att_f|
        sb.attribute_fields.build att_f 
        }
     sb.save 
   }
   role.save
  end

  task :build_status_1023 => :environment do
    student_status ={
      :studying => {"en"=>"Studying", "zh_tw"=>"在學"},
      :drop_out => {"en"=>"Drop-out", "zh_tw"=>"休學"},
      :alumi => {"en"=>"Alumi", "zh_tw"=>"校友"},
      :suspended => {"en"=>"Suspended", "zh_tw"=>"未完成"}
    }

    role = Role.first(conditions: {key: 'student'})
    student_status.each do |key,item|
      role.statuses.build(:key=>key.to_s,:title_translations=>item)
    end
    role.save

    teacher_status ={
      :fulltime => {"en"=>"Full Time", "zh_tw"=>"全職"},
      :adjunct => {"en"=>"Adjunct", "zh_tw"=>"兼職"},
      :co_hiring => {"en"=>"Co Hiring", "zh_tw"=>"合聘"},
      :distinguished => {"en"=>"Distinguished", "zh_tw"=>"特聘"}
    }
    role = Role.first(conditions: {key: 'teacher'})
    teacher_status.each do |key,item|
      role.statuses.build(:key=>key.to_s,:title_translations=>item)
    end
    role.save

  end

  task :clean_up_attribute_value_for_deleted_fields => :environment do
    AttributeValue.all.reject{|t| !AttributeField.find(t.attribute_field_id).nil? rescue false }.each{|t| t.delete}
  end
end
