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-jobactive',
  templateUrl: './jobactive.page.html',
  styleUrls: ['./jobactive.page.scss'],
})
export class JobactivePage implements OnInit {
  @ViewChild('yourmessage') yourmessage;

  jobData: any;
  tabs = 'message';
  item_banner:string = 'default.jpg';
  session_image_server:string='';
  curr_session:string='';

  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private loadingCtrl: LoadingController
  ) {
    this.jobData  =  this.router.getCurrentNavigation().extras.state;
    this.item_banner = this.jobData.banner;
  }


  ngOnInit() {
    this.curr_session =  this.sessionDataProviderService.session;
    this.session_image_server =  this.sessionDataProviderService.session_image_server;
    //this.activeJobMsgList();
  }

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

  // contrData: {
  //   action:number,
  //   member_id: number,
  //   uid: string,
  //   sessionid: string,
  //   limit:20,
  //   contract:string,
  //   offset: 0
  // };
  // msgListTotalData:any;
  // msgListData: [];
  // activeJobMsgList(){
  //   this.contrData = {
  //     action:14011,
  //     member_id: this.sessionDataProviderService.member_id,
  //     uid: this.sessionDataProviderService.member_uid,
  //     sessionid: this.sessionDataProviderService.session ,
  //     limit:20,
  //     contract: this.jobData.contract,
  //     offset: 0}
  //
  //   this.wrenchService.activeJobMsgList(this.contrData).subscribe(
  //       msgListTotalData => {
  //         this.msgListTotalData = msgListTotalData;
  //         // console.log("REFER  RETURN->", this.referTotalData);
  //         this.msgListData = this.msgListTotalData.result_list;
  //         console.log("REFER RETURN DATA->", this.msgListData);
  //       }
  //   );
  //
  // }

  reqData: {
    action:number, member_id: number, uid: string, sessionid: string, msg_type:string,
    contract:string,
    message:string
  };

  interest_status:string = "";
  interest_msg_status:string = "";

  msgReqResult:any;
  async sendActiveJobMessage(){
    if (this.yourmessage == undefined || this.yourmessage?.length < 5){
      alert("Enter Message to send");
      return;
    }

    const loading = await this.loadingCtrl.create({
      message: 'Sending...',
      duration: 2500,
    });
    this.reqData = {action:14010,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      msg_type: "TEXT",
      contract:this.jobData.contract,
      message: this.yourmessage
    }

    loading.present();
    this.wrenchService.sendTaskMessage(this.reqData).subscribe(
        reqResult => {
          this.msgReqResult = reqResult;
          console.log("msgReqResult  RETURN->", this.msgReqResult);
          this.interest_msg_status =  this.msgReqResult?.status;
          //this.activeJobMsgList();
          const event = new Event("app-taskactivities-refresh");
          // Dispatch the event.
          dispatchEvent(event);

        }
    );
  }


  uploadData: {
    action:number,
    member_id: number,
    uid: string,
    contract: 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 updates',
        duration: 5000,
      });
      var file_size = parseInt(String(image.base64String.toString().length / 1.3224954) ) ;
      // this.saveImage(image)
      //debugger;
      //image.base64String
      this.uploadData={
        action:14010,
        member_id: this.sessionDataProviderService.member_id,
        uid: this.sessionDataProviderService.member_uid,
        contract: this.jobData.contract,
        sessionid: this.sessionDataProviderService.session ,
        msg_type:'FILE',
        file_name: `${this.jobData.contract}-job.${image.format}`,
        file_size: file_size ,
        file_type: `image/${image.format}`,
        file_data: image.base64String
      };
      //  console.log(this.uploadData);
      loading.present();
      this.wrenchService.uploadFile(this.uploadData).subscribe(
          uploadResult => {
            this.uploadResult = uploadResult;
            console.log("this.wrenchService.uploadFile RETURN DATA->", this.uploadResult);
            const event = new Event("app-taskactivities-refresh");
            // Dispatch the event.
            dispatchEvent(event);
          }
      );

      //  console.log( this.uploadData);
    }
  }

}
