import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { action, computed, IReactionDisposer, makeObservable, observable, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { SnappingManager } from '../../../util/SnappingManager';
import './DocCreatorMenu.scss';
import { ObservableReactComponent } from '../../ObservableReactComponent';
import { IconButton, Size } from 'browndash-components';
import { returnFalse, setupMoveUpEvents } from '../../../../ClientUtils';
import { emptyFunction } from '../../../../Utils';
import { undoable } from '../../../util/UndoManager';
import { DragManager } from '../../../util/DragManager';
import { DocumentView } from '../DocumentView';
import { dropActionType } from '../../../util/DropActionTypes';

@observer
export class DocCreatorMenu extends ObservableReactComponent<{}> {

    static Instance: DocCreatorMenu;

    private _ref: HTMLDivElement | null = null;

    private _reactionDisposer?: IReactionDisposer;

    @observable _pageX: number = 0;
    @observable _pageY: number = 0;
    @observable _display: boolean = false;

    @observable _yRelativeToTop: boolean = true;
    @observable _selectedIndex = -1;

    @observable _mouseX: number = -1;
    @observable _mouseY: number = -1;
    @observable _startPos: {x: number, y: number} | undefined = undefined;
    @observable _shouldDisplay: boolean = false;

    @observable _menuContent: 'templates' | 'options' = 'templates';
    @observable _dragging: boolean = false;

    constructor(props: any) {
        super(props);
        makeObservable(this);
        DocCreatorMenu.Instance = this;
    }

    @action
    onPointerDown = (e: PointerEvent) => {
        this._mouseX = e.clientX;
        this._mouseY = e.clientY;
    };

    @action
    onPointerUp = (e: PointerEvent) => {
        if (e.button !== 2 && !e.ctrlKey) return;
        const curX = e.clientX;
        const curY = e.clientY;
        if (Math.abs(this._mouseX - curX) > 1 || Math.abs(this._mouseY - curY) > 1) {
            this._shouldDisplay = false;
        }
    };

    componentDidMount() {
        document.addEventListener('pointerdown', this.onPointerDown, true);
        document.addEventListener('pointerup', this.onPointerUp);
    }

    componentWillUnmount() {
        document.removeEventListener('pointerdown', this.onPointerDown, true);
        document.removeEventListener('pointerup', this.onPointerUp);
        this._reactionDisposer?.();
    }

    static readonly buffer = 20;
    static readonly width = 300;
    static readonly height = 400;
    get pageX() {
        return this._pageX + DocCreatorMenu.width > window.innerWidth - DocCreatorMenu.buffer ? window.innerWidth - DocCreatorMenu.buffer - DocCreatorMenu.width : Math.max(0, this._pageX);
    }

    @action
    toggleDisplay = (x: number, y: number) => {
        // maxX and maxY will change if the UI/font size changes, but will work for any amount
        // of items added to the menu
        if (this._shouldDisplay) {
            this._shouldDisplay = false;
        } else {
            this._pageX = x;
            console.log(y);
            this._pageY = y;
            this._shouldDisplay = true;
        }
    };

    @action
    closeMenu = () => {
        const wasOpen = this._display;
        this._display = false;
        this._shouldDisplay = false;
        return wasOpen;
    };

    @action
    onPointerMove = (e: any) => {
        if (this._dragging){
            this._pageX = e.pageX - (this._startPos?.x ?? 0);
            this._pageY = e.pageY - (this._startPos?.y ?? 0);
        }
    }

    setDragStart = () => {

    }

    render() {
        return (
            <div
                className="docCreatorMenu-cont"
                ref={r => this._ref = r}
                style={{
                    display: '',
                    left: this._pageX,
                    top: this._pageY,
                    width: this._shouldDisplay ? 300 : 0,
                    height: this._shouldDisplay ? 400 : 0,
                    background: SnappingManager.userBackgroundColor,
                    color: SnappingManager.userColor,
                }}>
            {!this._shouldDisplay ? undefined : 
                <>
                    <div 
                    className='docCreatorMenu-menu'
                    onPointerDown={e =>
                        setupMoveUpEvents(
                            this,
                            e,
                            (e) => {
                                this._dragging = true;
                                this._startPos = {x: 0, y: 0};
                                this._startPos.x = e.pageX - (this._ref?.getBoundingClientRect().left ?? 0);
                                this._startPos.y = e.pageY - (this._ref?.getBoundingClientRect().top ?? 0);
                                return true;
                            },
                            emptyFunction,
                            undoable(clickEv => {
                                clickEv.stopPropagation();
                            }, 'open actions menu')
                        )
                    
                    }
                    onPointerMove={e => this.onPointerMove(e)}
                    onPointerUp={() => this._dragging = false}
                    >  
                        <button 
                            className='docCreatorMenu-menu-button'
                            onPointerDown={e =>
                                setupMoveUpEvents(
                                    this,
                                    e,
                                    returnFalse,
                                    emptyFunction,
                                    undoable(clickEv => {
                                        clickEv.stopPropagation();
                                        runInAction(() => this._menuContent = this._menuContent === 'templates' ? 'options' : 'templates');
                                    }, 'open actions menu')
                                )
                            }
                        >
                            <FontAwesomeIcon icon={this._menuContent === 'templates' ? 'table-cells' : 'window-maximize'}/>
                        </button>
                        <button className='docCreatorMenu-menu-button close-menu'>
                            <FontAwesomeIcon icon={'minus'}/>
                        </button>
                        <button className='docCreatorMenu-menu-button right'>
                            <FontAwesomeIcon icon={'plus'}/>
                        </button>
                    </div>
                    <hr className='docCreatorMenu-menu-hr'/>
                    <div className='docCreatorMenu-content'>
                        {this._menuContent === 'templates' ? 
                            <div>
                            </div>
                            :
                            <div>
                            </div>
                        }
                    </div>
                </>
            }
            </div>
            )
        }
    

}