#displays the introduction screen
#when play is clicked load the first stage
$: << File.dirname(__FILE__)
require 'gosu'
require 'yaml.rb'
require 'level1.rb'
require 'level3.rb'
require 'chipmunk'
require 'enhanced_text_input'
require 'serialization.rb'

module Options 
  SCREEN_WIDTH = 640
  SCREEN_HEIGHT = 770
  TOP_COLOR = Gosu::Color.new(0xFF5c6263)
  BOTTOM_COLOR = Gosu::Color.new(0xFF04352f)
  # The number of steps to process every Gosu update 
  SUBSTEPS = 16
end



# Layering of sprites
module ZOrder
  Background, Player, Enemy, UI = *0..4
end

#draw rectangle in background
def draw_bg
  draw_quad(0,0,TOP_COLOR, Options::SCREEN_WIDTH, 0, TOP_COLOR, 0, Options::SCREEN_HEIGHT, Options::BOTTOM_COLOR, Options::SCREEN_WIDTH, Options::SCREEN_HEIGHT, Options::BOTTOM_COLOR, Background)
end

#the environment of the game
class MainMenu < Gosu::Window
  def initialize
    #check if highscore file was created
    #if not created create a new one
    if(File.exist?('highscores.yml') == false) then
      File.new("highscores.yml", "w")
    end

    super(Options::SCREEN_WIDTH, Options::SCREEN_HEIGHT, false, 16)
    self.caption = "Back to Black"
    @bg_image = Gosu::Image.new(self, "media/introScreen.png", false)
          
    @font = Gosu::Font.new(self, 'Arial', 12) 
    #enter username
    @text_username = Gosu::Font.new(self, 'Arial', 12)
    @text_username_input = EnhancedTextInput.new(self,250, 155, 0xff000000, @font, 'Enter Username', 150)
    self.text_input = @text_username_input
    
    @text_highscores = Gosu::Font.new(self, 'Arial', 13)
    @highscores = Array.new
    @serialisation = Serialization.new() 
    
    #get all scores from file
    myfile = File.open("highscores.yml","r+")
    yp = YAML::load_documents( myfile ) { |doc|      
      @temp = Array.new(2)
        @temp[0] = doc['username']
      @temp[1] = doc['score']  
      @temp[2] = doc['level']
      @highscores.push(@temp)
    }   
    myfile.close
    @highscores_sorted = @highscores.sort_by{|a|Integer(a[1]) * -1}    
    
    #fill an other array with the 10 largest values
    @top_scores = Array.new
    @count = 0;
    @highscores_sorted.each do |score|
      if(@count < 10) then
        score[0] = (@count + 1).to_s() + ".  "+score[0]
        @top_scores[@count] = score
        @count +=1
      end
    end
   
    @highscore_y = 360  
    @username = ''

    #background sound
    @bkg = Gosu::Song.new(self, "media/bg_music.ogg")
    #@bkg = Gosu::Song.new(self, "media/bgSound2.mid")
    @bkg.play(true)

  end
  
  def button_down(id)
    if id == Gosu::KbReturn 
      close
      #check username - if exists load game state - else load new level
      
      game_window = Level1.new(@username)      
      game_window.show
      
    end   
    if id == Gosu::KbEscape
      close
    end
  end 
 
  def draw
    @bg_image.draw(0, 0, ZOrder::Background)
        
    #print all highscores
    @top_scores.each do |highscore|
      user = highscore[0]
      score = Integer(highscore[1])
      level = Integer(highscore[2])
      @text_highscores.draw(user + " " + score.to_s() + " <level " + level.to_s() + ">", 250, @highscore_y, 1, 1, 1, 0xff7c7c7c)
      @highscore_y += 15
      
      if(@highscore_y > (360 + ((@top_scores.count - 1) * 15)))  then
        @highscore_y = 360
      end   
    end 
    @username = @text_username_input.draw
  end
end

window = MainMenu.new
window.show