package cn.hmsoft.ltc.controller;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hmsoft.ltc.common.constants.LtContext;
import cn.hmsoft.ltc.common.util.DialogUtil;
import cn.hmsoft.ltc.model.Course;
import cn.hmsoft.ltc.model.LoginRequest;
import cn.hmsoft.ltc.service.iface.CourseService;
import cn.hmsoft.ltc.service.iface.UserService;
import cn.hmsoft.ltc.service.impl.CourseServiceImpl;
import cn.hmsoft.ltc.service.impl.UserServiceImpl;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;


/**
 * 登录主界面.
 * @author zq
 *
 */
@FXMLController
public class LoginController implements Initializable {
	
	@FXML 
	private Label longNameLabel;

	@FXML 
	private GridPane loginGrid;
	
	@FXML
	private Label userNameLabel;
	
	@FXML
	private Label userPassLabel;
	@FXML
	private Label forgetThePasswordLabel;//忘记密码
	
	@FXML
	private TextField userNameTxf;
	
	@FXML
	private PasswordField userPassTxf;

	@FXML
	private Button submitBtn;
	
	@FXML
	private Button cancelBtn;
	
	@FXML
	private Button registerBtn; 
	
	@FXML
	private Text scenetitle = null;
	@Autowired
	private UserService userService = new UserServiceImpl();
	
	public CourseService courseService = new CourseServiceImpl();  //课程
	
  	//课程管理列表--数据源
  	public final ObservableList<Course> COURSE_DATALIST = FXCollections.observableArrayList(); 

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

		this.userPassTxf.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent event) {
				if (event.getCode() == KeyCode.ENTER) {
					loginLogic(event);
				}
			}
			
		});
	}
	
	/**
	 * 用户登入
	 * @param event
	 * @throws SQLException 
	 */
	public void doLoginFunc(ActionEvent event) throws SQLException {
		loginLogic(event);
	}
	
	//登录逻辑
	private void loginLogic(Event event) {
		//提示框
		try {
			String userName = userNameTxf.getText().trim();
			userNameTxf.setText(userName);
			String passWord = userPassTxf.getText().trim();
			userPassTxf.setText(passWord);
			if (userName.length() == 0 || passWord.length() == 0) {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "用户名或者密码不能为空!");
				return;
			}
			//校验用户名、密码
			LoginRequest request = userService.checkUserName(userName, passWord);
			if (request.getStatus() == 0) {// 登录成功
				LtContext.LOGIN_SCHOOL = request.getSchoolName();
				LtContext.LOGIN_USER_ID = request.getUserId();
				LtContext.COURSE_ID=request.getCourseId();
				LtContext.LOGIN_USER_NAME = userName;
				LtContext.CURR_LOGIN_USER = request;
				if (request.getFirstLogin() == 0) {
					userService.updateUserFirstLogin(request.getUserId());
					// 跳转到保密责任书
					FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/ProtocolView.fxml"));
					Parent root = fxmlLoader.load();
					Stage stage = new Stage();
					stage.setTitle("保密责任书");
					stage.setScene(new Scene(root));
					stage.setFullScreen(false);
					stage.setMaxWidth(560);
					stage.setHeight(730);
					stage.show();
					// 关闭当前窗体
					((Node) (event.getSource())).getScene().getWindow().hide();
				} else {
					FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/MainPaneView.fxml"));
					Parent root = fxmlLoader.load();
					Rectangle2D screen = Screen.getPrimary().getVisualBounds();
					Stage stage = new Stage();
					stage.setTitle("教师命题客户端1.0   登入用户名："+ LtContext.LOGIN_USER_NAME);
					stage.setScene(new Scene(root));
					stage.setX(screen.getMinX());
					stage.setY(screen.getMinY());
					stage.setWidth(screen.getWidth());
					stage.setHeight(screen.getHeight());
					stage.show();
					this.setMainSize(fxmlLoader, screen);
					//关闭当前窗体
					((Node) (event.getSource())).getScene().getWindow().hide();
				}
			} else {
				DialogUtil.showAlertDialog(GUIState.getStage(), AlertType.WARNING, "提示", "登入失败,用户名或密码错误!");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void setMainSize(FXMLLoader fxmlLoader, Rectangle2D screen) {
		MainPaneController controller = fxmlLoader.getController();
		controller.initGui(screen.getWidth(), screen.getHeight());
	}
	
	/**
	 * 用户注册
	 * @param event
	 */
	public void doRegisterFunc(ActionEvent event) {
		try {
			//跳转到注册页面
			FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/AppRegisterView.fxml"));
			Parent root = fxmlLoader.load();
			Stage stage = new Stage();
			stage.setTitle("注册");
			stage.setScene(new Scene(root,592,596));
			stage.show();
			//关闭当前窗体
			((Node) (event.getSource())).getScene().getWindow().hide();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户取消
	 * @param event
	 */
	public void doCancelFunc(ActionEvent event) {
		//关闭页面所有窗口
		Platform.exit();
	}

}
