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


require 'gosu'


load 'TextField.rb'
load 'SaveLoadScores.rb'





class GameMenuScore < Gosu::Window

  #first create x and y coordinate values
  attr_accessor  :lost
   

  def initialize(status,score)
    

    #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'

    
    @lost =true
    @score =score.ceil
    
    @musicexp = Gosu::Song.new(self,"./sound/shipexplosion.ogg")
    @musicvictory = Gosu::Song.new(self,"./sound/victory.ogg")
    
    if(status == "lost") then @lost =true else @lost =false end
    

    @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
    @font2 = Gosu::Font.new(self, Gosu::default_font_name, 30)
    @font3 = Gosu::Font.new(self, Gosu::default_font_name, 25)
    @background_imageScores = Gosu::Image.new(self,"./images/BackgroundMenuScores.png",true)
    
    if(@lost ==false) then
      # Set up an array of 1 text field.
      @text_fields = Array.new(1) { |index| TextField.new(self, @font, 50, 30 + index * 50) }
    
      index = @text_fields.index(self.text_input) || -1
      self.text_input = @text_fields[(index + 1) % @text_fields.size]
      
      @musicvictory.play(looping = false)
    else
       
        @musicexp.play(looping = false)
    end
     
  end


    #updates 60 times per second
    def update
  
    end
    


    #updating by key stroke.  
    def button_down(id)
   
     if id == Gosu::KbReturn then
       if(@lost==false) then
          s = SaveLoadScores.new
          s.save(@score, self.text_input.text)
          @musicvictory.stop
          close
          GameMenu.new.show
       else
          close
          @musicexp.stop
          GameMenu.new.show
       end
        
       
       
     end
    
    end
    
  
    def draw
    
      @background_imageScores.draw(0,0,0)
      
      if(@lost==false) then
        @font2.draw("You Won",330, 200, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
        @font3.draw("Your total score is "+@score.to_s,280, 290, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
        @text_fields.each { |tf| tf.draw }
      end
      
      if (@lost ==true) then
        
        @font2.draw("You Lost",330, 200, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
        @font3.draw("Your total score is "+@score.to_s,280, 290, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
        
      end
    end

end

