import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";

import { PouchService } from "./pouch-service";
import { UploadService } from "../services/upload-service";
import { DownloadService } from "../services/download-service";

import { getMetadataStorage } from "../odoo/utils/metadata-storage";

import "rxjs/add/operator/concat";

@Injectable()
export class SyncService {

    constructor(
        public pouch: PouchService,
        public upload: UploadService,
        public download: DownloadService
    ) { }

    /**
     *
     */
    do(): Observable<any> {
        // return this.removeAll().concat(this.download.do());
        return this.upload.do();
    }

    /**
     *
     */
    removeAll(): Observable<any> {
        return Observable.from(getMetadataStorage().getModels()).concatMap(item => this.pouch.removeAll(item.model.document));
    }
}