package dev.lions.btpxpress.view;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Named;
import lombok.Getter;
import lombok.Setter;
import org.primefaces.PrimeFaces;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Named("guestPreferences")
@SessionScoped
@Getter
@Setter
public class GuestPreferences implements Serializable {

    private static final long serialVersionUID = 1L;

    private String menuMode = "layout-sidebar";
    private String darkMode = "light";
    private String componentTheme = "purple";
    private String topbarTheme = "light";
    private String menuTheme = "light";
    private String inputStyle = "outlined";
    private boolean lightLogo = false;
    private List<ComponentTheme> componentThemes = new ArrayList<>();

    @PostConstruct
    public void init() {
        componentThemes.add(new ComponentTheme("Bleu", "blue", "#2c84d8"));
        componentThemes.add(new ComponentTheme("Vert", "green", "#34B56F"));
        componentThemes.add(new ComponentTheme("Orange", "orange", "#FF810E"));
        componentThemes.add(new ComponentTheme("Turquoise", "turquoise", "#58AED3"));
        componentThemes.add(new ComponentTheme("Avocat", "avocado", "#AEC523"));
        componentThemes.add(new ComponentTheme("Violet", "purple", "#464DF2"));
        componentThemes.add(new ComponentTheme("Rouge", "red", "#FF9B7B"));
        componentThemes.add(new ComponentTheme("Jaune", "yellow", "#FFB340"));
    }

    public void setDarkMode(String darkMode) {
        this.darkMode = darkMode;
        this.menuTheme = darkMode;
        this.topbarTheme = darkMode;
        this.lightLogo = !this.topbarTheme.equals("light");
    }

    public String getLayout() {
        return "layout-" + this.darkMode;
    }

    public String getTheme() {
        return this.componentTheme + '-' + this.darkMode;
    }

    public void setTopbarTheme(String topbarTheme) {
        this.topbarTheme = topbarTheme;
        this.lightLogo = !this.topbarTheme.equals("light");
    }

    public String getInputStyleClass() {
        return this.inputStyle.equals("filled") ? "ui-input-filled" : "";
    }

    public void onMenuTypeChange() {
        if ("layout-horizontal".equals(menuMode)) {
            menuTheme = topbarTheme;
            PrimeFaces.current().executeScript(
                "PrimeFaces.FreyaConfigurator.changeSectionTheme('" + menuTheme + "' , 'layout-menu')"
            );
        }
    }

    @lombok.Getter
    @lombok.Setter
    public static class ComponentTheme implements Serializable {
        private static final long serialVersionUID = 1L;
        private String name;
        private String file;
        private String color;

        public ComponentTheme(String name, String file, String color) {
            this.name = name;
            this.file = file;
            this.color = color;
        }
    }
}
