import * as tslib_1 from "tslib";
import { Component, ApplicationRef } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ConfigService } from 'src/providers/config/config.service';
import { ModalController, NavController, Events, NavParams } from '@ionic/angular';
import { SharedDataService } from 'src/providers/shared-data/shared-data.service';
import { AlertService } from 'src/providers/alert/alert.service';
import { SignUpPage } from '../sign-up/sign-up.page';
import { ForgotPasswordPage } from '../forgot-password/forgot-password.page';
import { Facebook } from '@ionic-native/facebook/ngx';
import { LoadingService } from 'src/providers/loading/loading.service';
let LoginPage = class LoginPage {
    constructor(http, config, modalCtrl, loading, shared, fb, alert, applicationRef, navCtrl, events, navParams) {
        this.http = http;
        this.config = config;
        this.modalCtrl = modalCtrl;
        this.loading = loading;
        this.shared = shared;
        this.fb = fb;
        this.alert = alert;
        this.applicationRef = applicationRef;
        this.navCtrl = navCtrl;
        this.events = events;
        this.navParams = navParams;
        this.hideGuestLogin = false;
        this.formData = { username: '', password: '' };
        this.errorMessage = '';
        this.hideGuestLogin = navParams.get('hideGuestLogin');
        console.log(this.hideGuestLogin);
    }
    // <!-- 2.0 updates -->
    guestLogin() {
        if (this.config.checkOutPage == 1)
            this.shared.onePageCheckOut();
        else
            this.events.publish('openShippingAddressPage');
        this.dismiss();
    }
    login() {
        this.loading.show();
        this.errorMessage = '';
        var formData = new FormData();
        formData.append("username", this.formData.username);
        formData.append("password", this.formData.password);
        this.http.post(this.config.url + '/api/appusers/generate_cookie/?insecure=cool', formData).subscribe((data) => {
            if (data.status == "ok")
                this.getUserData(data, "simple");
            else {
                this.errorMessage = data.error;
                this.loading.hide();
            }
        }, err => {
            this.loading.hide();
            if (err.ok == false) {
                this.errorMessage = "Invalid Username or Password";
            }
        });
        // this.config.Woocommerce.getAsync("customers/" + 118 + "?" + this.config.productsArguments).then((data) => {
        //   this.loading.hide();
        //   this.shared.login(JSON.parse(data.body));
        //   //console.log(this.shared.customerData);
        //   this.dismiss();
        //   this.applicationRef.tick();
        // });
    }
    getUserData(c, type) {
        let id;
        if (type == "simple")
            id = c.user.id;
        if (type == "fb")
            id = c.id;
        //alert(c.user.id + " -- " + c.id + " --- " + id);
        this.config.Woocommerce.getAsync("customers/" + id + "?" + this.config.productsArguments).then((data) => {
            this.loading.hide();
            let dat = JSON.parse(data.body);
            //alert(JSON.stringify(dat));
            this.shared.login(Object.assign({ cookie: c.cookie }, dat));
            // alert(JSON.stringify(Object.assign({ cookie: c.cookie }, dat)));
            this.dismiss();
            this.applicationRef.tick();
        });
    }
    openSignUpPage() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const modal = yield this.modalCtrl.create({
                component: SignUpPage
            });
            return yield modal.present();
        });
    }
    openForgetPasswordPage() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const modal = yield this.modalCtrl.create({
                component: ForgotPasswordPage
            });
            return yield modal.present();
        });
    }
    facebookLogin() {
        this.fb.getLoginStatus().then((res) => {
            if (res.status == 'connected') {
                console.log("user connected already" + res.authResponse.accessToken);
                this.createAccount(res.authResponse.accessToken, 'fb');
            }
            else {
                console.log("USer Not login ");
                this.fb.login(['public_profile', 'user_friends', 'email'])
                    .then((res) => {
                    // this.alert.show('Logged into Facebook!' + JSON.stringify(res));
                    console.log("successfully login ");
                    this.createAccount(res.authResponse.accessToken, 'fb');
                })
                    .catch(e => this.alert.show('Error logging into Facebook' + JSON.stringify(e)));
            }
        }).catch(e => this.alert.show('Error Check Login Status Facebook' + JSON.stringify(e)));
    }
    // googleLogin() {
    //   this.loading.autoHide(500);
    //   this.googlePlus.login({})
    //     .then(res => {
    //       //  alert(JSON.stringify(res))
    //       this.createAccount(res, 'google');
    //     })
    //     .catch(err => this.alert.show(JSON.stringify(err)));
    // }
    //============================================================================================  
    //creating new account using function facebook or google details 
    createAccount(info, type) {
        //this.formData.username = info;
        // alert(info);
        this.loading.show();
        var url = '';
        url = '/api/appusers/fb_connect/?insecure=cool&access_token=' + info;
        this.http.get(this.config.url + url).subscribe((data) => {
            this.loading.hide();
            //alert(JSON.stringify(data));
            this.getUserData(data, "fb");
        }, error => {
            this.loading.hide();
            //this.alert.show("error " + JSON.stringify(error));
        });
    }
    ;
    //close modal
    dismiss() {
        this.modalCtrl.dismiss();
    }
    ngOnInit() {
    }
};
LoginPage = tslib_1.__decorate([
    Component({
        selector: 'app-login',
        templateUrl: './login.page.html',
        styleUrls: ['./login.page.scss'],
    }),
    tslib_1.__metadata("design:paramtypes", [HttpClient,
        ConfigService,
        ModalController,
        LoadingService,
        SharedDataService,
        Facebook,
        AlertService,
        ApplicationRef,
        NavController,
        Events,
        NavParams])
], LoginPage);
export { LoginPage };
//# sourceMappingURL=login.page.js.map