import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
 
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
import { Storage } from '@ionic/storage';


import { LoadingController, AlertController, Events   } from 'ionic-angular';

/* import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
import { ImagePicker } from '@ionic-native/image-picker';
,private transfer: FileTransfer, private file: File, private imagePicker: ImagePicker
*/

@Injectable()
export class Myservice {

  
   //webservice_url = 'http://localhost/fixmyauto/Webservice_user/';
   // webservice_url = 'http://192.168.140.22/fixmyauto/Webservice_user/';
  // webservice_url = 'http://localhost/fixmyauto/Webservice_user/';
   webservice_url = 'http://techlabz.in/fixmyauto/Webservice_user/';
  // webservice_url = 'http://www.garnedo.com/fixmyauto/Webservice_user/';
  // webservice_url = 'https://104.198.77.184:8443/fixmyauto/Webservice_user/';
   
  loader : any;
  alert : any;
  post_url : any;
  data : any;
  result:any;
  user_data:any;
  notify:any;
  
  constructor(public http: Http, public loadingCtrl: LoadingController, public alertCtrl: AlertController, public events: Events,public storage:Storage ) {
	//const fileTransfer: TransferObject = this.transfer.create();
		this.storage.get('user_data').then((user_data) => {
		if(user_data){
			this.user_data =user_data;
		}
	})
  }
  
  show_loader() {
	this.loader = this.loadingCtrl.create({
      content: ""
    });
	this.loader.present();
  }
  
  
  presentLoadingCustom() {
  let loading = this.loadingCtrl.create({
    spinner: 'show',
    content: `
      <div class="custom-spinner-container">
        <div class="custom-spinner-box"></div>
      </div>
	  <h5>Please wait your image is being downloaded</h5>`,
    
  });

  loading.onDidDismiss(() => {
    console.log('Dismissed loading');
  });

  loading.present();
}
  
  show_alert(title, message) {
      this.alert = this.alertCtrl.create({
      title: title,
      subTitle: message,
      buttons: ['OK']
    });
    this.alert.present();
  }
      	fileUpload(data,fn_name){
		console.log(data);
		var post_url = this.webservice_url + fn_name; 
		return this.http.post(post_url, data) .map(res =>res.json()); 
	}
  
  hide_loader() {
	this.loader.dismissAll();
  }
  
 /* load_posts(post_data, fn_name) {
    
	post_data = JSON.stringify(post_data);
	
    this.post_url = this.webservice_url + fn_name;
	 
	 return this.http.post(`${this.post_url}`, `${post_data}`, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
      .map(res => res.json());
	    
  }
  
  */
 load_post(post_data, fn_name) {
 
     var post_url = this.webservice_url + fn_name;
 post_data = JSON.stringify(post_data);
let headers = new Headers(
{
  'Content-Type' : 'application/json'
});
let options = new RequestOptions({ headers: headers });
 return this.http.post(post_url, post_data, options)
      .map(res => res.json());
}
  
  
  load_get(fn_name) {
    
	
    this.post_url = this.webservice_url + fn_name;
	 
	 
	  return this.http.get(`${this.post_url}`).map(res => res.json());
  } 
  

   /* 
  load_post_file(imageURI, post_data, fn_name) {
	  console.log(imageURI);
        let ft = this.transfer.create();
        let filename = imageURI.substr(imageURI.lastIndexOf('/')+1);
        let options = {
            fileKey: 'file',
            fileName: filename,
            mimeType: 'image/jpeg',
            chunkedMode: false,
            headers: {
                'Content-Type' : undefined
            },
            params: post_data
        }; 
        //ft.onProgress(this.onProgress);
		this.show_loader();
        ft.upload(imageURI, this.webservice_url+fn_name, options, false)
        .then((result: any) => {
            //this.success(result);
			this.hide_loader();
			console.log(result);
			  var res =JSON.parse(result.response);
				if(res.status == "failed") {
	  			this.show_alert("Error!", result.message);
			}
			else {
				
				this.show_alert("Success!", res.message);
				console.log("result", JSON.parse(result.response)  );
				console.log("reg data post", this.reg_data);
				console.log("pro pic", res.result.profile_pic);
				this.reg_data.profile_pic = res.result.profile_pic;
					this.reg_data.first_name = res.result.first_name;
					this.reg_data.last_name = res.result.last_name;
					console.log(this.reg_data);
					this.events.publish('user:update_profile', this.reg_data);
			}
        }).catch((error: any) => {
            //this.failed(error);
			console.log(error);
			//this.result = error;
			this.hide_loader();
			this.show_alert("Error!", 'Error occured. Please try again');
        });
		//console.log(this.result); 
		//return this.result;
  }
  
load_post_file(imageURI, post_data, fn_name) {
        let ft = this.transfer.create();
        let filename = imageURI.substr(imageURI.lastIndexOf('/')+1);
        let options = {
            fileKey: 'file',
            fileName: filename,
            mimeType: 'image/jpeg',
            chunkedMode: false,
            headers: { 
                'Content-Type' : undefined
            },
            params: post_data
        }; 
        //ft.onProgress(this.onProgress);
        ft.upload(imageURI, this.webservice_url+fn_name, options, false)
        .then((result: any) => {
            //this.success(result);
			console.log("1 ", result.response);
			this.result = result.response;
        }).catch((error: any) => {
            //this.failed(error);
			console.log(error);
			this.result = error;
        });
		console.log("r ", this.result); 
		return this.result;
  }  */
  
  load_new() {
  	this.http.get('https://randomuser.me/api/?results=10')
      .map(res => res.json())
      .subscribe(data => {
        this.data = data;
        console.log(this.data);
      });   
  }
  

}
