import { AllCommunityModules } from "@ag-grid-community/all-modules";
import { Component, ViewChild } from '@angular/core';
import { IspSpinnerComponent, SpinnerService } from '@isp/xdce-arch-core';
import { INbpButton, NbpBaseComponent, NbpDataSource, NbpSize, NbpStyle } from '@isp/xdce-widget';
import { NbpFidDonutChartConfig } from '../nbp-fid-donut-chart/nbp-fid-donut-chart.component';
import { ISidePopupButton } from '../nbp-fid-side-popup/nbp-fid-side-popup.interfaces';
import { setSpinnerMessage } from "../../Utils";

@Component({
    selector: 'widget-fideuram-showcase',
    templateUrl: './widget-fideuram-showcase.component.html',
    styleUrls: ['./widget-fideuram-showcase.component.scss']
})
export class WidgetFideuramShowcaseComponent extends NbpBaseComponent {
    constructor(private spinnerService: SpinnerService) {
        super();
        this.iconspinner = './assets/xdce-widget/images/spinner.svg';
    }

    _nbpStyle = NbpStyle;

    @ViewChild("testPropMessage") spinnerTest: IspSpinnerComponent;
    @ViewChild("testCssMessage") spinnerTestCss: IspSpinnerComponent;
    iconspinner: string;
    

    longText: string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam porta enim sapien. Etiam interdum ullamcorper ullamcorper. Ut malesuada orci ac sapien tempor dignissim. Phasellus congue libero sit amet ex euismod pulvinar. Duis dolor tellus, convallis id odio vitae, iaculis feugiat lectus. Integer tempor ex ac justo dictum, at vehicula dui malesuada. Praesent eu turpis eros. Aliquam vel metus ac tellus eleifend iaculis. Nam venenatis eget sapien eget euismod. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nunc sagittis fermentum scelerisque. Sed luctus ut mi et molestie. Nullam ultricies, nisl aliquam rhoncus cursus, magna libero accumsan neque, id bibendum lorem mauris vitae libero. Proin sodales felis placerat mi venenatis gravida. Nam eu metus velit. Morbi in sapien ut urna vulputate sollicitudin. Nulla dictum eleifend enim, at hendrerit velit feugiat ac. Cras gravida a augue ut fermentum. Integer sagittis velit ut viverra scelerisque. Pellentesque sed magna at lectus interdum vehicula id vitae diam. Integer sed placerat nisi, non maximus eros. ";
    
    
    combo1Value: string = 'val2';
    combo1Values: Array<any> = new Array<any>(
        { label: "val 1", value: "val1" },
        { label: "val 2", value: "val2" },
    );
    combo1DataSource: NbpDataSource<any> = new NbpDataSource(this.combo1Values);


    ngAfterViewInit(): void {
        setSpinnerMessage("Caricamento in corso...", "testCssMessage");
        this.spinnerTest.showSpinner();
        this.spinnerTestCss.showSpinner();
    }

    donutData = [
        { name: 'A', value: 527 },
        { name: 'B', value: 333 },
        { name: 'C', value: 1245 },
        { name: 'D', value: 829 },
        { name: 'E', value: 2322 }
    ];

    donutConfig: NbpFidDonutChartConfig = {
        valueKey: 'value',
        labelKey: 'name',
        legendKey: 'name',
        format: (val: number) => {
            return Intl.NumberFormat('it-IT', { style: 'currency', currency: 'EUR' }).format(val);
        }
    }

    barData = [
        { name: 'A', value: 10 },
        { name: 'B', value: 20 },
        { name: 'C', value: 30 },
        { name: 'D', value: 40 },
        { name: 'E', value: 50 },
        { name: 'F', value: -50 }
    ];

    barConfig: NbpFidDonutChartConfig = {
        valueKey: 'value',
        labelKey: 'name',
        legendKey: 'name',
        format: (val: number) => val.toString()
    }

    NbpStyle = NbpStyle;
    NbpSize = NbpSize;

    // Stato dei popup
    showBasicPopup = false;
    showFormPopup = false;
    showInfoPopup = false;

    // Form data
    formData = {
        name: '',
        email: '',
        message: ''
    };

    // Configurazione bottoni per il popup base
    basicButtons: ISidePopupButton[] = [
        {
            id: 'cancel',
            label: 'Annulla',
            style: NbpStyle.DEFAULT,
            visible: true,
            disabled: false
        },
        {
            id: 'confirm',
            label: 'Conferma',
            style: NbpStyle.PRIMARY,
            visible: true,
            disabled: false,
            icon: 'ispv2-font-check'
        }
    ];

    // Configurazione bottoni per il form popup
    formButtons: ISidePopupButton[] = [
        {
            id: 'back',
            label: 'Indietro',
            style: NbpStyle.DEFAULT,
            visible: true,
            disabled: false,
            icon: 'ispv2-font-arrow-left',
            invertIconPosition: true
        },
        {
            id: 'save',
            label: 'Salva',
            style: NbpStyle.PRIMARY,
            visible: true,
            disabled: false,
            icon: 'ispv2-font-save'
        }
    ];

    onBasicButtonClick(button: INbpButton) {
        switch (button.id) {
            case 'confirm':
                console.log('Confermato');
                this.showBasicPopup = false;
                break;
            case 'cancel':
                this.showBasicPopup = false;
                break;
        }
    }

    onFormButtonClick(button: INbpButton) {
        switch (button.id) {
            case 'save':
                if (this.isFormValid()) {
                    console.log('Form data:', this.formData);
                    this.showFormPopup = false;
                }
                break;
            case 'back':
                this.showFormPopup = false;
                break;
        }
    }

    private isFormValid(): boolean {
        return !!(this.formData.name && this.formData.email);
    }

    rowData = [{ "athlete": "Michael Phelps", "age": 23, "country": "United States", "year": 2008, "date": "24/08/2008", "sport": "Swimming", "gold": 8, "silver": 0, "bronze": 0, "total": 8 }, { "athlete": "Michael Phelps", "age": 19, "country": "United States", "year": 2004, "date": "29/08/2004", "sport": "Swimming", "gold": 6, "silver": 0, "bronze": 2, "total": 8 }, { "athlete": "Michael Phelps", "age": 27, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 4, "silver": 2, "bronze": 0, "total": 6 }, { "athlete": "Natalie Coughlin", "age": 25, "country": "United States", "year": 2008, "date": "24/08/2008", "sport": "Swimming", "gold": 1, "silver": 2, "bronze": 3, "total": 6 }, { "athlete": "Aleksey Nemov", "age": 24, "country": "Russia", "year": 2000, "date": "01/10/2000", "sport": "Gymnastics", "gold": 2, "silver": 1, "bronze": 3, "total": 6 }, { "athlete": "Alicia Coutts", "age": 24, "country": "Australia", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 1, "silver": 3, "bronze": 1, "total": 5 }, { "athlete": "Missy Franklin", "age": 17, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 4, "silver": 0, "bronze": 1, "total": 5 }, { "athlete": "Ryan Lochte", "age": 27, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 2, "silver": 2, "bronze": 1, "total": 5 }, { "athlete": "Allison Schmitt", "age": 22, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 3, "silver": 1, "bronze": 1, "total": 5 }, { "athlete": "Natalie Coughlin", "age": 21, "country": "United States", "year": 2004, "date": "29/08/2004", "sport": "Swimming", "gold": 2, "silver": 2, "bronze": 1, "total": 5 }, { "athlete": "Ian Thorpe", "age": 17, "country": "Australia", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 3, "silver": 2, "bronze": 0, "total": 5 }, { "athlete": "Dara Torres", "age": 33, "country": "United States", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 2, "silver": 0, "bronze": 3, "total": 5 }, { "athlete": "Cindy Klassen", "age": 26, "country": "Canada", "year": 2006, "date": "26/02/2006", "sport": "Speed Skating", "gold": 1, "silver": 2, "bronze": 2, "total": 5 }, { "athlete": "Nastia Liukin", "age": 18, "country": "United States", "year": 2008, "date": "24/08/2008", "sport": "Gymnastics", "gold": 1, "silver": 3, "bronze": 1, "total": 5 }, { "athlete": "Marit Bjørgen", "age": 29, "country": "Norway", "year": 2010, "date": "28/02/2010", "sport": "Cross Country Skiing", "gold": 3, "silver": 1, "bronze": 1, "total": 5 }, { "athlete": "Sun Yang", "age": 20, "country": "China", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 1, "total": 4 }, { "athlete": "Kirsty Coventry", "age": 24, "country": "Zimbabwe", "year": 2008, "date": "24/08/2008", "sport": "Swimming", "gold": 1, "silver": 3, "bronze": 0, "total": 4 }, { "athlete": "Libby Lenton-Trickett", "age": 23, "country": "Australia", "year": 2008, "date": "24/08/2008", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 1, "total": 4 }, { "athlete": "Ryan Lochte", "age": 24, "country": "United States", "year": 2008, "date": "24/08/2008", "sport": "Swimming", "gold": 2, "silver": 0, "bronze": 2, "total": 4 }, { "athlete": "Inge de Bruijn", "age": 30, "country": "Netherlands", "year": 2004, "date": "29/08/2004", "sport": "Swimming", "gold": 1, "silver": 1, "bronze": 2, "total": 4 }, { "athlete": "Petria Thomas", "age": 28, "country": "Australia", "year": 2004, "date": "29/08/2004", "sport": "Swimming", "gold": 3, "silver": 1, "bronze": 0, "total": 4 }, { "athlete": "Ian Thorpe", "age": 21, "country": "Australia", "year": 2004, "date": "29/08/2004", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 1, "total": 4 }, { "athlete": "Inge de Bruijn", "age": 27, "country": "Netherlands", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 3, "silver": 1, "bronze": 0, "total": 4 }, { "athlete": "Gary Hall Jr.", "age": 25, "country": "United States", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 1, "total": 4 }, { "athlete": "Michael Klim", "age": 23, "country": "Australia", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 2, "silver": 2, "bronze": 0, "total": 4 }, { "athlete": "Susie O'Neill", "age": 27, "country": "Australia", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 1, "silver": 3, "bronze": 0, "total": 4 }, { "athlete": "Jenny Thompson", "age": 27, "country": "United States", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 3, "silver": 0, "bronze": 1, "total": 4 }, { "athlete": "Pieter van den Hoogenband", "age": 22, "country": "Netherlands", "year": 2000, "date": "01/10/2000", "sport": "Swimming", "gold": 2, "silver": 0, "bronze": 2, "total": 4 }, { "athlete": "An Hyeon-Su", "age": 20, "country": "South Korea", "year": 2006, "date": "26/02/2006", "sport": "Short-Track Speed Skating", "gold": 3, "silver": 0, "bronze": 1, "total": 4 }, { "athlete": "Aliya Mustafina", "age": 17, "country": "Russia", "year": 2012, "date": "12/08/2012", "sport": "Gymnastics", "gold": 1, "silver": 1, "bronze": 2, "total": 4 }, { "athlete": "Shawn Johnson", "age": 16, "country": "United States", "year": 2008, "date": "24/08/2008", "sport": "Gymnastics", "gold": 1, "silver": 3, "bronze": 0, "total": 4 }, { "athlete": "Dmitry Sautin", "age": 26, "country": "Russia", "year": 2000, "date": "01/10/2000", "sport": "Diving", "gold": 1, "silver": 1, "bronze": 2, "total": 4 }, { "athlete": "Leontien Zijlaard-van Moorsel", "age": 30, "country": "Netherlands", "year": 2000, "date": "01/10/2000", "sport": "Cycling", "gold": 3, "silver": 1, "bronze": 0, "total": 4 }, { "athlete": "Petter Northug Jr.", "age": 24, "country": "Norway", "year": 2010, "date": "28/02/2010", "sport": "Cross Country Skiing", "gold": 2, "silver": 1, "bronze": 1, "total": 4 }, { "athlete": "Ole Einar Bjørndalen", "age": 28, "country": "Norway", "year": 2002, "date": "24/02/2002", "sport": "Biathlon", "gold": 4, "silver": 0, "bronze": 0, "total": 4 }, { "athlete": "Janica Kostelic", "age": 20, "country": "Croatia", "year": 2002, "date": "24/02/2002", "sport": "Alpine Skiing", "gold": 3, "silver": 1, "bronze": 0, "total": 4 }, { "athlete": "Nathan Adrian", "age": 23, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 0, "total": 3 }, { "athlete": "Yannick Agnel", "age": 20, "country": "France", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 0, "total": 3 }, { "athlete": "Brittany Elmslie", "age": 18, "country": "Australia", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 1, "silver": 2, "bronze": 0, "total": 3 }, { "athlete": "Matt Grevers", "age": 27, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 0, "total": 3 }, { "athlete": "Ryosuke Irie", "age": 22, "country": "Japan", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 0, "silver": 2, "bronze": 1, "total": 3 }, { "athlete": "Cullen Jones", "age": 28, "country": "United States", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 1, "silver": 2, "bronze": 0, "total": 3 }, { "athlete": "Ranomi Kromowidjojo", "age": 21, "country": "Netherlands", "year": 2012, "date": "12/08/2012", "sport": "Swimming", "gold": 2, "silver": 1, "bronze": 0, "total": 3 }];
    columnDefs = [
        {
            headerName: "Athlete",
            field: "athlete",
            width: 150,
            sort: "desc"
        },
        {
            headerName: "Age",
            field: "age",
            width: 90
        },
        {
            headerName: "Country",
            field: "country",
            width: 120
        },
        {
            headerName: "Year",
            field: "year",
            width: 90,
            unSortIcon: true
        },
        {
            headerName: "Date",
            field: "date",
            width: 110,
            comparator: dateComparator
        },
        {
            headerName: "Sport",
            field: "sport",
            width: 110
        },
        {
            headerName: "Gold",
            field: "gold",
            width: 100
        },
        {
            headerName: "Silver",
            field: "silver",
            width: 100
        },
        {
            headerName: "Bronze",
            field: "bronze",
            width: 100
        },
        {
            headerName: "Total",
            field: "total",
            width: 100
        }
    ];

    defaultColDef = { sortable: true };
    multiSortKey = "ctrl";
    modules = AllCommunityModules;
}

function dateComparator(date1, date2) {
    var date1Number = monthToComparableNumber(date1);
    var date2Number = monthToComparableNumber(date2);
    if (date1Number === null && date2Number === null) {
        return 0;
    }
    if (date1Number === null) {
        return -1;
    }
    if (date2Number === null) {
        return 1;
    }
    return date1Number - date2Number;
}
function monthToComparableNumber(date) {
    if (date === undefined || date === null || date.length !== 10) {
        return null;
    }
    var yearNumber = date.substring(6, 10);
    var monthNumber = date.substring(3, 5);
    var dayNumber = date.substring(0, 2);
    var result = yearNumber * 10000 + monthNumber * 100 + dayNumber;
    return result;
}