import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { IMall } from '../models/mall';
import { MallService } from '../services/mall.service';

@Component({
	selector: 'app-bookmark',
	templateUrl: './bookmark.page.html',
	styleUrls: ['./bookmark.page.scss'],
})
export class BookmarkPage implements OnInit {
	selected_tab: string = 'malls';
	malls: Array<IMall> = [];
	bookmarked_malls: number = 0;
	bookmarked_outlets: number = 0;

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

	ngOnInit() {
	}

	ionViewDidEnter() {
		this.bookmarked_outlets = 0;

		this.mallService.getAllMalls().then((data: Array<IMall>) => {
            this.malls = data;
			this.bookmarked_malls = this.malls.filter((mall => {
				return mall.is_bookmarked;
			})).length;

			let i: number, j: number;

			for (i = 0; i < this.malls.length; i += 1) {
				for (j = 0; j < this.malls[i].outlets.length; j += 1) {
					if (this.malls[i].outlets[j].is_bookmarked) {
						this.bookmarked_outlets += 1;
					}
				}
			}

        });
	}

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

	showMallDetails(mall: IMall) {
        this.router.navigate(['/mall-details', { mall_id: mall.id }]);
    }

	outletDetails(mall_id: string, outlet_id: string) {
        this.router.navigate(['/outlet-details', { mall_id: mall_id, outlet_id: outlet_id }]);
    }

}
