import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,Events } from 'ionic-angular';
import { FormGroup, Validators, FormBuilder } from '@angular/forms'
import { Myservice } from '../../providers/myservice'
import { Storage } from '@ionic/storage'
import { driver } from '../../models/mymodel'
import { Toast } from '@ionic-native/toast';
import { TranslateService } from "@ngx-translate/core";

@IonicPage()
@Component({
  selector: 'page-changepass',
  templateUrl: 'changepass.html',
})
export class ChangepassPage {
  pswdForm: FormGroup
  local: driver
  lang: string = 'en';

  constructor(public navCtrl: NavController, public navParams: NavParams, private myservice: Myservice, public storage: Storage, private formbuilder: FormBuilder, private toast: Toast, private translate: TranslateService, public events:Events) {

    this.pswdForm = this.formbuilder.group({
      current: ['', Validators.compose([Validators.required, Validators.minLength(6)])],
      new: ['', Validators.compose([Validators.required, Validators.minLength(6)])],
      confirm: ['', Validators.compose([Validators.required]), this.MatchPassword.bind(this)],
      id: ['']
    })

    this.events.subscribe('driver:lang', data => {
      setTimeout(() => {
        this.langTrans();
      }, 500);
    })
  }

  ionViewDidEnter() {
    this.langTrans()

    this.storage.get('driver_data').then(data => {
      if (data != null) {
        this.local = data
        this.pswdForm.controls['id'].setValue(this.local.id)
      }
    })
  }

  langTrans() {
    this.storage.get('lang').then(lang => {
      if (lang != null) {
        this.translate.use(lang)
        this.lang = lang;
      }
      else {
        this.translate.use('en')
      }
    })
  }

  change() {
    this.myservice.show_loader();
    this.myservice.load_post(this.pswdForm.value, 'changePassword').subscribe(response => {
      this.myservice.hide_loader()
      if (response.status == 'success') {
        this.pswdForm.reset();
        this.translate.get(['changepass.' + response.message]).subscribe(value => {
          this.toast.show(value['changepass.' + response.message], '2000', 'center').subscribe(toast => {
          console.log(toast);
        });
      })
      }
      else {
        this.pswdForm.reset();
        this.translate.get(['changepass.' + response.message]).subscribe(value => {
          this.myservice.show_alert('', value['changepass.' + response.message])
        })
      }
    })
  }

  back() {
    this.navCtrl.pop();
  }

  //for checking new pswd and confirm are same or not
  MatchPassword(val) {
    return new Promise(resolve => {
      if (val.value == this.pswdForm.value.new) {
        resolve(null);
      }
      else {
        resolve({ 'match': false });
      }
    })
  }

  //for checking current pswd and new pswd are same or not
  crnt_new_check(val) {
    return new Promise(resolve => {
      if (val.value == this.pswdForm.value.current) {
        resolve({ 'match': false });
      }
      else {
        resolve(null);
      }
    })
  }

}
