/* eslint-disable react/require-default-props */
import { action } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc } from '../../../../fields/Doc';
import { NumCast, StrCast } from '../../../../fields/Types';
import { UndoManager } from '../../../util/UndoManager';
import { StyleProp } from '../../StyleProp';
import { StyleProviderFuncType } from '../../nodes/FieldView';
import { DimUnit } from './CollectionMultirowView';

interface ResizerProps {
    height: number;
    styleProvider?: StyleProviderFuncType;
    isContentActive?: () => boolean | undefined;
    columnUnitLength(): number | undefined;
    toTop?: Doc;
    toBottom?: Doc;
}

@observer
export default class ResizeBar extends React.Component<ResizerProps> {
    private _resizeUndo?: UndoManager.Batch;

    @action
    private registerResizing = (e: React.PointerEvent<HTMLDivElement>) => {
        e.stopPropagation();
        e.preventDefault();
        window.removeEventListener('pointermove', this.onPointerMove);
        window.removeEventListener('pointerup', this.onPointerUp);
        window.addEventListener('pointermove', this.onPointerMove);
        window.addEventListener('pointerup', this.onPointerUp);
        this._resizeUndo = UndoManager.StartBatch('multcol resizing');
    };

    private onPointerMove = ({ movementY }: PointerEvent) => {
        const { toTop, toBottom, columnUnitLength } = this.props;
        const movingDown = movementY > 0;
        const toNarrow = movingDown ? toBottom : toTop;
        const toWiden = movingDown ? toTop : toBottom;
        const unitLength = columnUnitLength();
        if (unitLength) {
            if (toNarrow) {
                const scale = StrCast(toNarrow._dimUnit, '*') === DimUnit.Ratio ? unitLength : 1;
                toNarrow._dimMagnitude = Math.max(0.05, NumCast(toNarrow._dimMagnitude, 1) - Math.abs(movementY) / scale);
            }
            if (toWiden) {
                const scale = StrCast(toWiden._dimUnit, '*') === DimUnit.Ratio ? unitLength : 1;
                toWiden._dimMagnitude = Math.max(0.05, NumCast(toWiden._dimMagnitude, 1) + Math.abs(movementY) / scale);
            }
        }
    };

    @action
    private onPointerUp = () => {
        window.removeEventListener('pointermove', this.onPointerMove);
        window.removeEventListener('pointerup', this.onPointerUp);
        this._resizeUndo?.end();
        this._resizeUndo = undefined;
    };

    render() {
        return (
            <div
                className="multiRowResizer"
                style={{
                    pointerEvents: this.props.isContentActive?.() ? 'all' : 'none',
                    height: this.props.height,
                    backgroundColor: !this.props.isContentActive?.() ? '' : this.props.styleProvider?.(undefined, undefined, StyleProp.WidgetColor) as string,
                }}>
                <div className="multiRowResizer-hdl" onPointerDown={e => this.registerResizing(e)} />
            </div>
        );
    }
}
