import BackgroundMessageService from "../application/BackgroundMessageService";
import State from "../domain/State";
import ActionProbabilityMap from "../domain/ActionProbabilityMap";
import Comment from "../domain/Comment";
import Action from "../domain/Action";
import ObjectiveCheckList from "../domain/CheckList";
import CheckList from "../domain/CheckList";


export default class ChromeBackgroundService implements BackgroundMessageService {

    getState(): Promise<State> {
        return new Promise((resolve, reject) => {
            chrome.runtime.sendMessage({
                kind: 'getStateForTabScript'
            }, {}, data => {
                //console.log('state:',data);
                const error = chrome.runtime.lastError;
                if (error) {
                    return reject(error);
                } else {
                    let isActive = data.isRecording ? true : false;

                    
                    const state = new State(
                        data.commentsUp,
                        data.isDisplayingComments,
                        isActive, 
                        data.webSite, 
                        data.popupCommentPosition);
                    return resolve(state)
                }
            });
        })
    }

    getProbabilityMap(): Promise<ActionProbabilityMap> {
        return new Promise((resolve, reject) => {
            chrome.runtime.sendMessage({
                kind: 'getProbabilities'
            }, {}, data => {
                const error = chrome.runtime.lastError;
                if (error) {
                    return reject(error);
                }
                return resolve(new ActionProbabilityMap(data.probabilityMap)); 
            })
        })   
    }

    getCommentList(): Promise<Comment[]> {
        return new Promise((resolve, reject) => {
            chrome.runtime.sendMessage({
                kind: 'getComments'
            }, {}, data => {
                const error = chrome.runtime.lastError;
                if (error) {
                    return reject(error);
                }
                let commentDistributionList = data.commentDistributionList;
                const comments = []
                for (const commentData of commentDistributionList) {
                    const comment = Comment.parseComment(commentData.note);
                    for (const distribution of commentData.distributions) {
                        comment.addDistribution(distribution.noteOccurence, distribution.contextOccurence, distribution.context.map(actionText => Action.parseAction(actionText)));
                    }
                    comments.push(comment);
                }
                return resolve(comments);
            })
        })
    }

    setCommentPopupPosition(newPosition: {x: number, y:number}) {
        return new Promise((resolve, reject) => {
            chrome.runtime.sendMessage({
                kind: 'setPopupCommentPosition',
                popupCommentPosition: newPosition
              }, {}, () => {
                const error = chrome.runtime.lastError;
                if (error) {
                    return reject(error);
                } else {
                    return resolve();
                }
            });
        })
    }

    upComment(comment: Comment) {
        return new Promise((resolve, reject) => {        
            chrome.runtime.sendMessage({
                kind: 'upComment',
                type: comment.type,
                value: comment.note
            }, {}, () => {
                const error = chrome.runtime.lastError;
                if (error) {
                    return reject(error);
                } else {
                    return resolve();
                }
            });
        })
    }

    sendAction(action: Action): Promise<void> {
        return new Promise((resolve, reject) => {        
            chrome.runtime.sendMessage({
                action,
                kind: 'pushAction'
            }, {}, () => {
                const error = chrome.runtime.lastError;
                    if (error) {
                        return reject(error);
                    } else {
                        return resolve();
                    }
            })
        })
    }

    getChecklist(): Promise<CheckList> {
        return new Promise((resolve, reject) => {
            chrome.runtime.sendMessage({
                kind: 'getChecklist'
            }, {}, (data) => {
                const error = chrome.runtime.lastError;
                if (error) {
                    return reject(error);
                } else {
                    //console.log(data);
                    return resolve(new CheckList([], []));
                }
            })
        })
    }

}

    