import { Router } from '@angular/router';
import {Component, OnInit, ViewChild} from '@angular/core';
import {ToastController} from "@ionic/angular";
import {NavController} from "@ionic/angular";
import { WrenchService } from 'src/app/services/wrench.service';
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import { environment} from "../../../environments/environment";

@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
  @ViewChild('username') username;
  @ViewChild('password') password;
  @ViewChild('firstname') firstname;
  @ViewChild('lastname') lastname;
  @ViewChild('country') country;

  @ViewChild('random_text') random_text;

  pending_uid:string = '';
  signup_state:number = 1;
  showSocial:boolean = true;
  /*
    'firstname': 'OluNG' ,
  'lastname': 'AmeyNG' ,
  'email': 'ameye+aug6160@chiefsoft.com' ,
  'username': 'ameye+aug6160@chiefsoft.com' ,
  'password': 'may12002' ,
  'terms' => 1,
  'news' => int 1
  'loc': '38.101.241.200' ,
  'country': 'NG' ,
  'action' => 11010
   */

  signupData: {
    action:11010,
    firstname: string,
    lastname: string,
    username: string,
    email: string,
    password: string,
    terms: number,
    news: number,
    country: string,
    sessionid: string
  };

  constructor(
      private router: Router,
      private toastController: ToastController,
      private navctr: NavController,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService
  ) {

  }

  ngOnInit() {
    this.showSocial =  environment.loginSocial;
  }

  onLogin() {
    this.router.navigate(['login']);
  }
  signUpResult:any;
  signUpData: [];

  signup_country :string='';
  signup_username :string='';

  onFillProfile() {
    if (this.username == null || this.username == '' || this.validateEmail(this.username) == false
        || this.password == null || this.password == ''
        || this.firstname == null || this.firstname == ''
        || this.lastname == null || this.lastname == ''
        || this.country == null || this.country == ''
    ) {
      this.presentToast('Missing Required Inputs', 'middle');
      return;
    }

    this.signupData= {
      action:11010,
      firstname:  this.firstname,
      lastname: this.lastname,
      username:  this.username,
      email:  this.username,
      password:  this.password,
      terms: 1,
      news: 1,
      country:  this.country,
      sessionid: "SSSSS-SSSSSS-SSSSSS"
    };
    this.pending_uid = "";

    this.wrenchService.createUser(this.signupData).subscribe(
        signUpResult => {
          this.signUpResult = signUpResult;
          console.log("signUpResult RETURN->", this.signUpResult);

          if (this.signUpResult.internal_return > 0 &&  this.signUpResult?.uid != ''){
            this.pending_uid = this.signUpResult.uid;
            this.signup_country =  this.signUpResult.country;
            this.signup_username =  this.signUpResult.username;
            this.random_text = '';
            this.signup_state  =2;

            /*
            {
    "action": "11010",
    "added": "2023-08-22 19:28:05.991937",
    "country": "NG",
    "email": "ses66181+x1234@gmail.com",
    "expire": "2023-09-21 19:28:05.991937",
    "firstname": "Olu",
    "id": "610",
    "lastname": "Ameye",
    "loc": "10.10.33.15",
    "mobile": "MOBILE",
    "news": "1",
    "password": "d0fbea2563b377ea7074bced45c88dcb",
    "pending_id": "610",
    "phone": "",
    "pid": "100",
    "sessionid": "SSSSS-SSSSSS-SSSSSS",
    "signup_random": "13635",
    "status": "1",
    "street1": "",
    "terms": "1",
    "uid": "07529070-79a6-4e34-835d-a3a50283e93d",
    "username": "ses66181+x1234@gmail.com",
    "verify_link": "WRENCHB-1176229640-1742094704-417159648",
    "zipcode": "",
    "internal_return": 610
}
             */
          }
          else {
            this.presentToast('Unable to continue with this params', 'middle');
          }
        }
    );
  }

  completeSignupData: {
    action:11010,
    username: string,
    random_text: string,
    password: string,
    pending_uid:string,
    mobile: string,
    sessionid: string
  };

  onCompleteProfile(){
    this.completeSignupData = {
      action:11010,
          username: this.signup_username,
          random_text: this.random_text,
          password: this.password,
          pending_uid: this.pending_uid,
          mobile: "MOBILE",
          sessionid: "ABOUT-TO_START"
    };
    console.log("COMPLETE SIGNUP ",this.completeSignupData);

    this.wrenchService.completeMobileSignup(this.completeSignupData).subscribe(
        signUpResult => {
          this.signUpResult =  signUpResult;
          if (signUpResult != null && signUpResult.internal_return == 100 && this.sessionDataProviderService.ConstructGlobalSessionData(this.signUpResult) == true) {
           // this.getBlogData();
            this.router.navigate(['tabs/tab1']);
          }
          else{
            this.showAlert("Sign up error","Unable to complete the sign up, please try again");
          }
        }

    );
  }
  showAlert(mtitle: string, amessage: string) {
    /* let alert = this.alertCtrl.create({
       title: mtitle,
       subTitle: amessage,
       buttons: ['OK']
     });
     alert.present();*/
    alert(amessage);
  }
  validateEmail(email) {
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
  }
  async presentToast(amessage, position: 'top' | 'middle' | 'bottom') {
    const toast = await this.toastController.create({
      message: amessage,
      duration: 1500,
      position: position,
    });

    await toast.present();
  }
}
