import { Injectable } from '@angular/core';
import { DataProvider } from '../data-provider';
import { OdooProvider } from '../odoo-provider';

@Injectable()
export class ProductTemplateSyncProvider {

    constructor(
        public dataProvider: DataProvider,
        public odooProvider: OdooProvider
    ) { }

    /**
     *
     */
    doSync(): Promise<any> {
        return new Promise((resolve, reject) => {
            this.getCreatedProducts().then(products => {
                return products;
            }).then(products => {
                return this.odooProvider.post("product_template", products);
            }).then(() => {
                console.log('product_template: new products sended');    
                return this.getUpdatedProducts();
            }).then(products => {
                console.log(products);
                    
                return this.odooProvider.put("product_template", products);
            }).then(response => {
                console.log('product_template: modified products sended');   
                return this.getDeletedProducts();
            }).then(products => {
                return this.odooProvider.delete("product_template", products);
            }).then(() => {
                console.log('product_template: deleted products sended');
                return this.odooProvider.get("product_template");
            }).then(products => {
                return this.storeProducts(products);
            }).then(() => {
                console.log('product_template: products downloaded');
                resolve(true);    
            }).catch(e => { 
                reject(e);
            });
        });
    }

    /**
     *
     */
    private getCreatedProducts(): Promise<any> {
        return new Promise((resolve, reject) => {
            this.dataProvider.getAll(DataProvider.DOCS.PRODUCT_TEMPLATE).then(templates => {
                resolve(templates.filter(item => {
                    return item.doc_state === "created" || (item.doc_state === "updated" && !item.remote_id);
                }));
            }).catch(e => {
                reject(e);
            });
        });
    }

    /**
     *
     */
    private getUpdatedProducts(): Promise<any> {
        return new Promise((resolve, reject) => {
            this.dataProvider.getAll(DataProvider.DOCS.PRODUCT_TEMPLATE).then(templates => {
                let products = templates.filter(item => {
                    return item.doc_state === "updated" && item.remote_id;
                });

                resolve(products.map(item => {
                    delete item.id;
                    return item;
                }));
            }).catch(e => {
                reject(e);
            });
        });
    }

    /**
     *
     */
    private getDeletedProducts(): Promise<any> {
        return new Promise((resolve, reject) => { 
            this.dataProvider.getAll(DataProvider.DOCS.PRODUCT_TEMPLATE).then(templates => {
                let products = templates.filter(item => { 
                    return item.doc_state === "deleted";
                });
                
                resolve(products.map(item => {
                    delete item.id;
                    return item;
                }));
            }).catch(e => { 
                reject(e);
            });
        });
    }

    /**
     *
     */
    private storeProducts(products: Array<any>): Promise<any> {
        return new Promise((resolve, reject) => {
            this.dataProvider.deleteAll(DataProvider.DOCS.PRODUCT_TEMPLATE).then(() => {
                return;
            }).then(() => {
                return this.dataProvider.save(DataProvider.DOCS.PRODUCT_TEMPLATE, products.map(item => {
                    item.remote_id = item.id;
                    item.doc_state = "sync";
                    delete item.id;

                    return item;
                }));
            }).then(() => { 
                resolve();
            }).catch(e => { 
                reject(e);    
            });
        });
    }
}