import { Component } from '@angular/core';
import { FormBuilder } from "@angular/forms";
import { Platform, LoadingController, ToastController, AlertController } from 'ionic-angular';
import { SMS } from '@ionic-native/sms';
import { TranslateService } from "@ngx-translate/core";

import { CustomerService } from '../../services/customer-service';

@Component({
	selector: 'page-home',
	templateUrl: 'home.html'
})
export class HomePage {
	public select_option: string = "information-tab";
	public isAndroid: boolean = false;
	public dataLoaded: boolean = true;
	public userInformation: any;
	public informations: any;
	
	constructor(
		private platform: Platform, private formBuilder: FormBuilder, private loadingCtrl: LoadingController,
		private customerService: CustomerService, private toastCtrl: ToastController, private translate: TranslateService,
		private alertCtrl: AlertController, private sms: SMS
	) {
		this.userInformation = this.formBuilder.group({
			informations: this.formBuilder.group(
				{
					name: [''],
					sex: [''],
					email: [''],
					phone: [''],
					address: [''],
					birthday: [],
				}
			),
			skills: this.formBuilder.group(
				{

				}
			),
			projects: this.formBuilder.group(
				{

				}
			)
		});

		this.isAndroid = platform.is('android');

		let loading = this.loadingCtrl.create();
		loading.present().then(() => {
			this.customerService.getSampleData({}).subscribe(data => {
				loading.dismiss().then(() => {
					if (data.success) {
						this.informations = data.informations;
						if (data.informations.gender == 0) 
 else {
							this.informations.gender = 'Female';
						}
					} else {
						let alert = this.alertCtrl.create({
							title: this.translate.instant('Notification'),
							subTitle: this.translate.instant(data.message),
							buttons: [this.translate.instant('OK')]
						});
						alert.present();
					}
				});
			}, (err) => {
				loading.dismiss().then(() => {
					this._presentToast();
				});
			});
		});
	}

	sendMessage() {
		this.sms.send(this.informations.telephone, 'Hello Tony!, ');
	}

	makeCall() {
		console.log('makeCall');
	}

	sendEmail() {
		console.log('sendEmail');
	}

	protected _presentToast() {
		let toast = this.toastCtrl.create({
			message: this.translate.instant('Connection failed.'),
			duration: 3000,
			position: 'bottom'
		});
		toast.present();
	}
}

{
							this.informations.gender = 'Male';
						}