class SeminarTemplateSetting
	include Mongoid::Document
  include Mongoid::Timestamps
  DefaultMenu = ["introduction","news","registration","registered_participants","submission","album"]
  PagesInfo = {"introduction" => "seminars_page",
      "news" => "custom_announcement",
      "registration" => "seminar_registration",
      "registered_participants" => "seminar_registered_participants",
      "submission" => "seminar_submission",
      "album" => "custom_gallery"}
  belongs_to :seminar_main
  field :enable_custom_template, type: Boolean, default: false
  field :display_menu, type: Array, default: ["introduction","news","registration","submission","album"]
  field :announcement_limit, type: Integer , default: 4
  field :album_limit, type: Integer , default: 4
  field :website_title, type: String , default: "", localize: true
  field :footer_info, type: String , default: "", localize: true
  field :introduction, type: String , default: "", localize: true
  field :background_style, type: String , default: "1"
  field :content_style, type: String , default: "1"
  has_many :seminar_banner_images, :autosave => true, :dependent => :destroy
  accepts_nested_attributes_for :seminar_banner_images, :allow_destroy => true
  after_create do
    self.create_seminar_pages
  end
  after_save do
    if self.display_menu_changed?
        @seminar_page_root = Page.where(:parent_page_id=>self.seminar_main_id).first
        display_menu = self.display_menu
        available_locales = Site.first.in_use_locales rescue I18n.available_locales
        available_locales = available_locales.map{|l| l.to_s}
        pages_info = self.class::PagesInfo
        if @seminar_page_root
          default_display_menu = self.class::DefaultMenu
          default_display_menu.each do |page_id|
            page = @seminar_page_root.child_page.where(:page_id=>page_id).first
            if page
              if display_menu.include?(page_id)
                page.update(:enabled_for=>available_locales,:menu_enabled_for=>available_locales)
              else
                page.update(:enabled_for=>[],:menu_enabled_for=>[])
              end
            else
              page_module = pages_info[page_id]
              seminar_page = @seminar_page_root.child_page.where(:page_id=>page_id).first
              page_trans = available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("seminar.menu.#{page_id}")}]}.to_h
              available_locales_tmp = available_locales.clone
              @seminar_page_root.child_page.create(:name_translations=>page_trans,:page_id=>page_id,:url=>"/home/#{page_id}",:enabled_for=>available_locales_tmp,:module=>page_module,:menu_enabled_for=>available_locales_tmp)
            end
          end
        end
    end
  end
  def create_seminar_pages(force_update=true)
    if self.seminar_main
      @seminar = self.seminar_main
      @seminar_page_root = Page.where(:parent_page_id=>self.seminar_main_id).first
      available_locales = Site.first.in_use_locales rescue I18n.available_locales
      available_locales = available_locales.map{|l| l.to_s}
      if @seminar_page_root.nil?
        @seminar_page_root = Page.create(:parent_page_id=>self.seminar_main_id,:module=>"seminars_home",:name_translations=>@seminar.title_translations,:url=>"/home",:page_id=>"home",:enabled_for=>available_locales,:bind_uid=>@seminar.uid,:bind_module_app=>"seminar",:bind_model=>"SeminarMain",:menu_enabled_for=>available_locales)
      else
        @seminar_page_root.update(:name_translations=>@seminar.title_translations,:module=>"seminars_home",:url=>"/home",:page_id=>"home",:enabled_for=>available_locales,:bind_uid=>@seminar.uid,:bind_module_app=>"seminar",:bind_model=>"SeminarMain",:menu_enabled_for=>available_locales)  if force_update
      end
      display_menu = self.display_menu
      pages_info = PagesInfo
      relation_model = {"news" => "CustomBulletin","album"=>"CustomAlbum"}
      pages_info.each do |page_id , page_module|
        seminar_page = @seminar_page_root.child_page.where(:page_id=>page_id).first
        page_trans = available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("seminar.menu.#{page_id}")}]}.to_h
        available_locales_tmp = available_locales.clone
        if display_menu.exclude?(page_id)
          available_locales_tmp = []
        elsif relation_model[page_id]
          relation = relation_model[page_id].constantize rescue nil
          if relation
            if relation.where(:bind_uid=>@seminar.uid).count == 0
              available_locales_tmp = []
            end
          end
        end
        if seminar_page.nil?
          seminar_page = @seminar_page_root.child_page.create(:name_translations=>page_trans,:page_id=>page_id,:url=>"/home/#{page_id}",:enabled_for=>available_locales_tmp,:module=>page_module,:menu_enabled_for=>available_locales_tmp)
        else
          seminar_page.update(:name_translations=>page_trans,:page_id=>page_id,:url=>"/home/#{page_id}",:module=>page_module) if force_update
        end
      end
      return @seminar_page_root
    end
  end
  def self.initialize_variables
  	app_path = Pathname.new(__FILE__).dirname.dirname
  	@@background_style_infos = {}
  	Dir.glob("#{app_path}/assets/stylesheets/seminar/background_style/*/").each do |folder|
  		files = Dir.glob("#{folder}/*")
  		image_file = files.select{|f| File.basename(f).match(/jpg|gif|png|jpeg/i)}
        folder_name = File.basename(folder)
        image_file = image_file.select{|f| File.basename(f).include?(folder_name)}.first
  		key = File.basename(folder)
  		image_path = "/assets/seminar/background_style/#{key}/#{File.basename(image_file) rescue ''}"
  		default_logo = nil
        if File.exist?("#{folder}logo.jpg")
            default_logo = "#{folder}logo.jpg".split("app")[1..-1].join("app").sub("/stylesheets/","/")
        end
        @@background_style_infos[key] = {
  			"image"=>image_path,
			"css"=> get_folder_content(folder,"css"),
			"js"=> get_folder_content(folder,"js"),
            "default_logo"=> default_logo
  		}
  	end
  	@@background_style_infos = @@background_style_infos.sort_by {|k, v| (k.match(/\d+/)[0].to_i rescue 0)}.to_h
  	@@content_style_infos = {}
  	Dir.glob("#{app_path}/assets/stylesheets/seminar/content_style/*/").each do |folder|
  		files = Dir.glob("#{folder}/*")
  		image_file = files.select{|f| File.basename(f).match(/jpg|gif|png|jpeg/i)}
        folder_name = File.basename(folder)
        image_file = image_file.select{|f| File.basename(f).include?(folder_name)}.first
  		key = File.basename(folder)
  		image_path = "/assets/seminar/content_style/#{key}/#{File.basename(image_file) rescue ''}"
  		template_info = (JSON.parse(File.read("#{folder}/info.json")) rescue {"template"=> "horizontal"})
  		@@content_style_infos[key] = {
  			"image"=>image_path,
  			"css"=> get_folder_content(folder,"css"),
  			"js"=> get_folder_content(folder,"js"),
            "info"=>template_info,
  			"template"=>"#{app_path}/assets/stylesheets/seminar/templates/#{template_info["template"]}/"
  		}
  	end
  	@@content_style_infos = @@content_style_infos.sort_by {|k, v| (k.match(/\d+/)[0].to_i rescue 0)}.to_h
  end
  def self.get_folder_content(folder,content)
  	return Dir.glob("#{folder}#{content}/*").map{|content| content.split("app")[1..-1].join("app").sub("/stylesheets/",'/')}
  end
  def self.background_style_infos
  	@@background_style_infos
  end
  def self.content_style_infos
  	@@content_style_infos
  end
  initialize_variables
end