package com.hmsoft.bizpanel;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import com.hmsoft.common.FileConst;
import com.hmsoft.common.util.DialogUtil;
import com.hmsoft.common.util.RegexpUtils;
import com.hmsoft.common.util.SysCfgUtil;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * 
 * @author zq
 *
 */
public class SystTab extends Tab {

	public static volatile Integer FRSH_MINUTES = 0;//默认为0-自动，>0-定时手动刷新,单位为分组
	
	public SystTab(final Stage stage) {
		super();
		this.setText("系统配置");
		this.setId("systTabId");
		this.setClosable(false);
		//Grid容器
		GridPane gp = new GridPane();
		gp.setPadding(new Insets(20));
		gp.setHgap(5);
		gp.setVgap(5);
		
		try {
			this.createTimeRadio(stage, gp);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//grid布局加到容器
		this.setContent(gp);
	}

	private void createTimeRadio(final Stage stage, final GridPane gp) throws FileNotFoundException, IOException {
		final Button btnSave = new Button("保存");
		final ToggleGroup group = new ToggleGroup();
		
		final RadioButton auto = new RadioButton("自动刷新");
		final RadioButton shedule = new RadioButton("定时刷新");
		final RadioButton manual = new RadioButton("手动刷新");
		
		Text sheduleStartText = new Text(" 每隔");
		Text sheduleEndText = new Text("分钟刷新一次");
		final TextField minutesField = new TextField();
		minutesField.setMinWidth(60);
		auto.setToggleGroup(group);
		shedule.setToggleGroup(group);
		manual.setToggleGroup(group);
		
		Properties props = SysCfgUtil.loadFromFile(FileConst.SYS_CFG_PATH);
		Object obj = props.get(FileConst.FRSH_MINUTES_KEY);
		if (obj == null || Integer.parseInt(obj.toString()) == 0) {
			auto.setSelected(true);
		} 
		if (obj != null && Integer.parseInt(obj.toString()) > 0) {
			shedule.setSelected(true);
			minutesField.setText(obj.toString());
			FRSH_MINUTES = Integer.parseInt(obj.toString());
		}
		if (obj != null && Integer.parseInt(obj.toString()) == -1) {
			manual.setSelected(true);
			FRSH_MINUTES = -1;
			//
			minutesField.setDisable(true);
			minutesField.setText("");
		}
		gp.add(auto, 0, 0);
		gp.add(shedule, 0, 1);
		gp.add(sheduleStartText, 1, 1);
		gp.add(minutesField, 2, 1);
		gp.add(sheduleEndText, 3, 1);
		gp.add(manual, 0, 2);
		gp.add(btnSave, 0, 5);//上面空一行
		//group
		group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

			@Override
			public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
				RadioButton newRB = (RadioButton)newValue;
				if (newRB.getText().equals("自动刷新")) {
					minutesField.setText("");
					minutesField.setDisable(true);
				} else if (newRB.getText().equals("定时刷新")) {
					minutesField.setDisable(false);
				} else if (newRB.getText().equals("手动刷新")) {
					minutesField.setText("");
					minutesField.setDisable(true);
				}
			}
			
		});
		btnSave.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				if (auto.isSelected()) {//自动
					FRSH_MINUTES = 0;
					MtzjTab.BTN_MANUAL.setDisable(true);
				} else if (shedule.isSelected()) {//定时刷新
					if (RegexpUtils.checkIsPositiveIntGtZero(minutesField.getText())) {
						FRSH_MINUTES = Integer.parseInt(minutesField.getText());
						MtzjTab.BTN_MANUAL.setDisable(true);
					} else {
						DialogUtil.showAlertDialog(stage, Alert.AlertType.WARNING, "提示", "请输入非0正整数!");
						return;
					}
				} else if (manual.isSelected()) {//手动刷新
					FRSH_MINUTES = -1;
					MtzjTab.BTN_MANUAL.setDisable(false);
				}
				try {
					final Properties pro = new Properties();
					pro.setProperty(FileConst.FRSH_MINUTES_KEY, String.valueOf(FRSH_MINUTES));
					SysCfgUtil.storeToFile(pro, FileConst.SYS_CFG_PATH);
					DialogUtil.showAlertDialog(stage, Alert.AlertType.INFORMATION, "成功", "设置成功.");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
