package com.example.datecalc;

import javax.microedition.lcdui.*;

import android.text.TextUtils;

public class calcForm extends Form implements CommandListener {
	private dateCalc dateCalc;
	private Display dpy;
	private Displayable prev;
	private globalCalendar now;
	//private errorForm er;

	private Command cmdMenu, cmdGo;
	private TextField day1, day2, month1, month2, year1, year2;
	private StringItem dateCalcStr, otherStr;
	private int d1, m1, y1, d2, m2, y2;
	private boolean correct = true;

	public calcForm(dateCalc dateCalc, String hdr, int d1, int m1, int y1,
			int d2, int m2, int y2) {
		super(hdr);
		this.dateCalc = dateCalc;
		dpy = MainActivity.ma.display;
		prev = dpy.getCurrent();
		this.d1 = d1;
		this.m1 = m1;
		this.y1 = y1;
		this.d2 = d2;
		this.m2 = m2;
		this.y2 = y2;
		cmdMenu = new Command("Назад", Command.BACK, 0);
		cmdGo = new Command("Ввод", Command.ITEM, 1);
		day1 = new TextField("День 1", d1 + "", 2, TextField.NUMERIC);
		month1 = new TextField("Месяц 1", m1 + 1 + "", 2, TextField.NUMERIC);
		year1 = new TextField("Год 1", y1 + "", 4, TextField.NUMERIC);
		day2 = new TextField("День 2", d2 + "", 2, TextField.NUMERIC);
		month2 = new TextField("Месяц 2", m2 + 1 + "", 2, TextField.NUMERIC);
		year2 = new TextField("Год 2", y2 + "", 4, TextField.NUMERIC);
		now = new globalCalendar();
		dateCalcStr = new StringItem("Количество дней:", ""
				+ now.kolDays(d1, m1, y1, d2, m2, y2));
		otherStr = new StringItem("Разница дат:", now.kolTimes(d1, m1, y1, d2,
				m2, y2));
		//append("\n");
		this.append(day1);
		this.append(month1);
		this.append(year1);
		this.append(day2);
		this.append(month2);
		this.append(year2);
		this.append(dateCalcStr);
		this.append(otherStr);
		this.addCommand(cmdMenu);
		// addCommand(cmdGo);
				setCommandListener(this);
				//new Thread(this).start();
			}


	public void starter() {
		new Thread(new Runnable(){
			public void run() {
				while (flag) {

					try {
						if (dpy.getCurrent() != calcForm.this){
							flag = false;
							return;
							}
						Thread.sleep(100);						
						commandAction(cmdGo, calcForm.this);

					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			}
		}).start();
	}

	boolean flag = true;

	public void commandAction(Command c, Displayable d) {
		
		if (c == cmdMenu) {
			flag =false;
			dpy.setCurrent(prev);
			 System.out.println("skip to prev screen");
		} else if (c == cmdGo) {
			
			UIGlobal.APP_HANDLER.post(new Runnable() { public void run(){
				try{
			
			if (now.correctDate(Integer.parseInt(day1.getString().trim()), Integer.parseInt(month1.getString().trim()) - 1, Integer.parseInt(year1.getString().trim())) == false) {
				correct = false;				
				new Alert("Ошибка", "Дата " + now.getDMYDate(Integer.parseInt(day1.getString().trim()), Integer.parseInt(month1.getString().trim()) - 1, Integer.parseInt(year1.getString().trim())) + " некорректна!", null, AlertType.ERROR).ShowAlert();
				day1.setString(String.valueOf(d1));
				month1.setString(String.valueOf(m1+1));
				year1.setString(String.valueOf(y1));
			} else {
				
				if (now.correctDate(Integer.parseInt(day2.getString().trim()), Integer.parseInt(month2.getString().trim()) - 1, Integer.parseInt(year2.getString().trim())) == false) {
					correct = false;
					
					new Alert("Ошибка", "Дата " + now.getDMYDate(Integer.parseInt(day2.getString().trim()), Integer.parseInt(month2.getString().trim()) - 1, Integer.parseInt(year2.getString().trim()))
							+ " некорректна!", null, AlertType.ERROR).ShowAlert();

					day2.setString(String.valueOf(d2));
					month2.setString(String.valueOf(m2+1));
					year2.setString(String.valueOf(y2));
				} else {
					
					correct = true;
					d1 = Integer.parseInt(day1.getString().trim());
					m1 = Integer.parseInt(month1.getString().trim()) - 1;
					y1 = Integer.parseInt(year1.getString().trim());
					
					d2 = Integer.parseInt(day2.getString().trim());
					m2 = Integer.parseInt(month2.getString().trim()) - 1;
					y2 = Integer.parseInt(year2.getString().trim());
					
					dateCalcStr.setText(""
							+ now.kolDays(d1, m1, y1, d2, m2, y2));
					otherStr.setText(now.kolTimes(d1, m1, y1, d2, m2, y2));
					dateCalc.days[0][0] = d1;
					dateCalc.days[0][1] = m1;
					dateCalc.days[0][2] = y1;
					dateCalc.days[1][0] = d2;
					dateCalc.days[1][1] = m2;
					dateCalc.days[1][2] = y2;
				}
			}
			
				} catch (Exception ex){
					ex.printStackTrace();
				}
			}});
				
		}
	}

}