import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc } from '../../../../fields/Doc';
import { NumCast } from '../../../../fields/Types';
import './CollectionFreeFormView.scss';

export interface CollectionFreeFormViewBackgroundGridProps {
    panX: () => number;
    panY: () => number;
    PanelWidth: () => number;
    PanelHeight: () => number;
    color: () => string;
    isAnnotationOverlay?: boolean;
    nativeDimScaling: () => number;
    zoomScaling: () => number;
    layoutDoc: Doc;
    cachedCenteringShiftX: number;
    cachedCenteringShiftY: number;
}
@observer
export class CollectionFreeFormBackgroundGrid extends React.Component<CollectionFreeFormViewBackgroundGridProps> {
    chooseGridSpace = (gridSpace: number): number => {
        if (!this.props.zoomScaling()) return gridSpace;
        const divisions = this.props.PanelWidth() / this.props.zoomScaling() / gridSpace;
        return divisions < 90 ? gridSpace : this.chooseGridSpace(gridSpace * 2);
    };
    render() {
        const gridSpace = this.chooseGridSpace(NumCast(this.props.layoutDoc['_backgroundGrid-spacing'], 50));
        const shiftX = (this.props.isAnnotationOverlay ? 0 : (-this.props.panX() % gridSpace) - gridSpace) * this.props.zoomScaling();
        const shiftY = (this.props.isAnnotationOverlay ? 0 : (-this.props.panY() % gridSpace) - gridSpace) * this.props.zoomScaling();
        const renderGridSpace = gridSpace * this.props.zoomScaling();
        const w = this.props.PanelWidth() / this.props.nativeDimScaling() + 2 * renderGridSpace;
        const h = this.props.PanelHeight() / this.props.nativeDimScaling() + 2 * renderGridSpace;
        const strokeStyle = this.props.color();
        return !this.props.nativeDimScaling() ? null : (
            <canvas
                className="collectionFreeFormView-grid"
                width={w}
                height={h}
                style={{ transform: `translate(${shiftX}px, ${shiftY}px)` }}
                ref={el => {
                    const ctx = el?.getContext('2d');
                    if (ctx) {
                        const Cx = this.props.cachedCenteringShiftX % renderGridSpace;
                        const Cy = this.props.cachedCenteringShiftY % renderGridSpace;
                        ctx.lineWidth = Math.min(1, Math.max(0.5, this.props.zoomScaling()));
                        ctx.setLineDash(gridSpace > 50 ? [3, 3] : [1, 5]);
                        ctx.clearRect(0, 0, w, h);
                        if (ctx) {
                            ctx.strokeStyle = strokeStyle;
                            ctx.fillStyle = strokeStyle;
                            ctx.beginPath();
                            if (this.props.zoomScaling() > 1) {
                                for (let x = Cx - renderGridSpace; x <= w - Cx; x += renderGridSpace) {
                                    ctx.moveTo(x, Cy - h);
                                    ctx.lineTo(x, Cy + h);
                                }
                                for (let y = Cy - renderGridSpace; y <= h - Cy; y += renderGridSpace) {
                                    ctx.moveTo(Cx - w, y);
                                    ctx.lineTo(Cx + w, y);
                                }
                            } else {
                                for (let x = Cx - renderGridSpace; x <= w - Cx; x += renderGridSpace)
                                    for (let y = Cy - renderGridSpace; y <= h - Cy; y += renderGridSpace) {
                                        ctx.fillRect(Math.round(x), Math.round(y), 1, 1);
                                    }
                            }
                            ctx.stroke();
                        }
                    }
                }}
            />
        );
    }
}
