// angular
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Http } from '@angular/http';
// libs
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Observable';
// libs
import { Store, } from '@ngrx/store';
// app
import { APIDispatcher, ResponseBody } from '../utils/index';
import { QueryAction, SetEntriesAction } from '../actions/loop-activity.action';
import { LoopActivity, LoopActivityPayload } from '../interfaces/index';

@Injectable()
export class LoopActivityService extends APIDispatcher {

    constructor(public http: Http, public store: Store<any>, private router: Router) {
        super(http, store);
    }

    query(page: number = 0, size: number = 10, filters: string[] = ['scenario', 'objection', 'content']): Observable<any> {
        return this.post('ma_loop/api/v1/activity/fetch', filters, page, size)
            .map(res => res.json())
            .catch(this.handleError);
    }

    queryForStore(page: number = 0, size: number = 10, filters: string[] = ['scenario', 'objection', 'content']): any {
        return this.query(page, size, filters).subscribe((response: ResponseBody) => {
                this.loaded = true;
                this.store.dispatch(new QueryAction(response.content));
            });
    }

    save(payload: LoopActivityPayload) {
        if(!payload) {
            return;
        }
        this.http.post('ma_loop/api/v1/activity', [payload])
            .catch(this.handleError)
            .subscribe();
    }

     /**
     * Gets the correct content title to display based on the tracked activity
     *
     * @param {LoopActivity} activity The activity to display
     * @returns The title for the activity
     *
     * @memberOf RecentActivityComponent
     */
    getTitle(activity: LoopActivity) {
        if(activity.scenario_title !== null) {
            return activity.scenario_title;
        }
        if(activity.objection_title !== null) {
            return activity.objection_title;
        }
        if(activity.content_title !== null) {
            return activity.content_title;
        }
        // V2 tracking (possibly)
        // if(activity.assignment_title !== null) {
        //     return activity.assignment_title;
        // }
        // if(activity.session_title !== null) {
        //     return activity.session_title;
        // }
        return '';
    }

    /**
     * Handles the desired clickable action for viewing an activity
     *
     * @param {LoopActivity} activity The activity to view
     *
     * @memberOf RecentActivityComponent
     */
    handleAction(activity: LoopActivity) {
        if(activity.scenario_identity !== null) {
            this.router.navigate(['/main/content/scenarios/', activity.scenario_identity]);
        }
        if(activity.objection_identity !== null) {
            this.router.navigate(['/main/content/topics/', activity.objection_identity]);
        }
        if(activity.content_identity !== null) {
            this.router.navigate(['/main/content/documents/', activity.content_identity]);
        }
        // V2
        // if(activity.assignment_identity !== null) {
        //     // TODO - View assignment
        // }
        // if(activity.session_identity !== null) {
        //     // TODO - View session
        // }
    }

    /**
     * Resets the entries in the app-state slice for LoopActivity
     *
     *
     * @memberOf LoopActivityService
     */
    resetEntries() {
        this.loaded = false;
        this.lastRequest = undefined;
        this.store.dispatch(new SetEntriesAction([]));
    }

}

