import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';
import { ICart } from '../cart/cart.page';
import * as moment from 'moment';
import { MallService } from '../services/mall.service';

@Component({
    selector: 'app-outlet-details',
    templateUrl: './outlet-details.page.html',
    styleUrls: ['./outlet-details.page.scss'],
})

export class OutletDetailsPage implements OnInit {
    show_top_bar: boolean = false;
    show_only_veg: boolean = false;
    selected_tag: string = null;
    show_sort_popup: boolean = false;
    selected_sort: string = null;
    show_grid: boolean = true;
	show_filter_popup: boolean = false;
    outlet_details: any;
    mallId: number;
    userCart: ICart = {
        orderedlist: []
    };

    constructor(
        private route: ActivatedRoute,
		private router: Router,
        private location: Location,
        private mallService: MallService
    ) { }

    ngOnInit() { }

    toggleOutletBookmark() {
        this.outlet_details.mall = {
            mall_id: this.mallId
        };
        this.outlet_details.is_bookmarked = !this.outlet_details.is_bookmarked;
        this.mallService.updateOutlet(this.outlet_details);
    }

    shareOutletDetails() {
        if (window.navigator && window.navigator['share']) {
            const shareData = {
                title: JSON.parse(this.route.snapshot.paramMap.get('mallData')).title + ',' + this.outlet_details.outlet_name,
                text: this.outlet_details.description,
                url: JSON.parse(this.route.snapshot.paramMap.get('mallData')).url
            }

            window.navigator['share'](shareData);
        }
    }

    getFormatterDateTime(date: any, format: string) {
        return moment(date).format(format);
    }

	ionViewDidEnter() {
		this.outlet_details = JSON.parse(this.route.snapshot.paramMap.get('outlet'));
		this.mallId = Number(this.route.snapshot.paramMap.get('mallId'));
        this.fetchCartItems();

        this.outlet_details.menuitems = this.outlet_details.menuitems.filter((item) => item.is_available);
	}

    fetchCartItems() {
        if (localStorage.userCart) {
            this.userCart = JSON.parse(localStorage.userCart);
        } else {
            localStorage.userCart = JSON.stringify(this.userCart);
        }
    }

    ionViewDidLeave() {
        localStorage.userCart = JSON.stringify(this.userCart);
    }

    addToCart(item: any) {
        this.userCart.orderedlist.push({
            mall_id: this.mallId,
            outlet_id: this.outlet_details.outlet_id,
            menuitem_id: item.menuitem_id,
            quantity: 1,
            pickup_time: moment().add(moment.duration(item.wait_duration).asMinutes(), 'minutes').format(),
            take_away: true,
            order_status: false,
            total_price: item.item_price - item.item_discount,
            soft_delete: false
        });
    }

    isItemPresentInCart(itemId: number) {
        let item = this.userCart.orderedlist.find((order) => {
            return order.menuitem_id === itemId;
        });

        return item ? true : false;
    }

    back() {
        this.location.back();
    }

    onScroll(event: any) {
        if (event.detail.scrollTop > 100) {
            this.show_top_bar = true;
        } else {
            this.show_top_bar = false;
        }
    }

    sortBy(type: string) {
        this.selected_sort = type;
        switch(this.selected_sort) {
            case 'name': this.outlet_details.menuitems.sort(function(a: any, b: any){
                if(a.menu_item_name < b.menu_item_name) { return -1; }
                if(a.menu_item_name > b.menu_item_name) { return 1; }
                return 0;
            });
            break;
            case 'rating': this.outlet_details.menuitems.sort(function(a: any, b: any){
                if(a.rating < b.rating) { return -1; }
                if(a.rating > b.rating) { return 1; }
                return 0;
            }).reverse();
            break;
            case 'price': this.outlet_details.menuitems.sort(function(a: any, b: any){
                if((a.item_price - a.item_discount) < (b.item_price - b.item_discount)) { return -1; }
                if((a.item_price - a.item_discount) > (b.item_price - b.item_discount)) { return 1; }
                return 0;
            });
            break;
        }
    }

	goToCart() {
        localStorage.userCart = JSON.stringify(this.userCart);
		this.router.navigate(['/cart']);
	}

}
