package com.example.j9.triqshot;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

//1 = MainMenu
//2 = LevelMenu
//31 = Level 1

public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;
//Where the focus is
private int Focus = 1;

private MainMenu mainMenu;
private LevelsMenu levelsMenu;
private Level1 level1;

public static AudioManager AM;

private Context context;

//Class Constructor
public GameView(Context context) {
   super(context);
  
   this.context = context;
  
   //Create the main thread of the game
   getHolder().addCallback(this);
   thread = new MainThread(getHolder(), this);
   setFocusable(true);
  
   mainMenu = new MainMenu(context);
   //Create Audio Support
   AM = new AudioManager(context, R.raw.main_bgmusic);
  }

//When the app is created
@Override
public void surfaceCreated(SurfaceHolder holder) {
   thread.setRunning(true);
   thread.start();
  }

//When the app is destroyed
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
   boolean retry = true;
   while (retry) {
    try {
     thread.setRunning(false);
     thread.join();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    retry = false;
   }
  }


//Set the focus of Graphics Controller
@Override
public void draw(Canvas canvas) {
   super.draw(canvas);
   if(Focus == 1){
    mainMenu.draw(canvas);
   }
   if(Focus == 2){
    levelsMenu.draw(canvas);
   }
   if(Focus == 31){
    level1.draw(canvas);
   }
  }

//Set the focus of the Input Controller
public boolean Touched(View v, MotionEvent event){
   if(Focus == 1){
    return mainMenu.Touched(v, event);
   }
   if(Focus == 2){
    return levelsMenu.Touched(v, event);
   }
   if(Focus == 31){
    return level1.Touched(v, event);
   }
   return false;
  }

public void setFocus(int FocusNumber){
   //Set focus to MainMenu
   if(FocusNumber == 1){
    mainMenu = new MainMenu(context);
    Focus = FocusNumber;
    levelsMenu = null;
   }
   //Set focus to LevelsMenu
   if(FocusNumber == 2){
    levelsMenu = new LevelsMenu(context);
    Focus = FocusNumber;
    mainMenu = null;
    level1 = null;
    AM.setSong(R.raw.main_bgmusic);
   }
   //Set focus to Level 1
   if(FocusNumber == 31){
    level1 = new Level1(context);
    Focus = FocusNumber;
    levelsMenu = null;
    AM.setSong(R.raw.lvlmusic);
   }
  }
}