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

@Component({
  selector: 'app-myfiles',
  templateUrl: './myfiles.page.html',
  styleUrls: ['./myfiles.page.scss'],
})
export class MyfilesPage implements OnInit {
@ViewChild('title') title;
@ViewChild('description') description;

  tabs = 'myfiles';
    session_image_server:string='';
    curr_session:string="";
    isDisabled:boolean = true;

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


  }


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

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

  onMentor() {
    this.router.navigate(['mentor']);
  }

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

  usrData: {
    action:number, member_id: number, uid: string, sessionid: string, limit:20, page:1
  };
 myFilesTotalData:any;
  myFilesData: [];

  getMyFiles(){
    this.usrData = {action:11309,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1}

    this.wrenchService.getMyFiles(this.usrData).subscribe(
        myFilesTotalData => {
          this.myFilesTotalData = myFilesTotalData;
          console.log("myFilesData  RETURN->", this.myFilesTotalData);
          this.myFilesData = this.myFilesTotalData.result_list;
          // debugger;

          console.log("myFilesData RETURN DATA->", this.myFilesData);
        }
    );
  }

    inVerify(){
      this.isDisabled = true;
      if ( this.title !='' && this.description !=''){
          this.isDisabled = false;
      }
    }

    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,
        title:string,
        description: 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 Banner',
                duration: 5000,
            });
            var file_size = parseInt(String(image.base64String.toString().length / 1.3224954) ) ;
            // this.saveImage(image)
            //debugger;
            //image.base64String
            this.uploadData={
                action:11307,
                member_id: this.sessionDataProviderService.member_id,
                uid: this.sessionDataProviderService.member_uid,
                sessionid: this.sessionDataProviderService.session ,
                msg_type:'FILE',
                file_name: `myfile.${image.format}`,
                file_size: file_size ,
                file_type: `image/${image.format}`,
                file_data: image.base64String,
                title: this.title,
                description: this.description
            };
            //console.log(image);
            loading.present();
            this.wrenchService.uploadFile(this.uploadData).subscribe(
                uploadResult => {
                    this.uploadResult = uploadResult;
                    console.log("this.wrenchService.uploadFile RETURN DATA->", this.uploadResult);
                    this.title='';
                    this.description ='';
                    this.getMyFiles();
                }
            );

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