package com.nextwavesoft;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;

import com.nextwavesoft.enumeration.CircularBackgroundType;
import com.nextwavesoft.enumeration.DigitalCharacterOverflowAlignment;
import com.nextwavesoft.enumeration.DigitalCharacterScrollingState;
import com.nextwavesoft.enumeration.DigitalCharacterType;
import com.nextwavesoft.enumeration.LedState;
import com.nextwavesoft.enumeration.PointerCapType;
import com.nextwavesoft.enumeration.RectangularFrameType;
import com.nextwavesoft.enumeration.ScalePlacement;
import com.nextwavesoft.enumeration.TextOrientation;
import com.nextwavesoft.enumeration.UnitType;
import com.nextwavesoft.gauge.*;
import com.nextwavesoft.primitives.GaugeItem;
import com.nextwavesoft.shared.Unit;
import javax.swing.Timer;


public class CircularTachometer  implements ActionListener{

	static Timer timer;
	CircularPointerNeedle mainneedle = new CircularPointerNeedle();
	CircularPointerNeedle subneedle = new CircularPointerNeedle();
	int z = 0;
	
	public void actionPerformed(ActionEvent e) {
		if ( z == 0)
		{
			mainneedle.setValue(7);
			subneedle.setValue(100);
			z++;
		}
		else
		{
			mainneedle.setValue(1);
			subneedle.setValue(0);
			z = 0;
		}
		//  ts1.setText(String.valueOf(z));
	}
	
	public static void main(String[] argv)
	{
		CircularTachometer m = new CircularTachometer();
		m.createAndShowGUI();
	}
/*
 *        
	/**
	 * Creates the and show gui.
	 */
	private  void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame("IconDisplayer");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Create circular gauge which holds circular scale.
		CircularGauge circularGauge = new CircularGauge();
		circularGauge.setBackground(new Color(44,44,44));
		circularGauge.setRimBrush(new Color(44,44,44));
		circularGauge.setBackgroundType(CircularBackgroundType.CircularTopGradient);
		circularGauge.setBackgroundRadiusRatio(0.97);
		
		
		JLabel label1 = new JLabel("Tachometer");
		label1.setForeground(Color.white);
		label1.setFont(new Font("Verdana",Font.BOLD,12));
		GaugeItem labelItem = new GaugeItem(label1, 
				new Unit(0,UnitType.Percentage), 
				new Unit(-20,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		JLabel label2 = new JLabel("x1000");
		label2.setForeground(Color.white);
		label2.setFont(new Font("Verdana",Font.PLAIN,10));
		GaugeItem labelItem1 = new GaugeItem(label2, 
				new Unit(0,UnitType.Percentage), 
				new Unit(20,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		JLabel label3 = new JLabel("RPM");
		label3.setForeground(Color.white);
		label3.setFont(new Font("Verdana",Font.PLAIN,10));
		GaugeItem labelItem3 = new GaugeItem(label3, 
				new Unit(30,UnitType.Percentage), 
				new Unit(58,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		DigitalGauge digitalGauge = new DigitalGauge();
		digitalGauge.setCharacterType(DigitalCharacterType.DotMatrix5By7Ellipse);
		digitalGauge.setText("Tachometer");
		digitalGauge.setForeground(Color.WHITE);
		digitalGauge.setCharacterSpacing(8);
		digitalGauge.setCharacterCount(10);
		digitalGauge.setCharacterPadding(10);
		digitalGauge.setCharacterOverflowAlignment(
			DigitalCharacterOverflowAlignment.Left);
		digitalGauge.setScrollState(DigitalCharacterScrollingState.None);
		digitalGauge.setItalic(false);
		digitalGauge.setLedState(LedState.Blinking);
		digitalGauge.setCharacterWidthRatio(0.5);

		GaugeItem labelItem4 = new GaugeItem(digitalGauge, 
				new Unit(0,UnitType.Percentage), 
				new Unit(-40,UnitType.Percentage), 
				new Unit(70,UnitType.Percentage), 
				new Unit(30,UnitType.Percentage));
		
		circularGauge.addGaugeItem(labelItem);
		circularGauge.addGaugeItem(labelItem1);
		circularGauge.addGaugeItem(labelItem3);
		circularGauge.addGaugeItem(labelItem4);
		
		//Create circular scale which holds circular tickset.
		CircularScale circularScale = new CircularScale();
		circularScale.setRadius(new Unit(74,UnitType.Percentage));
		circularScale.setBarExtent(new Unit(2,UnitType.Percentage));
		circularScale.setStartAngle(135);
		circularScale.setSweepAngle(270);
		circularScale.setBarVisible(false);
	
		
		CircularScale circularScaleTemp = new CircularScale();
		circularScaleTemp.setRadius(new Unit(50,UnitType.Percentage));
		circularScaleTemp.setBarExtent(new Unit(2,UnitType.Percentage));
		circularScaleTemp.setStartAngle(50);
		circularScaleTemp.setSweepAngle(80);
		circularScaleTemp.setBackground(Color.GRAY);
		
		
		circularGauge.getCircularScaleCollection().add(circularScale);
		circularGauge.getCircularScaleCollection().add(circularScaleTemp);
		
		CircularTickSet circularTickSet = new CircularTickSet();
		circularTickSet.setMinimum(0);
		circularTickSet.setMaximum(9);
		circularTickSet.setMinorInterval(0.1);
		circularTickSet.setMajorInterval(0.5);

	    //create tick mark minor
		CircularTickMarkMinor tickMarkMinor = new CircularTickMarkMinor();
		tickMarkMinor.setTickMarkAscent(new Unit(0.5,UnitType.Percentage));
		tickMarkMinor.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMinor.setBackground(Color.WHITE);
		tickMarkMinor.setBorderWidth(0);
		tickMarkMinor.setScalePlacement(ScalePlacement.Inside);
		tickMarkMinor.setEndValue(7);

		//create tick mark major 
		CircularTickMarkMajor tickMarkMajor = new CircularTickMarkMajor();
		tickMarkMajor.setTickMarkAscent(new Unit(1,UnitType.Percentage));
		tickMarkMajor.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		tickMarkMajor.setBackground(Color.WHITE);
		tickMarkMajor.setBorderWidth(0);
		tickMarkMajor.setScalePlacement(ScalePlacement.Inside);
		tickMarkMajor.setEndValue(6.5);
		
		CircularTickLabelMajor tickLabelMajor = new CircularTickLabelMajor();
		//@TODO Label Position
		tickLabelMajor.setFontFamily("Verdana");
		tickLabelMajor.setFontSize(new Unit(12,UnitType.Percentage));
		//@TODO Not Working
		tickLabelMajor.setForeground(Color.WHITE);
		tickLabelMajor.setOrientation(TextOrientation.Rotated);
		tickLabelMajor.setScalePlacement(ScalePlacement.Inside);		
		tickLabelMajor.setScaleOffset(new Unit(10,UnitType.Percentage));
		double [] skipDoubles = new double[]{0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5};
		ArrayList<Double> skipValues = new ArrayList<Double>();
		for(double skipDouble : skipDoubles )
			skipValues.add(skipDouble);
		tickLabelMajor.setSkipValues(skipValues);
		
		
	    //create tick mark minor
		CircularTickMarkMinor tickMarkMinor2 = new CircularTickMarkMinor();
		tickMarkMinor2.setTickMarkAscent(new Unit(0.5,UnitType.Percentage));
		tickMarkMinor2.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMinor2.setBackground(Color.RED);
		tickMarkMinor2.setBorderWidth(0);
		tickMarkMinor2.setScalePlacement(ScalePlacement.Inside);
		tickMarkMinor2.setStartValue(7);
		tickMarkMinor2.setEndValue(9);

		//create tick mark major 
		CircularTickMarkMajor tickMarkMajor2 = new CircularTickMarkMajor();
		tickMarkMajor2.setTickMarkAscent(new Unit(1,UnitType.Percentage));
		tickMarkMajor2.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		tickMarkMajor2.setBackground(Color.RED);
		tickMarkMajor2.setBorderWidth(0);
		tickMarkMajor2.setScalePlacement(ScalePlacement.Inside);
		tickMarkMajor2.setStartValue(7);
		tickMarkMajor2.setEndValue(9);
		
		//create tick mark major 
		CircularTickMarkMajor tickMarkMajor3 = new CircularTickMarkMajor();
		tickMarkMajor3.setTickMarkAscent(new Unit(4,UnitType.Percentage));
		tickMarkMajor3.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		tickMarkMajor3.setBackground(Color.GRAY);
		tickMarkMajor3.setBorderWidth(0);
		tickMarkMajor3.setScalePlacement(ScalePlacement.Outside);
		tickMarkMajor3.setMajorInterval(1);
		//tickMarkMajor3.setStartValue(0);
		//tickMarkMajor3.setEndValue(9);
		
		CircularTickMarkMajor tickMarkMajor4 = new CircularTickMarkMajor();
		tickMarkMajor4.setTickMarkAscent(new Unit(4,UnitType.Percentage));
		tickMarkMajor4.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		tickMarkMajor4.setBackground(Color.GRAY);
		tickMarkMajor4.setBorderWidth(0);
		tickMarkMajor4.setScalePlacement(ScalePlacement.Outside);
		tickMarkMajor4.setMajorInterval(0.5);
		double [] skipDoubles4 = new double[]{0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5};
		ArrayList<Double> skipValues4 = new ArrayList<Double>();
		for(double skipDouble : skipDoubles4 )
			skipValues4.add(skipDouble);
		tickMarkMajor4.setSkipValues(skipValues4);
		
		CircularTickMarkMajor tickMarkMajor5 = new CircularTickMarkMajor();
		tickMarkMajor5.setTickMarkAscent(new Unit(2,UnitType.Percentage));
		tickMarkMajor5.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		tickMarkMajor5.setBackground(Color.GRAY);
		tickMarkMajor5.setBorderWidth(0);
		tickMarkMajor5.setScalePlacement(ScalePlacement.Outside);
		tickMarkMajor5.setMajorInterval(0.5);
		double [] skipDoubles5 = new double[]{0,1,2,3,4,5,6,7,8,9};
		ArrayList<Double> skipValues5 = new ArrayList<Double>();
		for(double skipDouble : skipDoubles5 )
			skipValues5.add(skipDouble);
		tickMarkMajor5.setSkipValues(skipValues5);
		
		
		CircularTickMarkCustom custom1 = new CircularTickMarkCustom();
		custom1.setTickMarkAscent(new Unit(2,UnitType.Percentage));
		custom1.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		custom1.setBackground(Color.RED);
		custom1.setScalePlacement(ScalePlacement.Inside);
		custom1.setValue(0);
		
		CircularTickMarkCustom custom2 = new CircularTickMarkCustom();
		custom2.setTickMarkAscent(new Unit(2,UnitType.Percentage));
		custom2.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		custom2.setBackground(Color.RED);
		custom2.setScalePlacement(ScalePlacement.Inside);
		custom2.setValue(9);
		
		mainneedle.setPointerAscent(new Unit(3,UnitType.Percentage));
		mainneedle.setPointerExtent(new Unit(105,UnitType.Percentage));
		
		CircularPointerCap maincap = new CircularPointerCap();
		maincap.setBackground(Color.RED);
		maincap.setPointerExtent(new Unit(25,UnitType.Percentage));
		maincap.setCapType(PointerCapType.CircleConvex);
	
		circularTickSet.getCircularPointerCollection().add(maincap);
		circularTickSet.getCircularPointerCollection().add(mainneedle);
		circularTickSet.getCircularTickCollection().add(custom1);
		circularTickSet.getCircularTickCollection().add(custom2);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor);
		circularTickSet.getCircularTickCollection().add(tickMarkMinor);
		circularTickSet.getCircularTickCollection().add(tickLabelMajor);
		circularTickSet.getCircularTickCollection().add(tickMarkMinor2);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor2);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor3);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor4);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor5);
		
		
		circularScale.getCircularTickSetCollection().add(circularTickSet);
		
		
		//set up the temperature meter
		
		CircularTickSet circularTickSettemp = new CircularTickSet();
		circularTickSettemp.setMinimum(0);
		circularTickSettemp.setMaximum(100);
		circularTickSettemp.setMinorInterval(5);
		circularTickSettemp.setMajorInterval(100);
		
		//create tick mark minor
		CircularTickMarkMinor tickMarkMinorTemp = new CircularTickMarkMinor();
		tickMarkMinorTemp.setTickMarkAscent(new Unit(0.5,UnitType.Percentage));
		tickMarkMinorTemp.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMinorTemp.setBackground(Color.WHITE);
		tickMarkMinorTemp.setScalePlacement(ScalePlacement.Inside);

		CircularTickMarkCustom customTickMarkWhite = new CircularTickMarkCustom();
		customTickMarkWhite.setTickMarkAscent(new Unit(1,UnitType.Percentage));
		customTickMarkWhite.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		customTickMarkWhite.setBackground(Color.WHITE);
		customTickMarkWhite.setScalePlacement(ScalePlacement.Inside);
		customTickMarkWhite.setValue(0);
		
		CircularTickMarkCustom customTickMarkRed = new CircularTickMarkCustom();
		customTickMarkRed.setTickMarkAscent(new Unit(1,UnitType.Percentage));
		customTickMarkRed.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		customTickMarkRed.setBackground(Color.RED);
		customTickMarkRed.setScalePlacement(ScalePlacement.Inside);
		customTickMarkRed.setValue(100);
		
		CircularTickLabelCustom labelCold = new CircularTickLabelCustom();
		labelCold.setScaleOffset(new Unit(5,UnitType.Percentage));
		labelCold.setForeground(Color.WHITE);
		labelCold.setScalePlacement(ScalePlacement.Inside);
		labelCold.setOrientation(TextOrientation.Rotated);
		labelCold.setFontSize(new Unit(14,UnitType.Percentage));
		labelCold.setText("C");
		labelCold.setValue(0);
		
		CircularTickLabelCustom labelHot = new CircularTickLabelCustom();
		labelHot.setScaleOffset(new Unit(5,UnitType.Percentage));
		labelHot.setForeground(Color.WHITE);
		labelHot.setScalePlacement(ScalePlacement.Inside);
		labelHot.setOrientation(TextOrientation.Rotated);
		labelHot.setFontSize(new Unit(14,UnitType.Percentage));
		labelHot.setText("H");
		labelHot.setValue(100);
		
		subneedle.setPointerAscent(new Unit(3,UnitType.Percentage));
		subneedle.setPointerExtent(new Unit(70,UnitType.Percentage));
		
		CircularPointerCap capTemp = new CircularPointerCap();
		capTemp.setPointerExtent(new Unit(10,UnitType.Percentage));
		capTemp.setCapType(PointerCapType.CircleConvex);
		
		circularTickSettemp.getCircularPointerCollection().add(capTemp);
		circularTickSettemp.getCircularPointerCollection().add(subneedle);
		circularTickSettemp.getCircularTickCollection().add(tickMarkMinorTemp);
		circularTickSettemp.getCircularTickCollection().add(customTickMarkWhite);
		circularTickSettemp.getCircularTickCollection().add(customTickMarkRed);
		circularTickSettemp.getCircularTickCollection().add(labelCold);
		circularTickSettemp.getCircularTickCollection().add(labelHot);
		
		circularScaleTemp.getCircularTickSetCollection().add(circularTickSettemp);
		circularScaleTemp.setYDistanceFromCenter(new Unit(15,UnitType.Percentage));
		timer = new Timer(2000, this);
		// timer.setInitialDelay(pause);
		timer.start();
		
		GridLayout gl = new GridLayout(1,1);
		frame.setLayout(gl);
		frame.add(circularGauge);
		frame.setSize(new Dimension(150, 150));

		//Display the window.
		frame.pack();
		frame.setSize(new Dimension(400, 400));
		frame.setVisible(true);

	}


}
