import {Component, OnInit} from '@angular/core';
import {Router, ActivatedRoute} from '@angular/router';
import {AppConfig} from '../config/app.config';
import {HttpClient} from '@angular/common/http';

@Component({
    templateUrl: 'categories.component.html',
    styleUrls: ['categories.component.scss']
})

export class CategoriesComponent implements OnInit {
    currentUser: any;
    categories = [];
    items = [];

    constructor(private route: ActivatedRoute,
                private router: Router,
                private http: HttpClient) {
        this.currentUser = JSON.parse(sessionStorage.getItem(AppConfig.relativeURL + 'currentUser'));
    }

    openItem(item) {
        if (item.prodClass === '' && item.prodType === '') {
            this.router.navigate([AppConfig.routes.categories, item.prodCategory]);
        } else if (item.prodClass === '' && item.prodType !== '') {
            this.router.navigate([AppConfig.routes.categories, item.prodCategory, item.prodType]);
        } else if (item.prodClass !== '' && item.prodType !== '') {
            this.router.navigate([AppConfig.routes.products, item]);
        }
    }

    ngOnInit() {

        this.route.params.subscribe(params => {
            if (params.categoryID && params.typeID) {
                this.getLists(this.currentUser.sessionID).subscribe(
                    data => {
                        this.categories = data.dataset.ProductMenu;
                        this.items = this.categories.filter(
                            category => category.prodClass !== '' && category.prodType === params.typeID && category.prodCategory === params.categoryID);
                    },
                    error => {
                        console.log(error); // Error if any
                    });
            } else if (params.categoryID) {
                this.getLists(this.currentUser.sessionID).subscribe(
                    data => {
                        this.categories = data.dataset.ProductMenu;
                        this.items = this.categories.filter(
                            category => category.prodClass === '' && category.prodType !== '' && category.prodCategory === params.categoryID);
                    },
                    error => {
                        console.log(error); // Error if any
                    });
            } else {
                this.getLists(this.currentUser.sessionID).subscribe(
                    data => {
                        this.categories = data.dataset.ProductMenu;
                        this.items = this.categories.filter(
                            category => category.prodClass === '' && category.prodType === '');
                    },
                    error => {
                        console.log(error); // Error if any
                    });
            }
        });


    }

    private getLists(sessionID: string) {
        return this.http.post<any>(AppConfig.endpointURL() + AppConfig.endpoints.getProductCategories + sessionID, {}, {headers: {'Content-Type': 'text/plain'}}).map(data => {
            return data;
        });
    }
}
