import { Component } from '@angular/core';
import { NavController, NavParams, ActionSheetController } from 'ionic-angular';
// import { Product } from '../../models/product'
import { DataProvider } from '../../providers/data-provider'
import { CameraProvider } from '../../providers/camera-provider';
import { PreferencesProvider } from '../../providers/preferences-provider';

@Component({
    selector: 'page-product-details',
    providers: [CameraProvider],
    templateUrl: 'product-details.html'
})
export class ProductDetailsPage {

    product = {
        company_id: 0,
        default_code: null,
        description: null,
        ean13: null,
        image_medium: null,
        image_small: null,
        name: null,
        list_price: 1,
        purchase_ok: true,
        qty_available: 0,
        rental: false,
        sale_ok: true,
        standard_price: 0,
        type: "product",
        website_published: false,
        remote_id: 0,
        doc_state: "created"
    }

    constructor(
        public navCtrl: NavController,
        public params: NavParams,
        public data: DataProvider,
        public actionSheetCtrl: ActionSheetController,
        public cameraProvider: CameraProvider,
        public preferencesPreferences: PreferencesProvider
    ) { 
        this.initialize();
    }

    /**
     *
     */
    private initialize() {
        if (!(this.params.data instanceof Array)) {
            this.product = this.params.data;
            this.product.doc_state = "updated";
        }
    }

    /**
     *
     */
    private isValidForm() {
        if (!this.product.name) {
            return false;
        }

        return true;
    }

    /**
     *
     */
    showPhotoOptions() {
        this.actionSheetCtrl.create({
            title: "Tomar una foto",
            buttons: [
                {
                    text: "Desde la cámara",
                    icon: "camera",
                    handler: () => {
                        this.takePicture("camera");
                    }
                },
                {
                    text: "Desde la galería",
                    icon: "images",
                    handler: () => {
                        this.takePicture("album");
                    }
                },
                {
                    text: "Cancelar",
                    role: "cancel"
                }
            ]
        }).present();
    }

    /**
     *
     */
    takePicture(source: string): void {
        this.cameraProvider.getPicture(source).then(i => {
            this.product.image_medium = i;
            this.product.image_small = i;
        }).catch(e => {
            console.log(e);                    
        });
    }

    /**
     *
     */
    save(): void {
        if (!this.isValidForm()) {
            return;
        }

        this.product.company_id = this.preferencesPreferences.getCompany();
    
        this.data.save('product', this.product).then(result => {
            if (this.params.data instanceof Array) {
                this.params.data.push(result.products[0]);
            }
            
            this.navCtrl.pop(this);
        }).catch(e => {
            console.log(e);
        });
  
    }
}
