import { Component } from '@angular/core';
import { NavController, AlertController, LoadingController, ToastController } from 'ionic-angular';
import { LoginPage } from '../login/login';
import { DataProvider } from '../../providers/data-provider';
import { SyncProvider } from '../../providers/sync-provider';

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

    constructor(
        public navCtrl: NavController,
        public alertCtrl: AlertController,
        public loadingCtrl: LoadingController,
        public toastCtrl: ToastController,
        public dataProvider: DataProvider, 
        public syncProvider: SyncProvider
    ) { }

    ionViewDidLoad() {
      console.log('Hello Tools Page');
    }

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

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

        loader.present();

        this.syncProvider.doSync().then(r => {
            loader.dismiss();

            this.toastCtrl.create({
                message: "Datos actualizados correctamente",
                duration: 3000
            }).present();
        }).catch(e => {
            console.log(e);
            loader.dismiss();

            this.toastCtrl.create({
                message: "No se pudo actualizar los datos",
                duration: 3000
            }).present();
        });
    }

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

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

        loader.present();

        this.dataProvider.dropDatabase().then(r => {
            localStorage.clear();
            this.navCtrl.setRoot(LoginPage);

            loader.dismiss();
        }).catch(e => {
            console.log(e);
            loader.dismiss();

            this.toastCtrl.create({
                message: "No se pudo eliminar su cuenta",
                duration: 3000
            }).present();
        });
    }
}
