package com.qst.dms.ui;

import com.qst.dms.entity.*;
import com.qst.dms.service.LogRecService;
import com.qst.dms.service.TransportService;
import com.qst.dms.service.UserService;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.util.ArrayList;

public class MainFrame extends Application
{

    public static void main(String[] args){
        Application.launch(args);
    }
    public void startapp()
    {
        try
        {
            this.start(new Stage());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    private Stage stage;
    private Scene infoInputScene,tableScene,updateTimeSetScene;
    InfoInputFXML infoInputFXMLController;
    TableFXML tableFXMLController;
    UpdateTimeSetFXML updateTimeSetFXMLController;
    @Override
    public void start(Stage _stage) throws Exception
    {
        stage = _stage;
        FXMLLoader infoInputFrameFXMLLoader = new FXMLLoader();
        FXMLLoader tableFrameFXMLLoader = new FXMLLoader();
        FXMLLoader updateTimeSetFXMLLoader = new FXMLLoader();
        infoInputFrameFXMLLoader.setLocation(getClass().getClassLoader().getResource("resources/InfoInputFrame.fxml"));
        tableFrameFXMLLoader.setLocation(getClass().getClassLoader().getResource("resources/TableFrame.fxml"));
        updateTimeSetFXMLLoader.setLocation(getClass().getClassLoader().getResource("resources/timeSetDialog.fxml"));
        //set Scene
        infoInputScene = new Scene(infoInputFrameFXMLLoader.load());
        tableScene = new Scene(tableFrameFXMLLoader.load());
        updateTimeSetScene = new Scene(updateTimeSetFXMLLoader.load());
        //set Controller
        infoInputFXMLController = infoInputFrameFXMLLoader.getController();
        tableFXMLController = tableFrameFXMLLoader.getController();
        updateTimeSetFXMLController = updateTimeSetFXMLLoader.getController();
        //set Delegation
        infoInputFXMLController.getDelegation(this);
        tableFXMLController.getDelegation(this,infoInputFXMLController);
        updateTimeSetFXMLController.getDelegation(infoInputFXMLController,tableFXMLController);
        //set Stage
        stage.setScene(infoInputScene);
        stage.setResizable(false);
        stage.show();
    }
    public void toInfoInputFrame() {
        stage.setScene(infoInputScene);
        infoInputFXMLController.fadeIn(null);
    }

    public void toTableFrame() {
        tableFXMLController.setTable();
        stage.setScene(tableScene);
        tableFXMLController.fadeIn(null);
    }

    public void updateFlushTime() {
        Stage subWindow = new Stage();
        subWindow.setScene(updateTimeSetScene);
        subWindow.setOnCloseRequest((event)->{
            event.consume();
            updateTimeSetFXMLController.closeRequest();
            subWindow.close();
        });
        subWindow.show();
    }
}