import { Component, OnInit } from '@angular/core';
import {LoadingController, NavController} from "@ionic/angular";
import {Router} from "@angular/router";
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {WrenchService} from "../../services/wrench.service";
import { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera';
import {compareNumbers} from "@angular/compiler-cli/src/version_helpers";

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

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


  }
  member_uid:string='';
  curr_session:string='';
  session_image_server:string='';

  ngOnInit() {
    this.member_uid=  this.sessionDataProviderService.member_uid;
    this.curr_session =  this.sessionDataProviderService.session;
    this.session_image_server = this.sessionDataProviderService.session_image_server;
  }
  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 : 600,
      height : 300,
      allowEditing: false,
      resultType: CameraResultType.Base64,
      source: CameraSource.Photos // Camera, Photos or Prompt!
    });

    if (image) {
      const loading = await this.loadingCtrl.create({
        message: 'Uploading Banner',
        duration: 5000,
      });
      var file_size = parseInt(String(image.base64String.toString().length / 1.3224954) ) ;
      // this.saveImage(image)
      //debugger;
      //image.base64String
      this.uploadData={
        action:11304,
        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);
    }
  }

  onBack() {
    this.navctr.back();
  }
}
