import { Component } from '@angular/core';
import { NavController, NavParams, AlertController, LoadingController, ToastController } from 'ionic-angular';

import { LoginPage } from "../login/login";

import { SyncService } from "../../services/sync-service";
import { PouchService } from "../../services/pouch-service";

import { getMetadataStorage } from "../../odoo/utils/metadata-storage";

@Component({
    selector: 'page-tools',
    templateUrl: 'tools.html'
})
export class ToolsPage {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public alertCtrl: AlertController,
        public loadingCtrl: LoadingController,
        public toastCtrl: ToastController,
        public sync: SyncService,
        public pouch: PouchService
    ) { }

    /**
     *
     */
    ionViewDidLoad() {
        console.log('ionViewDidLoad ToolsPage');
    }

    /**
     *
     */
    askIfSyncData(): void {
        this.alertCtrl.create({
            title: "Confirmar",
            message: "Desea sincronizar sus datos ahora?",
            buttons: [
                {
                    text: "Cancelar"
                },
                {
                    text: "Aceptar",
                    handler: () => {
                        this.syncData();
                    }
                }
            ]
        }).present();
    }

    /**
     *
     */
    syncData(): void {
        let loader = this.loadingCtrl.create({
            content: "Actualizando datos , espere..."
        });
        loader.present();

        let toast = this.toastCtrl.create({
            duration: 3000
        });

        this.sync.do().subscribe(s => {

        }, e => {
            console.log(e);
            toast.setMessage("No se pudo actualizar los datos");
        }, () => {
            loader.dismiss();

            toast.setMessage("Datos actualizados correctamente");
            toast.present();
        });
    }

    /**
     *
     */
    askIfDeleteAccount() {
         this.alertCtrl.create({
            title: "Confirmar",
            message: "Desea eliminar su cuenta?",
            buttons: [
                {
                    text: "Cancelar"
                },
                {
                    text: "Aceptar",
                    handler: () => {
                        this.deleteAccount();
                    }
                }
            ]
        }).present();
    }

     /**
     *
     */
    deleteAccount() {
        let loader = this.loadingCtrl.create({
            content: "Eliminando cuenta, espere..."
        });
        loader.present();

        let toast = this.toastCtrl.create({
            duration: 3000
        });
        
        this.sync.removeAll().subscribe(s => { 
            localStorage.clear();
            this.navCtrl.setRoot(LoginPage);
        }, e => {
            toast.setMessage("No se pudo eliminar su cuenta");
        }, () => {
            loader.dismiss();
            toast.present();
        });
    }
}
