import { Injectable } from "@angular/core";
import { OdooRPCService } from "angular2-odoo-jsonrpc";
import { Observable } from "rxjs/Observable";

import "rxjs/add/observable/fromPromise";

@Injectable()
export class OdooService {

    constructor(
        private odooRPC: OdooRPCService
    ) { }

    /**
     *
     */
    initialize(url: string, port?: number): void {
        let host = url;

        if (port) {
            host = host + ":" + port;
        }
        
        this.odooRPC.init({
            odoo_server: host,
            http_auth: "username:password"
        });
    }

    /**
     *
     */
    login(database: string, username: string, password: string): Observable<any> {
        return Observable.fromPromise(this.odooRPC.login(database, username, password));
    }

    /**
     *
     */
    logout(): Observable<any> {
        return Observable.fromPromise(this.odooRPC.logout());
    }

    /**
     * Example:
     * instance.searchRead('res.partner', [[['is_company', '=', true], ['customer', '=', true]]], { 'fields': ['name', 'comment']})
     */
    searchRead(model: string, domain: any, fields?: any): Observable<any> {
        return Observable.fromPromise(this.odooRPC.searchRead(model, domain, fields));
    }

    /**
     * Example:
     * instance.call('res.partner', 'search', [[['customer', '=', true], ['is_company', '=', false]]], { 'limit': 1})
     */
    call(model: string, method: string, args: any, kwargs?: any): Observable<any> {
        return Observable.fromPromise(this.odooRPC.call(model, method, args, kwargs));
    }

    /**
     * Example:
     * instance.search('res.partner', [[['customer', '=', true], ['is_company', '=', false]]], { 'limit': 1})
     */
    search(model: string, args: any, kwargs?: any): Observable<any> {
        return this.call(model, 'read', args, kwargs);
    }

    /**
     * Example:
     * instance.fieldsGet('res.partner', [8, 3, 1], { 'fields': ['name', 'comment']})
     */
    read(model: string, args: any, kwargs?: any): Observable<any> {
        return this.call(model, 'read', args, kwargs);
    }

    /**
     * Example:
     * instance.read('res.partner', { 'attributes': ['string', 'help', 'type']})
     */
    fieldsGet(model: string, kwargs: any): Observable<any> {
        return this.call(model, 'fields_get', [], kwargs);
    }

    /**
     * Example:
     * instance.create('res.partner', 'create', [{ 'name': 'New Partner' }])
     */
    create(model: string, args: any): Observable<any> {
        return this.call(model, 'create', args);
    }

    /**
     * Example:
     * instance.write('res.partner', [[id], {'name': 'New name'}])
     */
    write(model: string, args: any): Observable<any> {
        return this.call(model, 'write', args);
    }

    /**
     * Example:
     * instance.unlink('res.partner', [[id]])
     */
    unlink(model: string, args: any): Observable<any> {
        return this.call(model, 'unlink', args);
    }

    /**
     * Example:
     * instance.checkAccessRights('res.partner', ['read'])
     */
    checkAccessRights(model: string, action: Array<string>): Observable<any> {
        return this.call(model, 'check_access_rights', action, { 'raise_exception': false });
    }
}