import { Component } from '@angular/core';
import { NavController, NavParams, LoadingController, Platform } from 'ionic-angular';
import { Storage } from '@ionic/storage';

import { HomePage } from '../home/home';
import { Pata } from '../../pata';

import { AuthService } from '../../services/auth.service';
import { SignupPage } from "../signup/signup";
import { StatusBar } from '@ionic-native/status-bar';
import { LostPasswordPage } from '../lostpassword/lostpassword';
import { FarmaciaPage } from '../farmacia/farmacia';
import { RutValidator } from 'ng2-rut';
import { SignupPage2 } from '../signup/signup2';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  
  public loading: any;
  appInvite: boolean = true;

  public login: { rut:string, password:string, level:string } = {
    rut: '',
    password:'',
    level: '1,5'
  };

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public service: Pata,
    public loadingCtrl: LoadingController,
    public storage: Storage,
    public platform: Platform,
    public statusBar: StatusBar,
    private auth: AuthService,
    public rutValidator: RutValidator
  ){
    this.appInvite = this.service.appInvite;
  	this.login = {
  		rut: '',
      password: '',
      level: '1,5'
    };

  }

  lostPassword() {
    this.navCtrl.push(LostPasswordPage);
  }

  next() {

  	if (this.login.rut == "") {
      if (this.appInvite) {
        this.service.logError({}, "Email no puede estar vacío o es incorrecto");
      }
      else {
        this.service.logError({}, "Rut no puede estar vacío o es incorrecto");
      }
  	}
  	else if (this.login.password == "") {
  		this.service.logError({}, "La contraseña no puede estar vacía");
  	}
  	else {
      this.loading = this.loadingCtrl.create();
      this.loading.present();

      this.auth.login(this.login).subscribe((data: any) => {
          this.loading.dismiss();
          if (data.res == "OK"){
            this.auth.setToken(data.token);
            this.storage.set("MP-Profile", data.profile);

            if (data.profile.level == "1") {
              this.navCtrl.setRoot(FarmaciaPage);
            }
            else if (data.profile.level == "5") {
              this.navCtrl.setRoot(HomePage);
            }

          }
          else {
            this.service.logError({}, data.msg);
          }
        },
        err => {
          this.loading.dismiss();
          this.service.logError({});
        }
      );

  	}

  }

  gotoSignup(){
    if (this.appInvite) {
      this.navCtrl.push(SignupPage2, {
        rut: '27'
      });
    }
    else {
      this.navCtrl.push(SignupPage);
    }
  }

  noRegistered() {
    this.loading = this.loadingCtrl.create();
    this.loading.present();
    this.auth.login({
      rut: '666',
      password: '666',
      level: '1,5'
    }).subscribe(
      (data: any) => {
        this.loading.dismiss();
        if (data.res == "OK"){
          this.auth.setToken(data.token);
          this.storage.set("MP-Profile", data.profile);

          if (data.profile.level == "1") {
            this.navCtrl.setRoot(FarmaciaPage);
          }
          else if (data.profile.level == "5") {
            this.navCtrl.setRoot(HomePage);
          }
          
        }
        else {
          this.service.logError({}, data.msg);
        }
      },
      err => {
        this.loading.dismiss();
        this.service.logError({});
      }
    );
  }

}

