package cn.hmsoft.ltc.controller;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;

import cn.hmsoft.ltc.common.util.DialogUtil;
import cn.hmsoft.ltc.common.util.StageManager;
import cn.hmsoft.ltc.model.SubjectOption;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

/**
 * 题目选项管理
 * 
 * @author xuwenjin
 * @date 2019-03-28
 */
@FXMLController
public class SubjectOptionController implements Initializable {

	@FXML
	public ComboBox<String> subjectOptionNoCombo;

	@FXML
	private TextArea subjectOptionContentTextArea;

	@Override
	public void initialize(URL location, ResourceBundle resources) {

		// 初始化下拉列表数据
		try {
			initData();

			initEditData();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initData() throws SQLException {
		subjectOptionNoCombo.setPromptText("选择选项");
		subjectOptionNoCombo.setEditable(false);
		subjectOptionNoCombo.setItems(FXCollections.observableArrayList("A", "B", "C", "D", "E", "F"));
	}

	/**
	 * 初始化编辑数据
	 * 
	 * @throws SQLException
	 */
	private void initEditData() throws SQLException {
		SubjectOption editSubjectOption = (SubjectOption) StageManager.VARIABLE.get("select_subject_option");
		// 是否为编辑
		if (editSubjectOption != null) {
			subjectOptionNoCombo.setValue(editSubjectOption.getSubjectOptionNo());
			subjectOptionContentTextArea.setText(editSubjectOption.getSubjectOptionContent());
		}
	}

	/**
	 * 保存题目
	 */
	public void doSaveOptionFunc(ActionEvent event) {

		String subjectOptionNo = subjectOptionNoCombo.getValue();
		//String subjectContent = subjectOptionContentTextArea.getText().trim();
		//subjectOptionContentTextArea.setText(subjectContent);
		if (StringUtils.isBlank(subjectOptionNo)) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "请选择选项!");
			return;
		}
		/*if (StringUtils.isBlank(subjectContent)) {
			DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.INFORMATION, "提示", "请填写选项内容!");
			return;
		}*/

		SubjectOption subOption = new SubjectOption();
		subOption.setSubjectOptionNo(subjectOptionNo);
		//subOption.setSubjectOptionContent(subjectContent);

		SubjectOption editSubjectOption = (SubjectOption) StageManager.VARIABLE.get("select_subject_option");
		/*// 是否为编辑
		if (editSubjectOption != null) {
			int index = ((ChoiceSubjectController) StageManager.CONTROLLER.get("choiceSubjectController")).SUBJECT_OPTION_DATA_LIST
					.indexOf(editSubjectOption);
			//System.out.println(index);
			((ChoiceSubjectController) StageManager.CONTROLLER.get("choiceSubjectController")).SUBJECT_OPTION_DATA_LIST.set(index,
					subOption);
		} else {
			// 新增
			((ChoiceSubjectController) StageManager.CONTROLLER.get("choiceSubjectController")).SUBJECT_OPTION_DATA_LIST
					.add(subOption);
		}*/
		closeCurrentWindow(event);
	}

	/**
	 * 取消
	 */
	public void doCancelFunc(ActionEvent event) {
		closeCurrentWindow(event);
		StageManager.VARIABLE.remove("select_subject_option");
	}

	/**
	 * 新增题目选项 弹出框
	 */
	public void addSubjectOptionFunc(ActionEvent event) {
		try {
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/AddSubjectOptionView.fxml"));
			Parent root = fxmlLoader.load();
			Stage stage = new Stage();
			stage.setTitle("新增题目选项");
			stage.setScene(new Scene(root));
			stage.setAlwaysOnTop(true);
			stage.show();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void closeCurrentWindow(ActionEvent event) {
		// 关闭当前窗体
		((Node) (event.getSource())).getScene().getWindow().hide();
	}

}
