import { Component, Input } from '@angular/core';
import { ObservableArray } from '@nativescript/core';
import { StatsDataBase } from '~/pages/sts/entities/stats-data-base';

@Component({
    selector: 'StsStatsChart',
    moduleId: module.id,
    templateUrl: './sts-stats-chart.component.html',
    styleUrls: ['./_sts-stats-chart.component.scss']
})
export class StsStatsChartComponent {
    statsDataObservable: any;
    statsDataObservableKeys: string[];

    /**
     * It prepare data for graph.
     * It create object where key is phone number and data are StatsDataBase entity
     * Then are data sorted (ASC by date) and ObservableArray is created from that.
     *
     * @param {StatsDataBase[]} value
     */
    @Input() set statsDataBase(value: StatsDataBase[]) {
        // value.push({
        //             enrollInvoiceId: 131,
        //             date: "12.2016",
        //             price: 382,
        //             phoneNumber: "777888999"
        //         });
        // value.push({
        //     enrollInvoiceId: 131,
        //     date: "12.2016",
        //     price: 382,
        //     phoneNumber: "777888999"
        // });
        // value.push({
        //     enrollInvoiceId: 131,
        //     date: "05.2016",
        //     price: 382,
        //     phoneNumber: "777888999"
        // });
        // value.push({
        //     enrollInvoiceId: 131,
        //     date: "10.2017",
        //     price: 382,
        //     phoneNumber: "777888999"
        // });
        // value.push({
        //     enrollInvoiceId: 131,
        //     date: "11.2017",
        //     price: 382,
        //     phoneNumber: "777888999"
        // });

        //  sort a prepare dates for filtering only last 3 monts
        // let dates = value
        //     .map(sdb => sdb.date)
        //     .filter((val, index, self) => self.indexOf(val) === index);
        //
        // dates = dates
        //     .sort((a, b) => {
        //         const [aDay, aYear] = a.split('.');
        //         const [bDay, bYear] = b.split('.');
        //
        //         if (parseInt(aYear, 10) === parseInt(bYear, 10)) {
        //             if (parseInt(aDay, 10) < parseInt(bDay, 10)) {
        //                 return 1;
        //             } else {
        //                 return -1;
        //             }
        //         } else if (parseInt(aYear, 10) < parseInt(bYear, 10)) {
        //             return 1;
        //         } else {
        //             return -1;
        //         }
        //     })
        //     .slice(0, 3);

        const statsData = value.reduce((acc: any, curr) => {
            if (curr.phoneNumber) {
                if (acc.hasOwnProperty(curr.phoneNumber)) {
                    acc[curr.phoneNumber].push(curr);
                } else {
                    acc[curr.phoneNumber] = [curr];
                }
            }

            return acc;
        }, {});

        // enrollInvoiceId": 497666,
        // "date": "10.2019",
        // "price": 452,
        // "phoneNumber": "721807386"

        const statsDataKeys = Object.keys(statsData); //  721807386,723408957,728501928

        this.statsDataObservable = statsDataKeys.reduce((acc, curr) => ({
            ...acc,
            [curr]: new ObservableArray(
                statsData[curr]
                    .sort((a, b) => { // insert to ObservableArray sorting array of dates ASC
                        const [aDay, aYear] = a.date.split('.');
                        const [bDay, bYear] = b.date.split('.');

                        if (parseInt(aYear, 10) === parseInt(bYear, 10)) {
                            if (parseInt(aDay, 10) < parseInt(bDay, 10)) {
                                return -1;
                            } else {
                                return 1;
                            }
                        } else if (parseInt(aYear, 10) < parseInt(bYear, 10)) {
                            return -1;
                        } else {
                            return 1;
                        }
                    })
            )
        }), {});

        this.statsDataObservableKeys = Object.keys(this.statsDataObservable);
    }
}
