/*
 * Auto-CreamAPI
 * Copyright (C) 2020 Jeddunk
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

package xyz.jeddunk.autocreamapi;import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.jeddunk.autocreamapi.pojo.App;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import static java.text.MessageFormat.format;

public class SearchResultWindowController implements Initializable {

    final Logger logger = LoggerFactory.getLogger(SearchResultWindowController.class);

    @FXML
    public Button okButton;
    @FXML
    public Button cancelButton;
    @FXML
    public TreeTableView<App> gameTable;
    @FXML
    public TreeTableColumn<App, Integer> appIdCol;
    @FXML
    public TreeTableColumn<App, String> nameCol;

    public Controller parent;

    private long lastTime;
    private boolean isDblClicked;

    public SearchResultWindowController() {
    }

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

    public void initMe(List<App> games) {
        TreeItem<App> root = new TreeItem<>();
        appIdCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("appId"));
        nameCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
        games.forEach(game -> root.getChildren().add(new TreeItem<>(game)));
        gameTable.setRoot(root);
    }

    public void confirm() {
        App app = gameTable.getSelectionModel().getSelectedItem().getValue();
        logger.info(format("Game selected: {0} ({1})!", app.getName(), app.getAppId()));
        parent.game_name_textfield.setText(app.getName());
        parent.appId_textfield.setText(String.valueOf(app.getAppId()));
        Stage current = (Stage) okButton.getScene().getWindow();
        current.close();
    }

    public void cancel() {
        logger.info("Closing window without setting game!");
        /*parent.game_name_textfield.setText("");
        parent.appId_textfield.setText("-1");*/
        Stage current = (Stage) cancelButton.getScene().getWindow();
        current.close();
    }

    public void setParentController(Controller controller) {
        parent = controller;
    }

    public void doubleclickConfirm(MouseEvent mouseEvent) {
        if (mouseEvent.getButton() == MouseButton.PRIMARY) {
            long diff;
            long currentTime = System.currentTimeMillis();
            if (lastTime != 0 && currentTime != 0) {
                diff = currentTime - lastTime;
                isDblClicked = (diff <= 215);
            }
            lastTime = currentTime;
            if (isDblClicked) {
                /*App app = gameTable.getSelectionModel().getSelectedItem().getValue();
                parent.game_name_textfield.setText(app.getName());
                parent.appId_textfield.setText(String.valueOf(app.getAppId()));
                Stage current = (Stage) gameTable.getScene().getWindow();
                current.close();*/
                confirm();
            }
        }
    }
}
