import {Component, OnInit, ViewChild} 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} from "@capacitor/camera";

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

  @ViewChild('amount') amount;
  @ViewChild('description') description;

  familyMemberData: any;
  tabs = 'lesson';
  member_firstname:string='';
  member_lastname:string='';

  member_username:string='';
  member_pin:string='';
  family_uid:string='';
  jobManagerActiveData:any;
  session_image_server:string='';
  curr_session:string='';
 // family_member_uid:string='';
  member_qr_string ='0000000';

  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private loadingCtrl: LoadingController
  ) {
    this.familyMemberData  =  this.router.getCurrentNavigation().extras.state;
    //debugger;
    this.member_firstname= this.familyMemberData.itemData.firstname;
    this.member_lastname = this.familyMemberData.itemData.lastname;
    this.family_uid = this.familyMemberData.itemData.family_uid;
    this.jobManagerActiveData = this.familyMemberData.jobManagerActiveData;
  }

  ngOnInit() {
    this.session_image_server = this.sessionDataProviderService.session_image_server;
    this.curr_session=this.sessionDataProviderService.session;
    this.getThisFamilyAccount();
    this.getFamilyWalletsData();
  }

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

  jobActive(item){
    this.router.navigate(['jobactive'],{state: item});
  }
  onMentor() {
    this.router.navigate(['mentor']);
  }

  onEnroll() {
    this.router.navigate(['enroll']);
  }

  postData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    family_uid:string
  };

  familyMemberResult:any;
  familySuggestData:any;
  getThisFamilyAccount(){
    this.postData = {
      action:22025,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      family_uid: this.family_uid
    };
    this.wrenchService.familyManage(this.postData).subscribe(
        familyMemberResult => {
          this.familyMemberResult = familyMemberResult;
          console.log("familyMemberResult RETURN->", this.familyMemberResult);
          this.member_username = this.familyMemberResult.username;
          this.member_pin = this.familyMemberResult.pin;
         // this.member_qr_string = this.sessionDataProviderService.member_uid+"@"+this.member_username+"@"+this.member_pin; //member_uid@family_username@family_pin
          this.member_qr_string = this.sessionDataProviderService.member_uid+"@"+this.member_username+"@"+this.family_uid; //member_uid@family_username@family_pin
          console.log("this.member_qr_string    ---->  ",this.member_qr_string);
        }
    );
  }

  familyMemberWalletTotalData:any;
  familyMemberWalletData: [];
  getFamilyWalletsData(){
    this.postData = {
      action:22025,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      family_uid: this.family_uid
    };

    this.wrenchService.getFamilyWallets(this.postData).subscribe(
        familyMemberWalletTotalData => {
          this.familyMemberWalletTotalData = familyMemberWalletTotalData;
          console.log("familyMemberWalletTotalData  RETURN->", this.familyMemberWalletTotalData);
          this.familyMemberWalletData = this.familyMemberWalletTotalData.result_list;
          // debugger;
          console.log("FAMILY familyMemberWalletData DATA->", this.familyMemberWalletData);
          //this.suggestion_total = this.familyMemberWalletData.length;
        }
    );

  }

  toolBarShow:string="1";

  backToToolBar(){
    this.toolBarShow = "1";
    this.startSendData=null;
  }

  reqStart: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    family_uid: string,
    wallet_uid: string
  };

  startAddfund(item:any){
    // need to start ths process
    /*
  this.usrData = {action:11200, member_id: this.sessionDataProviderService.member_id, uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,'limit': 30,
    array (size=6)
  'member_id' => int 1
  'sessionid' => string 'C954B8DF32A5DDF37064DBA89A9C972604393E98A9806B9D5593055BB348D627' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'family_uid' => string '1f385837-b5e6-420e-a17a-488c4b8ef015' (length=36)
  'wallet_uid' => string 'aa6e6488-2e71-4f83-b265-27c3c4339f73' (length=36)
  'action' => int 22013
/var/www/html/public/wrenchboard/wrenchboard_class.php:215:
array (size=10)
  'currency' => string 'NAIRA' (length=5)
  'current_balance' => string '33617' (length=5)
  'exchange_rate' => string '1' (length=1)
  'origing_currency' => string 'NAIRA' (length=5)
  'origing_current_balance' => string '91799493' (length=8)
  'origing_transfer_limit' => string '500000' (length=6)
  'origing_wallet_uid' => string 'cc0f8743-3f18-4214-ba4b-781e5dda9cb8' (length=36)
  'recipient_wallet_uid' => string 'aa6e6488-2e71-4f83-b265-27c3c4339f73' (length=36)
  'status' => string 'OK' (length=2)
  'internal_return' => int 0
TARGET ENDPOINT[POST]http://10.204.5.100:9083/en/wrench/api/v1/familytransferstart

familyStartTransfer(reqData){
	return this.getPostData("familytransferstart", reqData);
}

familyTransfer(reqData){
	return this.getPostData("familytransfer", reqData);
}
     */
    this.reqStart = {
      action:22013,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      family_uid: this.family_uid,
      wallet_uid: item.wallet_uid
    };
   // debugger;

    this.wrenchService.familyStartTransfer(this.reqStart).subscribe(
        startSendData => {
          this.startSendData = startSendData;
          console.log("startSendData RETURN->", this.startSendData);

          this.origing_current_balance =  this.startSendData.origing_current_balance;
          this.send_cuurency = this.startSendData.currency;
          this.toolBarShow = item.wallet_uid;
          // if ( this.accountAddResult.internal_return != undefined && this.accountAddResult.internal_return > 0 ){
          //   this.presentToast("Recipient Accound Added",'middle');
          //   setTimeout(() => {
          //     this.onBack();
          //   }, 3000);
          // }
        }
    );



  }
  startSendData:any;
  origing_current_balance:number =0;
  send_cuurency:string='';

  reqSend: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    family_uid: string,
    origing_wallet_uid: string,
    wallet_uid: string,
    currency: string,
    amount: number,
    description:string,
    family_transfer_mode: number
  };

  sendResultData:any;

  sendReward(item){

    this.reqSend = {
      action:22014,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      family_uid: this.family_uid,
      origing_wallet_uid: this.startSendData.origing_wallet_uid,
      wallet_uid: item.wallet_uid,
      currency: this.startSendData.currency,
      amount: this.amount*100,
      description: this.description,
      family_transfer_mode: 100
    };

    this.wrenchService.familyTransfer(this.reqSend).subscribe(
        sendResultData => {
          this.sendResultData = sendResultData;
          console.log("sendResultData RETURN->", this.sendResultData);

          // if all is good
          setTimeout(()=>{
            this.toolBarShow = "1";
            this.startSendData=null;
          }, 3000);

        }
    );


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

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

    if (image) {
      const loading = await this.loadingCtrl.create({
        message: 'Uploading Profile Picture',
        duration: 5000,
      });
      var file_size = parseInt(String(image.base64String.toString().length / 1.3224954) ) ;
      // this.saveImage(image)
      //debugger;
      //image.base64String
      this.uploadData={
        action:11305,
        member_id: this.sessionDataProviderService.member_id,
        uid: this.sessionDataProviderService.member_uid,
        family_uid: this.family_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);
    }
  }
  startWalletPlan(){}
}
