# To change this template, choose Tools | Templates
# and open the template in the editor.


require 'gosu'

load 'GameWindow.rb'
load 'SaveLoadScores.rb'
load 'SaveLoadGame.rb'


class GameMenu < Gosu::Window

  #first create x and y coordinate values
  attr_accessor :x,:y
   

  def initialize()
    

    #create a 640 x 480 window.
    #set fullscreen : false
    #update interval : 20ms
    #you can only use one instance of this class at a time.
    super(800,600,false,20)

    #set the caption of the game window
    self.caption = 'Jumpy Rocket V1.0'
     
     
     @text = Gosu::Font.new(self,"Arial",20)

     #loading backround image
     @background_image = Gosu::Image.new(self,"./images/BackgroundMenu.png",true)
     @background_imageHelp = Gosu::Image.new(self,"./images/BackgroundMenuHelp.png",true)
     @background_imageScores = Gosu::Image.new(self,"./images/BackgroundMenuScores.png",true)
     @background_imageDiff = Gosu::Image.new(self,"./images/BackgroundMenuDifficulty.png",true)
     @arrow_image = Gosu::Image.new(self,"./images/arrow.png",true)
 
     @x_arrow =45
     @y_arrow =322
     
     @x_arrow2 =45
     @y_arrow2 = 322
     
    @xscore = 300
    @yscore =180
    
    @index = 0
    @indexdif = 0
     
    @music = Gosu::Song.new(self,"./sound/menusong.ogg")
    @music.play(looping = false)
    
    
    @musicitem = Gosu::Sample.new(self,"./sound/menuitem.ogg")
    
    @backgroundmain =true
    @backgroundhelp =false
    @backgroundscore =false
    @backgrounddif = false
    
    @scores = []
     

  end


  #updates 60 times per second
  def update
  
  end
    


  #updating by key stroke.  
   def button_down(id)
   
     
    if id == Gosu::KbEscape then 
       @backgroundhelp =false
       @backgroundmain =true
       @backgroundscore =false
       @backgrounddif = false
    end
    
    if id == Gosu::KbReturn then 
  
       if(@backgrounddif == true) then
         @music.stop
         close
         
         if(@indexdif == 0) then
           
          w = GameWindow.new("easy")
          w.show
         
          
           
         end
         
         if(@indexdif == 1) then
             w = GameWindow.new("medium")
             w.show
         end
         
         if(@indexdif == 2) then
             w = GameWindow.new("hard")
             w.show
         end
         
         if(@indexdif == 3) then
             w = GameWindow.new("extreme")
             w.show
         end
         
       end
       
       if(@index == 0) then
         
         @backgroundhelp =false
         @backgroundmain =false
         @backgroundscore =false
         @backgrounddif = true
         
         
         
       
        
       end
       
       if (@index == 1) then
         @backgroundhelp =true
         @backgroundmain =false
         @backgroundscore =false
         @backgrounddif = false
       end
       
        if (@index == 2) then
         @backgroundhelp =false
         @backgroundmain =false
         @backgroundscore =true
         @backgrounddif = false
         @xscore = 300
         @yscore =180
         @scores = SaveLoadScores.new.load
       end
       
       if (@index == 3) then
         @music.stop
         close
       end
       
      
     end
     
    if id == Gosu::KbUp then 
      if(@backgroundmain ==true) then 
       @index -=1   
       if(@index >-1) then
         @y_arrow -=40
       else
         @index =0
       end
       @musicitem.play(vol = 0.5, speed = 1, looping = false)
      end
      
     if(@backgrounddif ==true) then 
       @indexdif -=1   
       if(@indexdif >-1) then
         @y_arrow2 -=40
       else
         @indexdif =0
       end
       @musicitem.play(vol = 0.5, speed = 1, looping = false)
      end
       
       
    end
     
     if id == Gosu::KbDown then 
          if(@backgrounddif ==true) then 
        @indexdif +=1   
       if(@indexdif <4) then
         @y_arrow2 +=40  
       else
         @indexdif =3
       end
     
         @musicitem.play(vol = 0.5, speed = 1, looping = false) 
      end
     end
     
     
     
     if id == Gosu::KbDown then 
          if(@backgroundmain ==true) then 
        @index +=1   
       if(@index <4) then
         @y_arrow +=40  
       else
         @index =3
       end
     
         @musicitem.play(vol = 0.5, speed = 1, looping = false) 
      end
     end
    
    end
     
  
  def draw
  
   
    if(@backgroundmain ==true) then  
      @background_image.draw(0,0,0)
      @arrow_image.draw(@x_arrow,@y_arrow,1)
    end
     
    if(@backgroundhelp == true) then
      @background_imageHelp.draw(0,0,0)
    end
    
    if(@backgroundscore ==true) then
        @background_imageScores.draw(0,0,0)
        i=0
        @scores.each do |entry|
         if(i<10) then  
           @text.draw(entry[:score],@xscore, @yscore, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
           @text.draw(entry[:name],@xscore+50, @yscore, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
           
           @yscore +=20
           i+=1
         end
        end  
      @yscore =180
    end
    
    if(@backgrounddif ==true) then
      @background_imageDiff.draw(0,0,0)
      @arrow_image.draw(@x_arrow2,@y_arrow2,1)
    end
  end
  


end



