import { IconButton } from '@dash/components';
import { action, makeObservable, observable } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { CgClose } from 'react-icons/cg';
import { ClientUtils, setupMoveUpEvents } from '../../../../ClientUtils';
import { emptyFunction } from '../../../../Utils';
import { Doc } from '../../../../fields/Doc';
import { StrCast } from '../../../../fields/Types';
import { DragManager } from '../../../util/DragManager';
import { DocumentView } from '../DocumentView';
import './SchemaCSVPopUp.scss';

@observer
export class SchemaCSVPopUp extends React.Component<object> {
    // eslint-disable-next-line no-use-before-define
    static Instance: SchemaCSVPopUp;

    @observable public dataVizDoc: Doc | undefined = undefined;
    @observable public view: DocumentView | undefined = undefined;
    @observable public target: Doc | undefined = undefined;
    @observable public visible: boolean = false;

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

    @action
    public setDataVizDoc = (doc: Doc) => {
        this.dataVizDoc = doc;
    };

    @action
    public setView = (docView: DocumentView) => {
        this.view = docView;
    };

    @action
    public setTarget = (doc: Doc) => {
        this.target = doc;
    };

    @action
    public setVisible = (vis: boolean) => {
        this.visible = vis;
    };

    dataBox = () => (
        <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
            {this.heading('Schema Table as Data Visualization Doc')}
            <div className="image-content-wrapper">
                <div className="img-wrapper">
                    <div className="img-container" onPointerDown={e => this.drag(e)}>
                        <img width={150} height={150} src="/assets/dataVizBox.png" />
                    </div>
                </div>
            </div>
        </div>
    );

    heading = (headingText: string) => (
        <div className="summary-heading">
            <label className="summary-text">{headingText}</label>
            <IconButton color={StrCast(Doc.UserDoc().userVariantColor)} tooltip="close" icon={<CgClose size="16px" />} onClick={() => this.setVisible(false)} />
        </div>
    );

    drag = (e: React.PointerEvent) => {
        const downX = e.clientX;
        const downY = e.clientY;
        setupMoveUpEvents(
            {},
            e,
            moveEv => {
                const sourceAnchorCreator = () => this.dataVizDoc!;
                const targetCreator = () => {
                    const embedding = Doc.MakeEmbedding(this.dataVizDoc!);
                    return embedding;
                };
                if (this.view && sourceAnchorCreator && !ClientUtils.isClick(moveEv.clientX, moveEv.clientY, downX, downY, Date.now())) {
                    DragManager.StartAnchorAnnoDrag(moveEv.target instanceof HTMLElement ? [moveEv.target] : [], new DragManager.AnchorAnnoDragData(this.view, sourceAnchorCreator, targetCreator), downX, downY, {
                        dragComplete: () => this.setVisible(false),
                    });
                    return true;
                }
                return false;
            },
            emptyFunction,
            action(() => {})
        );
    };

    render() {
        return (
            <div className="summary-box" style={{ display: this.visible ? 'flex' : 'none' }}>
                {this.dataBox()}
            </div>
        );
    }
}
