import { Component, OnInit,Input } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { AuthService } from '../services/auth.service';
import { environment } from '../../environments/environment.dev';
@Component({
  selector: 'app-pagenotfound',
  templateUrl: './pagenotfound.component.html',
  styleUrls: ['./pagenotfound.component.css']
})
export class PagenotfoundComponent implements OnInit {

 myFile: File;
  //myFile:File; /* property of File type */

  url:string
  contentType:string
  mettleReport:string
  imageUrl: string = environment.imageUrl;
  constructor(private AuthService:AuthService,public sanitizer: DomSanitizer) {


  //  this.getFiles()

  }

  ngOnInit() {


   }



  fileChange(files: any){
    console.log(files);
    var json = {
      file:this.myFile = files[0],
      name: files[0].name
    }

    console.log(this.myFile);
    console.log(json);

    const formData: any = new FormData();
    const file: Array<File> = files[0]

    formData.append("file", files[0], files[0]['name']);



    this.AuthService.getFileUpload(formData).then(resp =>{

      console.log(resp)





    }).catch(err =>{

    })



  }



   blobToFiles(theBlob, fileName){
    //A Blob() is almost a File() - it's just missing the two properties below which we will add
    theBlob.lastModifiedDate = new Date();
    theBlob.name = fileName;
    return theBlob;
  }


   dataURItoBlob(dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
      byteString = atob(dataURI.split(',')[1]);
    else
    //  byteString = unescape(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to a typed array
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
    }

    return new Blob([ia], {
      type: mimeString
    });
  }

  getFiles(){

    this.AuthService.getFile(17).then(resp =>{


      console.log(resp,'getAllFileDetasilssss');

      this.mettleReport = 'data:'+resp.contentType+';base64,'+resp.fileContent
      this.contentType = resp.contentType

    }).catch(err =>{

    })


  }

   base64ToArrayBuffer(base64) {
    var binaryString =  window.atob(base64);
    var binaryLen = binaryString.length;
    var bytes = new Uint8Array(binaryLen);
    for (var i = 0; i < binaryLen; i++)        {
      var ascii = binaryString.charCodeAt(i);
      bytes[i] = ascii;
    }
    return bytes;
  }



}
