import {LoadingController, NavController} from '@ionic/angular';
import {Component, OnInit, ViewChild} from '@angular/core';
import { SessionDataProviderService } from 'src/app/store/session-data-provider.service';
import { WrenchService } from 'src/app/services/wrench.service';
import {Camera, CameraResultType, CameraSource} from "@capacitor/camera";

@Component({
  selector: 'app-edit-profile',
  templateUrl: './edit-profile.page.html',
  styleUrls: ['./edit-profile.page.scss'],
})
export class EditProfilePage implements OnInit {

  // @ViewChild('firstname') firstname;
  // @ViewChild('lastname') lastname;
  // //@ViewChild('lastname') lastname;
  // @ViewChild('email') email;
  // @ViewChild('online_name') online_name;

  constructor(
    private navctr: NavController,
    public sessionDataProviderService:SessionDataProviderService,
    private loadingCtrl: LoadingController,
    private wrenchService: WrenchService,
  ) {
    // this.firstname = this.sessionDataProviderService.firstname;
    // this.lastname =  this.sessionDataProviderService.lastname;
    // this.email = this.sessionDataProviderService.email;
    // this.online_name = this.sessionDataProviderService.online_name;
  }

  firstname:string='';
  lastname:string='';
  email:string='';
  online_name:string='';
  profilePicture:string='';

  ngOnInit() {
    this.firstname = this.sessionDataProviderService.firstname;
    this.lastname =  this.sessionDataProviderService.lastname;
    this.email = this.sessionDataProviderService.email;
    this.online_name = this.sessionDataProviderService.online_name;
    this.profilePicture = this.sessionDataProviderService.profilePicture(this.sessionDataProviderService.session, this.sessionDataProviderService.member_uid);
  }

  onBack() {
    this.navctr.back();
  }
/*
'member_id' => int 1
  'sessionid' => string '936571FD2E081B667930E7FFDF4D819938171883CA0CC951DC6C4FB184280EB3' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'action' => int 15031
  'firstname' => string 'First68' (length=7)
  'lastname' => string 'Last32' (length=6)
  'email' => string 'ses66181+new39@gmail.com' (length=24)
  'state' => string 'Georgia' (length=7)
  'city' => string 'Atlanta3' (length=8)
  'online_name' => string 'Online Name61' (length=13)
  'pref_email' => int 1
  'pref_phone' => int 1
  'promo' => int 1
 */

  profileData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    firstname: string,
    lastname: string,
    email: string,
    state: string,
    city: string,
    online_name: string,
    pref_email: number,
    pref_phone: number,
    promo: number,
  };

  profileResult:any;
  async sendUpdateProfile(){
    const loading = await this.loadingCtrl.create({
      message: 'Uploading Profile',
      duration: 5000,
    });

    this.profileData = {action:15031,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      firstname: this.firstname,
      lastname: this.lastname,
      email: this.email,
      state: this.sessionDataProviderService.user_state,
      city: this.sessionDataProviderService.city,
      online_name: this.sessionDataProviderService.online_name,
      pref_email: this.sessionDataProviderService.pref_email,
      pref_phone: this.sessionDataProviderService.pref_phone,
      promo: this.sessionDataProviderService.promo,
    }

    loading.present();
    this.wrenchService.updateProfile(this.profileData).subscribe(
        profileResult => {
          this.profileResult = profileResult;
          console.log("profileResult  RETURN->", this.profileResult);
        }
    );

  }

  uploadData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    msg_type:'FILE',
    file_name: string,
    file_size: number,
    file_type: string,
    file_data: string
  };

  uploadResult:any;
  async startCamera(){
    const image = await Camera.getPhoto({
      quality: 50,
      width : 200,
      height : 200,
      allowEditing: false,
      resultType: CameraResultType.Base64,
      source: CameraSource.Camera // Camera, Photos or Prompt!
    });

    if (image) {
      const loading = await this.loadingCtrl.create({
        message: 'Uploading Profile',
        duration: 5000,
      });
      var file_size = parseInt(String(image.base64String.toString().length / 1.3224954) ) ;
      // this.saveImage(image)
      //debugger;
      //image.base64String
      this.uploadData={
        action:11300,
        member_id: this.sessionDataProviderService.member_id,
        uid: this.sessionDataProviderService.member_uid,
        sessionid: this.sessionDataProviderService.session ,
        msg_type:'FILE',
        file_name: `family-banner.${image.format}`,
        file_size: file_size,
        file_type: `image/${image.format}`,
        file_data: image.base64String
      };
      //console.log(image);
      loading.present();
      this.wrenchService.uploadFile(this.uploadData).subscribe(
          uploadResult => {
            this.uploadResult = uploadResult;
            console.log("this.wrenchService.uploadFile RETURN DATA->", this.uploadResult);
          }
      );

      console.log( this.uploadData);
    }
  }

}
