package ctbrec.ui.sites.jasmin;

import java.io.IOException;

import ctbrec.Config;
import ctbrec.sites.Site;
import ctbrec.ui.DesktopIntegration;
import ctbrec.ui.PaginatedScheduledService;
import ctbrec.ui.ThumbOverviewTab;
import javafx.concurrent.WorkerStateEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class LiveJasminTab extends ThumbOverviewTab {
    protected Label status;
    protected Button acknowledge = new Button("That's alright");
    private Button createAccount = new Button("Create Account");
    private boolean betaAcknowledged = Config.getInstance().getSettings().livejasminBetaAcknowledged;

    public LiveJasminTab(String title, PaginatedScheduledService updateService, Site site) {
        super(title, updateService, site);
        if(!betaAcknowledged) {
            status = new Label("LiveJasmin is not fully functional. Live previews do not work.\n"
                    + "Also make sure, that you have an account and that you have entered your credentials.\n"
                    + "Otherwise you might get errors.");
            grid.getChildren().add(status);
            grid.getChildren().add(acknowledge);
            grid.getChildren().add(createAccount);
        } else {
            status = new Label("Logging in...");
            grid.getChildren().add(status);
        }

        acknowledge.setOnAction(e -> {
            betaAcknowledged = true;
            Config.getInstance().getSettings().livejasminBetaAcknowledged = true;
            try {
                Config.getInstance().save();
            } catch (IOException e1) {
            }
            status.setText("Logging in...");
            grid.getChildren().remove(acknowledge);
            grid.getChildren().remove(createAccount);
            if(updateService != null) {
                updateService.cancel();
                updateService.reset();
                updateService.restart();
            }
        });

        createAccount.setOnAction(e -> DesktopIntegration.open(site.getAffiliateLink()));
    }

    @Override
    protected void createGui() {
        super.createGui();
    }

    @Override
    protected void onSuccess() {
        if(Config.getInstance().getSettings().livejasminBetaAcknowledged) {
            grid.getChildren().remove(status);
            grid.getChildren().remove(acknowledge);
            grid.getChildren().remove(createAccount);
            super.onSuccess();
        }
    }

    @Override
    protected void onFail(WorkerStateEvent event) {
        if(Config.getInstance().getSettings().livejasminBetaAcknowledged) {
            status.setText("Login failed");
            grid.getChildren().remove(acknowledge);
            grid.getChildren().remove(createAccount);
            super.onFail(event);
        }
    }

    @Override
    public void selected() {
        super.selected();
    }

    public void setScene(Scene scene) {
        scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
            if(this.isSelected()) {
                if(event.getCode() == KeyCode.DELETE) {
                    follow(selectedThumbCells, false);
                }
            }
        });
    }
}
