import './MeshTransformButton.scss';
import * as React from 'react';
import ReactLoading from 'react-loading';
import { Button, IconButton, Type } from '@dash/components';
import { AiOutlineInfo } from 'react-icons/ai';
import { SettingsManager } from '../../../../util/SettingsManager';
import MeshTransformGrid from './imageMesh';

interface ButtonContainerProps {
    onClick: () => Promise<void>;
    loading: boolean;
    onReset: () => void;
    btnText: string;
    imageWidth: number;
    imageHeight: number;
    gridXSize: number;  // X subdivisions
    gridYSize: number;  // Y subdivisions
}

export function MeshTransformButton({
    loading,
    onClick,
    onReset,
    btnText,
    imageWidth,
    imageHeight,
    gridXSize,
    gridYSize
}: ButtonContainerProps) {
    const [showGrid, setShowGrid] = React.useState(false);
    const [isGridInteractive, setIsGridInteractive] = React.useState(false); // Controls the dragging of control points
    const imageRef = React.useRef<HTMLImageElement>(null); // Reference to the image element

    const handleGridToggle = () => {
        if (showGrid) {
            setShowGrid(false); // Hide the grid
            setIsGridInteractive(false); // Disable control points manipulation
        } else {
            setShowGrid(true); // Show the grid
            setIsGridInteractive(true); // Enable control points manipulation
        }
    };

    return (
        <div className="meshTransformBtnContainer">
            <Button text="RESET" type={Type.PRIM} color={SettingsManager.userVariantColor} onClick={onReset} />
            {loading ? (
                <Button
                    text={btnText}
                    type={Type.TERT}
                    color={SettingsManager.userVariantColor}
                    icon={<ReactLoading type="spin" color="#ffffff" width={20} height={20} />}
                    iconPlacement="right"
                    onClick={() => {
                        if (!loading) handleGridToggle(); // Toggle the grid visibility and control points manipulation
                    }}
                />
            ) : (
                <Button
                    text={btnText}
                    type={Type.TERT}
                    color={SettingsManager.userVariantColor}
                    onClick={() => {
                        if (!loading) handleGridToggle(); // Toggle the grid visibility and control points manipulation
                    }}
                />
            )}

            {/* The IconButton will toggle the grid */}
            <IconButton
                type={Type.SEC}
                color={SettingsManager.userVariantColor}
                tooltip="Toggle Grid"
                icon={<AiOutlineInfo size="16px" />}
                onClick={handleGridToggle} // Toggle the grid when clicked
            />

            {/* Only show the grid if `showGrid` is true */}
            {showGrid && (
                <MeshTransformGrid
                    imageRef={imageRef}
                    gridXSize={gridXSize}
                    gridYSize={gridYSize}
                    isInteractive={isGridInteractive} // Pass the interactive flag to control point manipulation
                />
            )}
            <img ref={imageRef} src="your-image-source.jpg" alt="Mesh" style={{ width: imageWidth, height: imageHeight }} />
        </div>
    );
}
