package cn.hmsoft.ltc.controller;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import cn.hmsoft.ltc.common.constants.LtContext;
import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.event.ActionEvent;
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.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *保密责任书页面 
 * @author Administrator
 *
 */
@FXMLController
public class ProtocolController implements Initializable {
	@FXML 
	private BorderPane proPane;
	@FXML 
	private GridPane proGridPane;
	@FXML 
	private Label protocolLab;
	@FXML
	private ImageView proImage;
	@FXML
	private Button proAgreeBtn;
	@FXML
	private Button proDisagreeBtn;
	
	public static Stage MAIN_STAGE = null;
	

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		Image image = new Image("image/bmzrs.jpg", 530, 640, false, false);
		proImage.setImage(image);
		//按钮开始倒计时
		proAgreeBtn.setDisable(true);
		proAgreeBtn.setText("同意(15)");
		//计时
		new Thread(new Runnable() {
			@Override
			public void run() {
				
				int tSecs = 15;
				while (tSecs > 0) {
					try {
						TimeUnit.SECONDS.sleep(1);
						tSecs--;
						final int showSeconds = tSecs;
						Platform.runLater(new Runnable() {
							@Override
							public void run() {
								proAgreeBtn.setText("同意(" + showSeconds + ")");
							}
						});
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Platform.runLater(new Runnable() {
					@Override
					public void run() {
						proAgreeBtn.setText("同意");
						proAgreeBtn.setDisable(false);
					}
				});
			}
		}).start();
	}

	/**
	  * 同意
	 * @param event
	 */
	public void doProAgree(ActionEvent event) {
		System.err.println("------------------->>>>>"+LtContext.LOGIN_SCHOOL);
		try {
			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();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void setMainSize(FXMLLoader fxmlLoader, Rectangle2D screen) {
		MainPaneController controller = fxmlLoader.getController();
		controller.initGui(screen.getWidth(), screen.getHeight());
	}
	
	/**
	 *  不同意
	 * @param event
	 */
	public void doProDisagree(ActionEvent event) {
		((Node) (event.getSource())).getScene().getWindow().hide();
	}
	

}
