import { Component, OnInit } from '@angular/core';
import {AlertController, NavController} from "@ionic/angular";
import {Router} from "@angular/router";
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {WrenchService} from "../../services/wrench.service";
import {RecipientsService} from "../../store/recipients.service";

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

  walletData:any;
  tabs = 'bank_redeem';
  curr_balance:number =0;
  currency_description : string='';
  currency_code:string='';
  wallet_uid:string='';
  isDisabled:boolean=false;

  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private alertController: AlertController,
      public recipientsService: RecipientsService
  ) {
    this.walletData  =  this.router.getCurrentNavigation().extras.state;
    if ( this.walletData != undefined ){
      this.curr_balance = this.walletData.amount*0.01;
      this.currency_code = this.walletData.code;
      this.currency_description = this.walletData.description;
      this.wallet_uid= this.walletData.wallet_uid;
    }
    else{
      this.onBack();
    }
  }

  myRecipientsData:any;
  ngOnInit() {
    //this.recipientsService.getMyRecipientsData();
    this.myRecipientsData =  this.recipientsService.getMyRecipientsData();
    this.getPaymentHxData();
    //this.getMyRecipientsData();
    this.myRecipientsData =  this.recipientsService.myRecipientsData;
  }

  ionViewDidEnter(){
    console.log('Home page did enter - > ionViewDidEnter ');
    //this.recipientsService.getMyRecipientsData();
    this.getMyRecipientsData();
  }

  ionViewWillEnter(){
    console.log('Home page did enter - > ionViewWillEnter');
    //this.recipientsService.getMyRecipientsData();
    this.myRecipientsData =  this.recipientsService.myRecipientsData;
  }

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

  usrData: {
    action:number, member_id: number,
    uid: string,
    sessionid: string,
    limit:20, page:1,
    offset: 0,
    amount: number
  };
  paymentHxData:any;
  paymentData: [];
  getPaymentHxData(){
    this.usrData = {action:11200, member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid, sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0, amount: 0}
    this.wrenchService.getPaymentHx(this.usrData).subscribe(
        paymentHxData => {
          this.paymentHxData = paymentHxData;
          console.log("PAH HX RETURN->", this.paymentHxData);
          this.paymentData = this.paymentHxData.result_list;
          // debugger;
          console.log("BANNERS RETURN DATA->", this.paymentData);
          //  this.blogDataService.setBlogData(this.blogResult.blog_data);
        }
    );

  }

  /*
array (size=5)
  'member_id' => int 1
  'sessionid' => string '936571FD2E081B667930E7FFDF4D819938171883CA0CC951DC6C4FB184280EB3' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'amount' => int 816
  'action' => int 33025
/var/www/html/public/wrenchboard/wrenchboard_class.php:209:
array (size=4)
  'processing_fee' => string '12008' (length=5)
  'result' => string 'YES I GET TO BACK END' (length=21)
  'total_amount' => string '12824' (length=5)
  'internal_return' => int 12008
   */

  amount:number =0;
  fee:number = 0;
  fee_display: string;
  total:number = 0;
  total_display: string;
  recipient:string ='';
  recipient_name:string ='';

  feeReturn: any;
  refreshFee(){
    this.total =  this.fee = 0;
    console.log("Refresh Fee******************");
    this.usrData = {action:33025,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0, amount: this.amount*100}
    if (this.amount <= 0 ) return;


    this.wrenchService.sendMoneyFee(this.usrData).subscribe(
        feeReturn => {
          this.feeReturn = feeReturn;
         if ( this.feeReturn != null && this.feeReturn.processing_fee >= 0 ){
           this.fee =  this.feeReturn.processing_fee*0.01;
           var num = new Number(this.fee);
           this.fee_display = num.toFixed(2);
          // parseFloat("123.456").toFixed(2);
           this.total =  this.feeReturn.total_amount*0.01;
           var numt = new Number(this.total);
           this.total_display= numt.toFixed(2);
         }
        }
    );


  //  this.total =  this.fee + this.amount;
  }



  curr_page:string= 'redeem_start'
  recipient_uid:string = '';
  contineRedeem(){
    if (this.recipient == ''){
      //alert("Select Recipient");
      this.showAlert("Error","Select Recipient");
      return;
    }
    if (this.amount <= 0 ) {
      this.showAlert("Error","Enter valid Amount");
      //alert("Enter Amount");
      return;
    }

    if (this.curr_balance < this.total){
      this.showAlert("Error","Enter Amount less than your wallet");
      //alert("Enter Amount less than your wallet");
      return;
    }

    const selRecipient = this.myRecipientsData.filter((item) => item.recipient_uid == this.recipient);
    if ( selRecipient.length == 1 && this.amount > 0){
      this.recipient_uid =  selRecipient[0].recipient_uid;
      this.recipient_name = selRecipient[0].recipient;
      this.curr_page ='redeem_confirm';
    }
  }

  checkSelectVal(){
    if (this.recipient == 'ADD_NEW_RECIP'){
      this.addNewBank();
      this.recipient='';
    }
  }
  async showAlert(mtitle: string, amessage: string) {
    const alert = await this.alertController.create({
      header: 'WrenchBoard',
      subHeader: mtitle,
      message: amessage,
      buttons: ['OK'],
    });

    await alert.present();
  }
  /*

  {
    "recipient": "Olu Ameye 0690000034 Access Bank",
    "account_no": "0690000034",
    "recipient_id": "3",
    "recipient_uid": "6bba49e2-dd92-403e-9459-927e37c852ef",
    "country": "NG",
    "added": "2017-03-22 11:55:25.133006",
    "bank_name": "Access Bank",
    "bank_uid": "a2c6c1a2-5cf2-4396-8630-50c47da15316"
}


array (size=9)
'member_id' => int 1
'sessionid' => string '936571FD2E081B667930E7FFDF4D819938171883CA0CC951DC6C4FB184280EB3' (length=64)
'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
'wallet_uid' => string 'cc0f8743-3f18-4214-ba4b-781e5dda9cb8' (length=36)
'amount' => int 240800
'Fee' => float 2408
'recipient_uid' => string 'bfd7dc27-e2a6-4d52-957b-789fdb660eec' (length=36)
'mode' => int 100
'action' => int 33020

this.usrData = {action:11200, member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid, sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0, amount: 0}
 */
  sendMoneyData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    wallet_uid:string,
    recipient_uid:string,
    amount:number,
    Fee:number,
    mode: 100,
  };

  sendMoneyResult:any;
  confirmation:string ='';
  confirmRedeem(){
    this.isDisabled = true;
    this.sendMoneyData = {
      action:33020,
          member_id: this.sessionDataProviderService.member_id,
          uid: this.sessionDataProviderService.member_uid,
          sessionid: this.sessionDataProviderService.session,
          wallet_uid: this.wallet_uid,
          recipient_uid: this.recipient_uid,
          amount: this.amount*100,
          Fee:this.fee*100,
          mode: 100,
    };

    this.wrenchService.sendMoneyAction(this.sendMoneyData).subscribe(
        sendMoneyResult => {
          this.sendMoneyResult = sendMoneyResult;
          console.log("sendMoneyResult  RETURN->", this.sendMoneyResult);
          this.confirmation = this.sendMoneyResult.confirmation;
          this.curr_page ='redeem_result';
        }
    );
  }

  myRecipientsTotalData:any;
  getMyRecipientsData(){
    this.usrData = {action:11175,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,
      offset: 0, amount: 0}

    this.wrenchService.recipientAccount(this.usrData).subscribe(
        myRecipientsTotalData => {
          this.myRecipientsTotalData = myRecipientsTotalData;
          console.log("myRecipientsTotalData  RETURN->", this.myRecipientsTotalData);
          this.myRecipientsData = this.myRecipientsTotalData.result_list;
          // debugger;
          console.log("myRecipientsData RETURN DATA->", this.myRecipientsData);
        }
    );
  }

  addNewBank(){
   // debugger;
    this.router.navigate(['addbank'],{state: this.walletData});
  }
}
