# encoding: utf-8
require 'fileutils'
class Filefield
  include Mongoid::Document
  include Mongoid::Timestamps
  @ckeditor_path = Rails.root.to_s + '/app/assets/javascripts/ckeditor/'
  @model_path = Rails.root.to_s + '/app/models/'
  @helper_path = Rails.root.to_s + '/app/helpers/'
  @views_path = Rails.root.to_s + '/app/views/'
  @controller_path = Rails.root.to_s + '/app/controllers/'
  @app_path = File.expand_path(__dir__)
   def self.prevpath(dir_path)
	dir_path = dir_path.gsub('\\','/')
	dir_arr= dir_path.split('/')
	dir_str=''
	dir_arr.delete_at(-1)
	dir_arr.each_with_index do |path,index|
		dir_str = dir_str + path 
		if index != dir_arr.count - 1
			dir_str = dir_str +"/"
		end
	end
	return dir_str
  end
  @app_path = self.prevpath(@app_path)
  @app_path =  self.prevpath(@app_path)
  field :title, :type=> String ,default:"patchfile"
  #field :file_show, :type=> Hash ,default:{"ckeditor"=>0,"site-preference(two-site-logo)"=>0,"member"=>0,"accessibility"=>0}
  field :file_show, :type=> Hash ,default:{"ckeditor"=>{:files=>{'config@js@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=> @ckeditor_path},
																 'ckeditor@js'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=> @ckeditor_path}
																					},:status=>0} ,
											"site-preference(two-site-logo)"=>{:files=>{'site@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=>@model_path},
																				'application_helper@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=>@helper_path},
																				'preference@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/admin/', :dest=>@views_path+'admin/sites/'}
																				},:status=>0} ,
											"member"=>{:files=>{'member_profile@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/', :dest=>@model_path},
														'members_controller@rb'=>{:status=>0, :sc=>@app_path+'/updatefiles/admin/', :dest=>@controller_path+'admin/'},
														'_user_basic_passwd@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/admin/', :dest=>@views_path+'admin/members/'}
														},:status=>0} ,
											"accessibility"=>{:files=>{'back_end@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>@views_path+'layouts/'},
																'_form@html@erb'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>@views_path+'page_parts/'},
																'jquery-ui-1@12@1/'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>Rails.root.to_s + '/app/assets/stylesheets/lib/jquery-ui-1.12.1/'},
																'orbit_bar/'=>{:status=>0, :sc=>@app_path+'/updatefiles/accessibility/', :dest=>@views_path+'orbit_bar/'}
																},:status=>0}
											}
  field :recovery, :type=> String , default:"false"
  field :last_message , :type=> String , default:""
  field :num , default: 0
  after_update :updatefile
  def enabled_for?(lang)
  	if lang.nil?
  		return true
  	else
  		return self.choose_lang.include?(lang)
  	end
  end
  
  def backup_file(file_name,file_path)
	FileUtils.cd(file_path)
	if (file_name.include? "/") == false
		FileUtils.cp(file_name , file_name+'_back')
	else
		FileUtils.cd('..')
		@back_foler =file_path.dup #very important,don't just use '=',it will change original value when it change
		@back_foler[-1] = "_back/"
		FileUtils.cp_r(file_name+'.' , @back_foler)
	end
  end
  def recover_file(file_name,file_path)
	FileUtils.cd(file_path)
	if (file_name.include? "/") == false
		FileUtils.cp(file_name+'_back', file_name)
	else
		FileUtils.cd('..')
		@back_foler = file_name.dup #very important,don't just use '=',it will change original value when it change
		@back_foler[-1] = "_back/."
		if !Dir.exist?(file_name)
			FileUtils.mkdir_p file_name
		end
		FileUtils.cp_r(@back_foler, file_name)
	end
  end
  def copy_file(file_name,source_file_path,dest_file_path)
	FileUtils.cd(source_file_path)
	if (file_name.include? "/") == false
		FileUtils.cp(file_name , dest_file_path)
	else
		if !Dir.exist?(dest_file_path)
			FileUtils.mkdir_p dest_file_path
		end
		FileUtils.cp_r(file_name , dest_file_path+'..')
	end
  end
  def updatefile
	if self.num == 0
		self.last_message = ""
		if self.recovery == "true"
			self.file_show.each do |name,property|
				if property[:status].to_i == 1
					puts 'recovering all files of '+ name
					self.last_message += ('recovering all files of '+ name+'</br>')
				else
					@i = 0
					property[:files].each do |file_name,sub_property|
						if sub_property[:status].to_i == 1
							@i = @i + 1
						end
					end
					if(@i != 0)
						puts 'recovering part of files:'+ name
						self.last_message += ('recovering part of files:'+ name+'</br>')
					end
				end
				property[:files].each do |file_name,sub_property|
					file_name = file_name.gsub("@",".")
					if sub_property[:status].to_i == 1
						puts 'recovering '+file_name
						self.last_message += ('recovering '+file_name+'</br>')
						begin
							recover_file(file_name,sub_property[:dest])
							puts 'finish recovery'
							self.last_message += '<span style="color:green;">finish recovery</span></br>'
						rescue
							puts Dir.pwd
							puts 'error recovery'
							self.last_message += ('<span style="color:red;">Dir: '+Dir.pwd+'</span></br>')
							self.last_message += '<span style="color:red;">error recovery</span></br>'
						end
					end
				end
			end
		else
			self.file_show.each do |name,property|
				if property[:status].to_i == 1
					puts 'copying all files of '+ name
					self.last_message += ('copying all files of '+name+'</br>')
				else
					@i = 0
					property[:files].each do |file_name,sub_property|
						if sub_property[:status].to_i == 1
							@i = @i + 1
						end
					end
					if(@i != 0)
						puts 'copying part of files:'+ name
						self.last_message += ('copying part of files: '+name+'</br>')
					end
				end
				property[:files].each do |file_name,sub_property|
					file_name = file_name.gsub("@",".")
					if sub_property[:status].to_i == 1
						puts 'backuping '+file_name
						self.last_message += ('backuping '+file_name+'</br>')
						begin
							backup_file(file_name,sub_property[:dest])
							puts 'finish backup</br>'
							self.last_message += '<span style="color:green;">finish backup</span></br>'
						rescue
							puts Dir.pwd
							puts 'error backup</br>'
							self.last_message += ('<span style="color:red;">Dir: '+Dir.pwd+'</span></br>')
							self.last_message += '<span style="color:red;">error backup</span></br>'
						end
						puts 'copying '+file_name
						self.last_message += ('copying '+file_name+'</br>')
						begin
							copy_file(file_name,sub_property[:sc],sub_property[:dest])
							puts 'finish copy'
							self.last_message += '<span style="color:green;">finish copy</span></br>'
						rescue
							puts Dir.pwd
							self.last_message += ('<span style="color:red;">Dir: '+Dir.pwd+'</span></br>')
							puts 'error copy'
							self.last_message += '<span style="color:red;">error copy</span></br>'
						end
					end
				end
			end
		end
		self.num = 1
		self.save
	end
	Dir.chdir(Rails.root.to_s)
  end
end
