package de.heatup.ui.actionPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.*;

import de.heatup.mapengine.MapGrid;
import de.heatup.ui.FontLoader;
import de.heatup.ui.Main;

public class ActionControl{
	private static int count = 0;
	private static long beforTime = 0;
	private static boolean keySequenceFail=false;
	private static int option=0;
	private static char chr;
	private static char arr[];
	private static JPanel panel;
	private static JProgressBar bar;
	private static JLabel actionLabel;
	private static JLabel bigActionLabel;
	private static int tmpCount=0;
	private static boolean keyA=true;
	private static Font ttfBase=FontLoader.getFont();
	private static Font emulogicaction;
	private static Font emulogicbigaction;
	
	public static void setPanel(JPanel newPanel){
		emulogicaction = ttfBase.deriveFont(Font.PLAIN, 12);
		emulogicbigaction= ttfBase.deriveFont(Font.LAYOUT_RIGHT_TO_LEFT, 25);	
		
		panel = newPanel;
		panel.setBackground(Color.BLACK);
		actionLabel = new JLabel();
		bigActionLabel = new JLabel();
		bar = new JProgressBar();
		bar.setForeground(Color.YELLOW);
		bar.setBackground(Color.BLACK);
		bar.setBorderPainted(false);
		//actionLabel.setFont(new Font("Arial", Font.CENTER_BASELINE, 16));
		actionLabel.setFont(emulogicaction);
		actionLabel.setHorizontalAlignment(JLabel.CENTER);
		actionLabel.setForeground(Color.YELLOW);
		//bigActionLabel.setFont(new Font("Arial", Font.LAYOUT_RIGHT_TO_LEFT,40));
		bigActionLabel.setFont(emulogicbigaction);
		bigActionLabel.setForeground(Color.RED);
		bigActionLabel.setHorizontalAlignment(JLabel.CENTER);
		bigActionLabel.setVisible(false);
		bar.setVisible(false);
		panel.setLayout(new BorderLayout());
		panel.add(actionLabel, BorderLayout.NORTH);
		panel.add(bar,BorderLayout.SOUTH);
		panel.add(bigActionLabel,BorderLayout.CENTER);
		resetActionPanel();
	}
	public static void setAction(int rd){
		setKeyA();
		if(rd==1){
			option=1;
			setSmash(new Smashing().getKey());
			String str= "Bitte die Taste --"+getSmash()+"-- so schnell wie moeglich druecken";
			actionLabel.setText(str);
			setProgressBar(0);
			beforTime=System.nanoTime();
		}else{
			option=2;
			setSequence(new Keysequence().getKeySequence());
			String str="Bitte diese Tastenkombination so schnell wie moeglich eingeben";
			actionLabel.setText(str);
			setProgressBar(100);
		}
	}
	public static void tick(){
		if(ActionWatcher.getAction()){
			if(option==1){				//Smashing-Tick
				if(bar.getValue()>=100){
					setAchiv(8_000_000_000L, System.nanoTime()-beforTime);
				}
			}else{ 		 				//KeySequence-Tick
				if(!getKeySequenceFail()){
					if(count<arr.length&&bar.getValue()>0){	
						bigActionLabel.setVisible(true);
						String str="";
						for(int i=count;i<arr.length;i++){
							str=str+getSequence()[i]+" ";
						}
						bigActionLabel.setText(str);
					}else{
						setAchiv(bar.getValue(),0);	
					}
				}else{
					setAchiv();
				}
			}
			tickProgressBar(); //ProgressBar-Tick
		}
	}
	public static boolean getKeyA(){
		return keyA;
	}
	private static void setKeyA(){
		keyA=false;
	}
	public static void resetKeyA(){
		keyA=true;
	}
	private static void tickProgressBar(){
		tmpCount++;
		if(option==1){
			if(bar.getValue()>1){
				if(tmpCount%2==0)bar.setValue(bar.getValue()-1);
				if(bar.getValue()<3)bar.setValue(0);
				
			}
		}else{
			if(tmpCount%3==0)bar.setValue(bar.getValue()-1);
		}
	}
	private static void setAchiv(double value1, double value2){
		ActionWatcher.setAction(false);
		if(value1<=value2){							
			resetActionPanel();
			actionLabel.setText("Zu langsam! Fehlgeschlagen!!!");
		}else{
			resetActionPanel();
			actionLabel.setText("Manipulation erfolgreich!");
			HardewareManipulate.setHardware(MapGrid.getGridCell(Main.getPlayer().getGridLocation().x, Main.getPlayer().getGridLocation().y).getCorrespondingHardwareToTrigger());
		}	
	}
	private static void setAchiv(){
		ActionWatcher.setAction(false);
		resetActionPanel();
		actionLabel.setText("Falsche Taste! Fehlgeschlagen!!!");
	}
	public static int getOption(){
		return option;
	}
	private static void setSmash(char c){
		chr=c;
	}
	private static void setSequence(char c[]){
		arr=c;
	}
	public static char getSmash(){
		return chr;
	}
	public static char[] getSequence(){
		return arr;
	}
	public static void resetActionPanel(){
		resetBigActionLabel();
		resetActionLabel();
		resetProgressBar();
		resetCount();
		resetBeforTime();
		resetKeySequenceFail();
	}
	private static void resetActionLabel(){
		actionLabel.setText("");
	}
	private static void resetBigActionLabel(){
		bigActionLabel.setText("");
	}
	private static void resetBeforTime(){
		beforTime=0;
	}
	public static int getCount(){
		return count;
	}
	public static void incCount(){
		count++;
	}
	private static void resetCount(){
		tmpCount=0;
		count=0;
	}
	public static boolean getKeySequenceFail(){
		return keySequenceFail;
	}
	public static void setKeySequenceFail(){
		keySequenceFail=true;
	}
	private static void resetKeySequenceFail(){
		keySequenceFail=false;
	}
	public static void setTextforA(){
		actionLabel.setText("Taste A druecken");
	}
	public static void setTextforManipulate(){
		actionLabel.setText("Bereits Manipuliert");
	}
	private static void setProgressBar(int value){
		bar.setValue(value);
		bar.setVisible(true);
	}
	public static void resetProgressBar(){
		bar.setValue(0);
		bar.setVisible(false);
	}
	public static void setProgressBarTimer(int value){
		bar.setValue(bar.getValue()+value);
	}
}
