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/webservices/fixmyauto/webservice/';
	//webservice_url = 'http://techlabz.in/fixmyauto/webservice/';
	//webservice_url = 'http://www.garnedo.com/fixmyauto/webservice/';
	  webservice_url = 'http://192.168.140.22/fixmyauto/webservice/';
	
	loader : any;
	alert : any;
	post_url : any;
	data : any;
	result:any;
	reg_data:any;
  
	constructor(public http: Http, public loadingCtrl: LoadingController, public alertCtrl: AlertController, public events: Events,public storage:Storage,private transfer: FileTransfer, private file: File, private imagePicker: ImagePicker ) {
	//	const fileTransfer: TransferObject = this.transfer.create();
		this.storage.get('reg_data').then((reg_data) => {
			this.reg_data=reg_data;
			console.log("reg data", this.reg_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){
		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_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_new() {
		this.http.get('https://randomuser.me/api/?results=10')
		.map(res => res.json())
		.subscribe(data => {
			this.data = data;
			console.log(this.data);
		});
	}
	  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);
				
			}
        }).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;
  }
}
