import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { ToastrService } from 'ngx-toastr';

@Injectable({
    providedIn: 'root'
})
export class UtilsService {

    public colors: any = {
        primary: '#8d2790',
        secondary: '#2c3890',
        three: '#9b7ede',
        four: '#2a446f',
        five: '#7280ac',
        six: '#46768b',
        seven: '#C62E65'
    };

    public colors_category: any = {
        "For Time": "primary",
        "Rest": "secondary",
        "Tabata": "three",
        "For max total load": "four",
        "For Load": "five",
        "EMOM": "six",
        "Death By": "seven",
        "Chipper": "three", 
        "AMRAP": "seven",
        "AMRAP For As Long As Possible": "seven"
    };

    public colors_type: any = {
        wod: "primary",
        open: "secondary",
        olympics: "three",
        power: "four",
        endurance: "five",
        gymnastics: "six",
        unset: "seven",
        basics: "three", 
        mobility: "four",
        kids: "five",
        advance: "six",
        special: "secondary",
        megawod: "primary",
        on_ramp: "three"
    };

    private subjectLoading: Subject<boolean> = new Subject();

    constructor(private toastr:ToastrService) { }


    /**
     * NOTIFICATIONS 
     */
    showMessage(type:string, title:string, message:string = null) {
        switch(type) {
            case 'success':
                this.toastr.success(message, title, {
                    progressBar: true
                });
                break;
            case 'info':
                this.toastr.info(message, title, {
                    progressBar: true
                });
                break;
            case 'warning':
                this.toastr.warning(message, title, {
                    progressBar: true
                });
                break;
            case 'error':
                this.toastr.error(message, title, {
                    progressBar: true
                });
                break;
            default:
                this.toastr.info(message, title, {
                    progressBar: true
                });
                break;
        }
        
    }

    clearNotifications() {
        this.toastr.clear();
    }

    /**
     * LOADING OBSERVABLE
     */
    enableLoading() {
        this.subjectLoading.next(true);
    }

    disableLoading() {
        this.subjectLoading.next(false);
    }

    getLoadingAsObservable() {
        return this.subjectLoading.asObservable();
    }

    /**
     * PRINT
     */
    print() {
        window.print();
    }

    /**
     * COLORS
     */
    getColors() {
        return this.colors;
    }

    getColor(key:string) {
        return this.colors[key];
    }

    getColorsArray() {
        return Object.keys(this.colors).map((key)=>{ return this.colors[key]});
    }

    getCategoryColor(c) {
        if (this.colors_category[c]) return this.colors[this.colors_category[c]];
        else return this.colors[this.colors_category["For Time"]];
    }

    getTypeColor(t) {
        if (this.colors_type[t]) return this.colors[this.colors_type[t]];
        else return this.colors[this.colors_type["wod"]];
    }

}
