

module Sgf_Info

  def open_sgf_info(ninf)

    @sgf_info= Hash.new


    while ninf

      ninf.each_value{ |prop|
        if (prop.class == PInfo or prop.class == PRoot) and not @sgf_info.has_key?(prop.ident)
          @sgf_info[prop.ident]=TkVariable.new(prop.content)
    # elsif prop.class == PInfo_TO TODO when SGF5 comes out
        end
      }

      ninf= ninf.prev
    end

    @sgf_form= Form.new('sgf_info')

    frame= @sgf_form.add_frame('sgf_info.frame.players',1,1)

    add_label(frame,'name',1,0)
    add_label(frame,'rank',2,0)
    add_label(frame,'team',3,0)
    add_label(frame,'white',0,1)
    add_label(frame,'black',0,2)
    add_field(frame,'PW',1,1)
    add_field(frame,'WR',2,1)
    add_field(frame,'WT',3,1)
    add_field(frame,'PB',1,2)
    add_field(frame,'BR',2,2)
    add_field(frame,'BT',3,2)



    frame= @sgf_form.add_frame('sgf_info.frame.file',0,2)

    label='sgf_info.entry.file'

    @row=0
    add_entry(frame,'Name','GN',label)
    add_entry(frame,'Version','FF',label)
    add_entry(frame,'Event','EV',label)
    add_entry(frame,'Date','DT',label)
    add_entry(frame,'Copyright','CP',label)
    add_entry(frame,'Place','PC',label)
    add_entry(frame,'User','US',label)
    add_entry(frame,'Source','SO',label)
    add_entry(frame,'Comment','GC',label)

 if @sgf_info['SY']
    add_entry(frame,'Application','SY',label)
 else
    add_entry(frame,'Application','AP',label)
 end

    frame= @sgf_form.add_frame('sgf_info.frame.game',0,0)
    frame.grid('rowspan'=>2)
    @row=0
    add_entry(frame,'Game','ID')
    add_entry(frame,'Size','SZ')
    add_entry(frame,'Round','RO')
    add_entry(frame,'Time','TM')
    add_entry(frame,'Byo Yomi','OT')
    add_entry(frame,'Handicap','HA')
    add_entry(frame,'Komi','KM')
    add_entry(frame,'Rules','RU')
    add_entry(frame,'Result','RE')
    add_entry(frame,'Opening','ON')


    frame= @sgf_form.add_frame('sgf_info.frame.annot',1,2)
    frame.grid('sticky'=>'nsew')

    opts= TkDefaults::GUI['sgf_info.label.annot']
    TkLabel.new(frame,opts).pack


    opts= TkDefaults::GUI['sgf_info.text.annot']
    an= TkText.new(frame,opts).pack('fill'=>'both','expand'=>'yes')

    if @sgf_info['AN'].nil?
      @sgf_info['AN']= TkVariable.new('')
    else
      an.insert('end', @sgf_info['AN'].value)
    end

    an.configure('state'=>( @edit_sgf_info ? 'normal' : 'disabled'))

    frame= @sgf_form.add_frame('sgf_info.frame.buttons',1,0)
    frame.grid('ipadx'=>0, 'ipady'=>0)

    TkButton.new(frame,'text'=>'Apply','command'=> proc { modify_info(an); @sgf_form.destroy}).pack('side'=>'left') if  @edit_sgf_info
    TkButton.new(frame,'text'=>'Dismiss','command'=> proc { @sgf_form.destroy} ).pack('side'=>'left')

  end


  def modify_info(an)

    ninf=  @root

    @sgf_info.each { |key,var|
      if var.value.empty?
        ninf.delete(key) #if ninf.has_key?(key)
      else
        ninf.set(key, var.value)
        ninf.action(self,key)  if ninf[key].class == PInfo
        end
    }

    text= an.get(1.0,'end')
    if text.empty?
      ninf['AN'].values[0]= '' unless ninf['AN'].nil?
    else
      ninf.set('AN',text)
    end


  end


  def add_label(frame,text,col,row)
    @sgf_form.add_label(frame,'sgf_info.label.'+text,col,row)
  end


  def add_field(frame,key,col,row,label='sgf_info.entry')

    if @sgf_info[key].nil?
      @sgf_info[key]= TkVariable.new('')
    end
    st= editable(key) #( @edit_sgf_info ? 'normal' : 'disabled')
    if key == 'PW' or key == 'PB'
      tr,id=  PNames.to_english(@sgf_info[key].value)
      @sgf_info[key].value= tr if tr
      debug "#{col}-#{row}->#{id}" if id
    end
    f= @sgf_form.add_field(frame,label, @sgf_info[key], st,col,row)
#debug key + '->' + @sgf_info[key].value

  end

  def add_entry(frame,text,key,label='sgf_info.entry')
    add_label(frame,text,0,@row)
    add_field(frame,key,1,@row,label)
    @row+=1
  end

  def editable(key)

    if @edit_sgf_info and  Property::Klass[key] == PInfo
      return 'normal'
    else
      return 'disabled'
    end
  end

end

