import { computed, makeObservable, observable, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { DictationManager } from '../util/DictationManager';
import './Main.scss';
import { MainViewModal } from './MainViewModal';

@observer
export class DictationOverlay extends React.Component {
    // eslint-disable-next-line no-use-before-define
    public static Instance: DictationOverlay;
    @observable private _dictationState = DictationManager.placeholder;
    @observable private _dictationSuccessState: boolean | undefined = undefined;
    @observable private _dictationDisplayState = false;
    @observable private _dictationListeningState: DictationManager.Controls.ListeningUIStatus = false;

    public hasActiveModal = false;

    constructor(props: object) {
        super(props);
        makeObservable(this);
        DictationOverlay.Instance = this;
    }

    @computed public get dictatedPhrase() { return this._dictationState; } // prettier-ignore
    public set dictatedPhrase(value: string) {
        runInAction(() => {
            this._dictationState = value;
        });
    }
    @computed public get dictationSuccess() { return this._dictationSuccessState; } // prettier-ignore
    public set dictationSuccess(value: boolean | undefined) {
        runInAction(() => { this._dictationSuccessState = value; }); // prettier-ignore
    }
    @computed public get dictationOverlayVisible() { return this._dictationDisplayState; } // prettier-ignore
    public set dictationOverlayVisible(value: boolean) {
        runInAction(() => { this._dictationDisplayState = value; }); // prettier-ignore
    }
    @computed public get isListening() { return this._dictationListeningState; } // prettier-ignore
    public set isListening(value: DictationManager.Controls.ListeningUIStatus) {
        runInAction(() => { this._dictationListeningState = value; }); // prettier-ignore
    }
    public initiateDictationFade = () => {
        setTimeout(() => {
            this.dictationOverlayVisible = false;
            this.dictationSuccess = undefined;
            DictationOverlay.Instance.hasActiveModal = false;
            setTimeout(() => { this.dictatedPhrase = DictationManager.placeholder; }, 500); // prettier-ignore
        }, DictationManager.Commands.dictationFadeDuration);
    };

    render() {
        const success = this.dictationSuccess;
        const result = this.isListening && !this.isListening.interim ? DictationManager.placeholder : `"${this.dictatedPhrase}"`;
        const dialogueBoxStyle = {
            background: success === undefined ? 'gainsboro' : success ? 'lawngreen' : 'red',
            borderColor: this.isListening ? 'red' : 'black',
            fontStyle: 'italic',
        };
        const overlayStyle = {
            backgroundColor: this.isListening ? 'red' : 'darkslategrey',
        };
        return <MainViewModal contents={result} isDisplayed={this.dictationOverlayVisible} interactive={false} dialogueBoxStyle={dialogueBoxStyle} overlayStyle={overlayStyle} closeOnExternalClick={this.initiateDictationFade} />;
    }
}
