package com.example.schedulerapp;

import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.GridPane;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;

public class AvailabilityView extends ScrollPane implements ModelSubscriber{
    Model model;
    ArrayList<Label> days;
    ArrayList<Label> times;
    GridPane contents;

    public AvailabilityView() {
        //list of day labels
        days = new ArrayList<>();
        days.addAll(Arrays.asList(new Label("Sunday"), new Label("Monday"), new Label("Tuesday"),
                new Label("Wednesday"), new Label("Thursday"), new Label("Friday"), new Label("Saturday")));

        for (Label label : days) {
            GridPane.setHalignment(label, HPos.CENTER);
            label.setAlignment(Pos.CENTER);
            label.setPrefWidth(80);
        }

        times = new ArrayList<>();
        for (int i = 0; i < 24; i++){
            String j = i + "00";
            String k = i + "30";
            if (i < 10) {
                j = "0" + j;
                k = "0" + k;
            }
            Label jLabel = new Label(j);
            Label kLabel = new Label(k);
            times.add(jLabel);
            times.add(kLabel);
        }
        for (Label label : times){
            label.setPrefHeight(18);
            //label.setStyle(" -fx-border-color: black");
        }

        contents = new GridPane();
        contents.setStyle("-fx-background-color: darkgrey");

        this.setContent(contents);
    }


    public void draw(){
        //clear
        contents.getChildren().clear();

        Label blank = new Label("      ");
        blank.setStyle("-fx-background-color: white");
        contents.add(blank, 0, 0);

        //add to gridpane - time axis never changes
        int i = 1;
        for (Label time : times){
            contents.add(time, 0, i);
            time.setStyle("-fx-background-color: white");
            i++;
        }

        //add to gridpane - day axis never changes
        i = 1;
        for (Label time : days){
            contents.add(time, i, 0);
            time.setStyle("-fx-background-color: white");
            i++;
        }

        //get employee
        Employee e = model.getEmployee(model.getSelectedEmployee());
        LocalTime[][] ava = e.getAvailability();
        System.out.println("View: " + ava[0][0] +" "+ ava[0][1]);
        for (int j = 0; j < 7; j++){
            if (ava[j][0] == null || ava[j][0] == null){
                System.out.println("availbiity null error");
                continue;
            }
            Button b = this.formatButton(ava[j][0], ava[j][1]);
            contents.add(b, j+1, this.availGetRow(ava[j]), 1, this.availGetLength(ava[j]));
        }



    }
    public int availGetRow(LocalTime[] times){
        //convert from start time to offset, granularity in 30 min increments
        int row = (times[0].getHour()) * 2 + 1;
        if (times[0].getMinute() == 30) row++;
        return row;
    }

    public int availGetLength(LocalTime[] times){
        //calculate length
        int length = (times[0].getHour() - times[1].getHour()) * 2;
        if (times[0].getMinute() == 30) length--;
        if (times[1].getMinute() == 30) length++;
        return length;
    }


    public String formatTimeLabel(LocalTime start, LocalTime end){
        //String text = start.format(timeFormat);
        String text = start.toString();
        text += "\n   -   \n";
        //text += end.format(timeFormat);
        text += end.toString();
        return text;
    }

    public Button formatButton(LocalTime start, LocalTime end){
        Button rep = new Button(formatTimeLabel(start, end));
        rep.setMaxHeight(700);                 //so it always fills its region
        rep.setMaxWidth(100);
        GridPane.setFillHeight(rep, true);
        GridPane.setFillWidth(rep, true);
        //css styling
        rep.setStyle("-fx-background-color: lightgreen");

        return rep;
    }

    public void setModel(Model m){
        model = m;
        draw();
    }

    @Override
    public void modelChanged() {
        draw();
    }
}