require 'forwardable'

def cb_search
    return unless wait_for_loading&&$main_bible.valid?
    req=Request::new(Request::SEARCH,$main_textview)
    return unless req.valid
    return unless req
    $request.push req
    req.show
    $request.shift if $request.length>MAX_REQUEST
    $casual_request=$request.length-1
    update_forrew_buttons
end

class BookRef 
    extend Forwardable
    attr_reader :book,:active,:title
    def_delegators :book,:search_expression
    def initialize(book,title,number,active=false)
	@book=book
	@title=title
	@number=number
	@active=active
    end
    def toggle_active
	@active=!@active
    end
end

class SearchDialog < Gtk::Dialog
    NONE=0;
    BEGIN_OF_WORDS=1;
    END_OF_WORDS=2;
    WHOLE_WORDS=3;

    @@options=Hash::new
    @@options["no_accents"]=false;
    @@options["no_case"]=true;
    @@options["no_punct"]=true;
    @@options["position"]=NONE;
    @@options["whole_bible"]=true;
    @@options["is_regexp"]=false;
    def SearchDialog::options
	@@options
    end
    @@whole_list=nil
    @@target_list=nil
    def SearchDialog::init_whole_list
	@@whole_list=Array::new
	@@target_list=Array::new
	$main_bible.book_refs_list.each do |book,name,noc| 
	    @@whole_list.push BookRef::new(book,name,noc,true)
	    @@target_list.push BookRef::new(book,name,noc)
	end
    end
    def SearchDialog::whole_list
	@@whole_list
    end

    def initialize(bible,text="")
	super("Rechercher une expression", 
	      $main_window, 
	      Gtk::Dialog::MODAL|Gtk::Dialog::DESTROY_WITH_PARENT 
	     )
	help_button=create_button(Gtk::Stock::HELP,$lang.menu("9002"),Gtk::IconSize::MENU)
	action_area.pack_start(help_button,false,false,0)
	accel_group=Gtk::AccelGroup::new
	add_accel_group(accel_group)
	# otherwise the help button will not appear!
	action_area.show_all
	action_area.layout_style=Gtk::ButtonBox::EDGE
	cancel_button=add_button(Gtk::Stock::CANCEL,Gtk::Dialog::RESPONSE_CANCEL)
	ok_button=add_button(Gtk::Stock::OK,Gtk::Dialog::RESPONSE_OK)
	cancel_button.add_accelerator('clicked',accel_group,Gdk::Keyval::GDK_Escape,0,0)
	ok_button.add_accelerator('clicked',accel_group,Gdk::Keyval::GDK_Return,0,0)
	help_button.add_accelerator('clicked',accel_group,Gdk::Keyval::GDK_F1,0,0)
	help_button.signal_connect('clicked'){cb_help("3000")}
	hbox=Gtk::HBox::new(false,0)
	vbox.pack_start(hbox)
	img=Gtk::Image::new(Gtk::Stock::DIALOG_QUESTION,Gtk::IconSize::DIALOG)
	hbox.pack_start(img,false,false,0)
	vboxr=Gtk::VBox::new(false,10)
	hbox.pack_start(vboxr,true,true,0)

	target_frame=Gtk::Frame::new($lang.menu("3100"))
	target_frame_vbox=Gtk::VBox::new(false,0)
	target_frame.add(target_frame_vbox)
	@target_whole_button=Gtk::RadioButton::new($lang.menu("3110"))
	target_frame_vbox.pack_start(@target_whole_button,false,true,0)
	#target_select_hbox=Gtk::HBox::new(false,0)
	@target_select_button=Gtk::RadioButton::new(@target_whole_button,$lang.menu("3120"))
	target_frame_vbox.pack_start(@target_select_button,false,true,0)
	@target_select_scrolled=Gtk::ScrolledWindow::new()
	@target_select_store=Gtk::ListStore::new(BookRef)
	@@target_list.each do |br|
	    @target_select_store.append[0]=br
	end
	@target_select_view=Gtk::TreeView::new()
	@target_select_view.model=@target_select_store
	toggle_renderer=Gtk::CellRendererToggle::new
	@target_select_view.insert_column(0,$lang.menu("3121"),toggle_renderer){|tvc,cell,model,iter| cell.active=iter.get_value(0).active}
	@target_select_view.insert_column(1,$lang.menu("3122"),Gtk::CellRendererText::new){|tvc,cell,model,iter| cell.text=iter.get_value(0).title}
	toggle_renderer.signal_connect('toggled') {|cell,path| target=@target_select_store.get_iter(path)[0];target.toggle_active}
	@target_select_view.selection.mode=Gtk::SELECTION_MULTIPLE
	@target_select_scrolled.add(@target_select_view)
	@target_select_scrolled.set_policy(Gtk::PolicyType::AUTOMATIC,Gtk::PolicyType::AUTOMATIC)
	@target_select_scrolled.width_request=300
	@target_select_scrolled.height_request=200
	hbox.pack_end(@target_select_scrolled,true,true,0)
	vboxr.pack_start(target_frame,false,true,0)
	@target_select_button.signal_connect('clicked'){|widget| if @target_select_button.active? then @target_select_scrolled.show else @target_select_scrolled.hide end}


	expression_frame=Gtk::Frame::new($lang.menu("3200"))
	expression_frame_vbox=Gtk::VBox::new(true,0)
	expression_frame.add(expression_frame_vbox)
	@expression_entry=Gtk::Entry::new
	@expression_entry.text=text
	expression_frame_vbox.pack_start(@expression_entry,false,false,0)
	if $general_options["regexps"] then
	    @keywords_radio=Gtk::RadioButton::new($lang.menu("3210"))
	    @keywords_radio.signal_connect('clicked'){|widget| if widget.active? then @@options["is_regexp"]=false; @char_frame.sensitive=true;@words_frame.sensitive=true; end}
	    expression_frame_vbox.pack_start(@keywords_radio)
	    @regexp_radio=Gtk::RadioButton::new(@keywords_radio,$lang.menu("3220"))
	    @regexp_radio.signal_connect('clicked'){|widget| if widget.active? then @@options["is_regexp"]=true; @char_frame.sensitive=false;@words_frame.sensitive=false; end}
	    expression_frame_vbox.pack_start(@regexp_radio)
	else
	    @@options["is_regexp"]=false
	end
	vboxr.pack_start(expression_frame,false,true,0)


	options_table=Gtk::Table::new(1,2,true)
	vboxr.pack_start(options_table,true,true,0)
	@char_frame=create_char_frame
	options_table.attach(@char_frame,0,1,0,1,Gtk::FILL,Gtk::FILL,10,10)
	@words_frame=create_words_frame
	options_table.attach(@words_frame,1,2,0,1,Gtk::FILL,Gtk::FILL,10,10)
	# pas avant
	if $general_options["regexps"] then
	    if @@options["is_regexp"] then @regexp_radio.active=true else @keywords_radio.active=true end
	end

	hbox.show_all
	@target_select_button.active=true
	@target_whole_button.active=true
    end
    def create_char_frame
	frame=Gtk::Frame::new($lang.menu("3300"))
	vbox=Gtk::VBox::new(true)
	frame.add(vbox)
	@no_accents_check=Gtk::CheckButton::new($lang.menu("3310"))
	@no_accents_check.signal_connect('clicked'){|widget| @@options["no_accents"]=widget.active?}
	vbox.pack_start(@no_accents_check)
	@no_accents_check.active=@@options["no_accents"];
	@no_case_check=Gtk::CheckButton::new($lang.menu("3320"))
	@no_case_check.signal_connect('clicked'){|widget| @@options["no_case"]=widget.active?}
	vbox.pack_start(@no_case_check)
	@no_case_check.active=@@options["no_case"];
	@no_punct_check=Gtk::CheckButton::new($lang.menu("3330"))
	@no_punct_check.signal_connect('clicked'){|widget| @@options["no_punct"]=widget.active?}
	vbox.pack_start(@no_punct_check)
	@no_punct_check.active=@@options["no_punct"];
	frame
    end
    def create_words_frame
	frame=Gtk::Frame::new($lang.menu("3400"))
	vbox=Gtk::VBox::new(true)
	frame.add(vbox)
	@position_none_radio=Gtk::RadioButton::new($lang.menu("3410"))
	@position_none_radio.signal_connect('clicked'){|widget| if widget.active? then @@options["position"]=NONE end}
	vbox.pack_start(@position_none_radio)
	@position_begin_radio=Gtk::RadioButton::new(@position_none_radio,$lang.menu("3420"))
	@position_begin_radio.signal_connect('clicked'){|widget| if widget.active? then @@options["position"]=BEGIN_OF_WORDS end}
	vbox.pack_start(@position_begin_radio)
	@position_end_radio=Gtk::RadioButton::new(@position_none_radio,$lang.menu("3430"))
	@position_end_radio.signal_connect('clicked'){|widget| if widget.active? then @@options["position"]=END_OF_WORDS end}
	vbox.pack_start(@position_end_radio)
	@position_word_radio=Gtk::RadioButton::new(@position_none_radio,$lang.menu("3440"))
	@position_word_radio.signal_connect('clicked'){|widget| if widget.active? then @@options["position"]=WHOLE_WORDS end}
	vbox.pack_start(@position_word_radio)
	case @@options["position"]
	when NONE: @position_none_radio.active=true;
	when BEGIN_OF_WORDS: @position_begin_radio.active=true;
	when END_OF_WORDS: @position_end_radio.active=true;
	when WHOLE_WORDS: @position_word_radio.active=true;
	end
	frame
    end

    def list
	if @target_whole_button.active?
	    return @@whole_list
	else
	    return @@target_list
	end
    end
    def expression
	@expression_entry.text
    end
end
