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

@Injectable()
export class PartnerSyncProvider {

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

    /**
     *
     */
    doSync(): Promise<any> {
        return new Promise((resolve, reject) => {
            return new Promise((resolve, reject) => {
                this.getCreatedPartners().then(partners => {
                return partners;
            }).then(partners => {
                return this.odooProvider.post("partner", partners);
            }).then(() => {
                console.log('partners: new partners sended');    
                return this.getUpdatedPartners();
            }).then(partners => {
                return this.odooProvider.put("partner", partners);
            }).then(response => {
                console.log('partner: modified partners sended');   
                return this.getDeletedPartners();
            }).then(partners => {
                return this.odooProvider.delete("partner", partners);
            }).then(() => {
                console.log('partner: deleted partners sended');
                return this.odooProvider.get("partner");
            }).then(partners => {
                return this.storePartners(partners);
            }).then(() => {
                console.log('partner: partners downloaded');
                resolve(true);    
            }).catch(e => { 
                reject(e);
            });
        });
        });
    }

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

    /**
     *
     */
    private getUpdatedPartners(): Promise<any> {
        return new Promise((resolve, reject) => {
            this.dataProvider.getAll(DataProvider.DOCS.PARTNER).then(partners => {
            
                resolve(partners.filter(item => {
                    return item.doc_state === "updated" && item.remote_id;
                }).map(item => {
                    delete item.id;
                    return item;
                }));
                
            }).catch(e => {
                reject(e);
            });
        });
    }

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

    /**
     *
     */
    private storePartners(partners: 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, partners.map(item => {
                    item.remote_id = item.id;
                    item.doc_state = "sync";
                    delete item.id;

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

}