import { Injectable } from '@angular/core';
import { Application, Permission } from '../../shared';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { AppSettings } from 'app/core/app-settings';

const sampleApp: Application[] = [
    {
        code: '1',
        name: 'Web Portal',
        permissions: [
            {
                code: '1',
                title: 'Users - Create/ Edit/ Delete'
            },
            {
                code: '2',
                title: 'Loads - View'
            },
            {
                code: '3',
                title: 'Loads - View Accessorials'
            },
            {
                code: '4',
                title: 'Loads - Find/ Bid'
            },
            {
                code: '5',
                title: 'Dedicated Lanes'
            }
        ]
    },
    {
        code: '2',
        name: 'Driver App',
        permissions: [
            {
                code: '6',
                title: 'View Assigned Moves'
            },
            {
                code: '7',
                title: 'View Load Board'
            }
        ]
    }
];

@Injectable()
export class ApplicationService {
    private application: any;

    constructor(private http: Http, private appSettings: AppSettings) {
        this.application = sampleApp;
    }

    getApplications(): Promise<Array<Application>> {
        let url = this.getUri();
        return new Promise((resolve, reject) => {
            resolve(this.application);
        });
    }

    private getHeaders(obj?: any) {
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');

        for (let h in obj || {}) {
            headers.append(h, obj[ h ]);
        }

        return headers;
    }

    private getUri(endpoint?: string): string {
        return this.appSettings.apiUrl + 'Application/' + endpoint;
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error);
        return Promise.reject(error.message || error);
    }

}
