require 'rexml/document'

CONF_FILE=ENV["HOME"]+"/.bibliconcrc"
$bibles_directory=BIBLICONC_BASE_DIRECTORY.to_s+"/bibles/"

class Configg
    NONE=0
    MAIN=1
    SELECTION=2
    NO_SELECTION=3

    @@lang=""
    @@main_bible=""
    @@optional_bible=Array::new

    attr_reader :name
    def initialize(el)
	@name=el.attributes["name"]
	@default=(el.attributes["default"]&&el.attributes["default"]=="true")
	@optional_bible=Array::new
	el.each_element do |opt|
	    case opt.name
	    when "lang": @lang=opt.text.strip
	    when "main_bible": @main_bible=opt.text.strip
	    when "optional_bible": @optional_bible.push opt.text.strip
	    end
	end
    end

    def write
	conf=REXML::Element::new("config")
	if !@default then conf.attributes["name"]=@name else conf.attributes["default"]="true" end
	conf.add_element("lang").text=@name
	conf.add_element("main_bible").text=@main_bible
	@optional_bible.each do |ob|
            conf.add_element("optional_bible").text=ob
	end
	conf
    end
	
    def load
	$lang=Lang::new(@lang)
	$main_load_thr=Thread::new { 
		$main_bible=BibleWhole::new($bibles_directory+@main_bible) 
		if !$main_bible.valid? then 
		    $main_bible=BibleWhole::new(BIBLICONC_BASE_DIRECTORY.to_s+"/bibles/"+@main_bible) 
		    if $main_bible.valid? then $bibles_directory=BIBLICONC_BASE_DIRECTORY.to_s+"/bibles/" end
		end
		$optional_bible.clear
		@optional_bible.each do |b|
		    $optional_bible.push BibleWhole::new($bibles_directory+b)
		end
		SearchDialog::init_whole_list
	}
	#$main_load_thr.priority=1
	$main_load_thr.run
    end
    def Configg::load_default(el)
	el.each_element do |opt|
            case opt.name
            when "lang": @@lang=opt.text.strip
            when "main_bible": @@main_bible=opt.text.strip
            when "optional_bible": @@optional_bible.push opt.text.strip
            end
        end
    end

    def Configg::default(main=nil,optionals=nil)
	if !main then main=$main_bible.xml_name end
	if !optionals then optionals=$optional_bible.map{|ob| ob.xml_name} end
	el=REXML::Element::new("config")
	el.add_element("lang").text=$lang.xml_name
	el.add_element("main_bible").text=main
	optionals.each{|opt| el.add_element("optional_bible").text=opt}
	Configg::new(el)
    end
    def selection_of(xml_name)
	if xml_name==@main_bible then return MAIN end
	@optional_bible.each {|b| if b==xml_name then return SELECTION end}
	return NO_SELECTION
    end


    def Configg::read_config
	#defaults
	$optional_bible=Array::new()
	@@lang="francais.xml"
	@@main_bible="francais_darby.xml"
	@@optional_bible=Array::new
	$general_options["save_location"]=false
	$general_options["save_size"]=false
	$general_options["regexps"]=false
	$general_options["is_regexp"]=false
	$config=Array::new
	#read
	begin
	    file=File::new(CONF_FILE)
	    doc=REXML::Document::new(file)
	    root=doc.root
	    return unless root.name="bibliquest_concordance_config"
	    root.each_element do |el|
		case el.name
		when "config": default=(el.attributes["default"]&&el.attributes["default"]=="true")
		    if default then Configg::load_default(el)
		    else conf=$config.push Configg::new(el) end
		when "bibles_directory": $bibles_directory=el.text.strip if FileTest::directory?(el.text.strip)
		when "lang": lang=el.text.strip
		when "main_bible": main_bible=el.text.strip
		when "optional_bible": optional_bible.push el.text.strip
		when "location":$general_options["save_location"]=true
		    el.text.strip.scan(/([-]?[0-9]+)([-+][0-9]+)/) {|x,y| $xmw=x.to_s.to_i;$ymw=y.to_s.to_i}
		when "size":
		    $general_options["save_size"]=true
		    el.text.strip.scan(/([-]?[0-9]+)([-+][0-9]+)/) {|x,y| $wmw=x.to_s.to_i;$hmw=y.to_s.to_i}
		when "options"
		    el.each_element do |opt|
			case opt.name
			when "regexps": $general_options["regexps"]=(opt.text.strip=="true")
			end
		    end
		end
	    end
	rescue
	    # no lang defined yet!
	end
	$lang=Lang::new(@@lang)
	Configg::default(@@main_bible,@@optional_bible).load
    end

    def Configg::write_config
	begin
	    file=File::new(CONF_FILE,"w+")
	    doc=REXML::Document::new
	    doc<<REXML::XMLDecl::new("1.0","UTF-8")
	    root=doc.add_element("bibliquest_concordance_config")
	    root.add_element("location").text=$main_window.position*"+" if $general_options["save_location"]
	    root.add_element("size").text=$main_window.size*"+" if $general_options["save_size"]
	    opts=root.add_element("options")
	    opts.add_element("regexps").text=$general_options["regexps"].to_s
	    root.add_element("bibles_directory").text=$bibles_directory
	    root.elements<<Configg::default.write
	    $config.each {|c| root.elements<<c.write}
	    form=REXML::Formatters::Pretty::new(4)
	    form.write(doc,file)
	rescue => err
	    print_error($lang.message("201")+":\n"+err)
	end
    end
end

