import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { MallService } from '../services/mall.service';
import { ToastService } from '../services/toast.service';
import { LoadingController } from '@ionic/angular';

@Component({
	selector: 'app-bookmark',
	templateUrl: './bookmark.page.html',
	styleUrls: ['./bookmark.page.scss'],
})
export class BookmarkPage implements OnInit {
    tempMalls: any = [];
    loader: any;
    
	constructor(
		private router: Router,
        private location: Location,
        private mallService: MallService,
        private toastService: ToastService,
        public loadingController: LoadingController,
	) { }

	ngOnInit() {
        
    }

    async presentLoading() {
        this.loader = await this.loadingController.create({
          message: 'Please wait...',
          spinner: 'dots',
          mode: 'ios'
        });
        await this.loader.present();
    }

    ionViewDidEnter() {
        this.presentLoading();
        this.mallService.getBookmarks().then((data) => {
            this.tempMalls = data;
            this.loader ? this.loader.dismiss() : null;
        }, () => {
            this.loader ? this.loader.dismiss() : null;
            this.toastService.presentToast("Failed to get malls / outlets bookmarked");
        });
    }

    shareMallDetails(mallData) {
        if (window.navigator && window.navigator['share']) {
            const shareData = {
                title: mallData.mall.mall_name,
                text: mallData.mall.description,
                url: 'https://maps.google.com/?q=' + mallData.latitude + ',' + mallData.longitude
            }

            window.navigator['share'](shareData);
        }
    }
    
    toggleMallBookmark(mallData: any) {
        mallData.mall.is_bookmarked = !mallData.mall.is_bookmarked;
        this.mallService.updateMallData(mallData);
    }

    showMallDetails(mall: any) {
        this.router.navigate(['/mall-details', { mall: JSON.stringify(mall) }]);
    }


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

}
