/* eslint-disable jsx-a11y/control-has-associated-label */
import mermaid from 'mermaid';
import { action, makeObservable, observable, reaction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { Doc, DocListCast } from '../../../fields/Doc';
import { List } from '../../../fields/List';
import { RichTextField } from '../../../fields/RichTextField';
import { DocCast } from '../../../fields/Types';
import { GPTCallType, gptAPICall } from '../../apis/gpt/GPT';
import { DocumentType } from '../../documents/DocumentTypes';
import { Docs } from '../../documents/Documents';
import { DocumentManager } from '../../util/DocumentManager';
import { LinkManager } from '../../util/LinkManager';
import { ViewBoxAnnotatableComponent } from '../DocComponent';
import { InkingStroke } from '../InkingStroke';
import './DiagramBox.scss';
import { FieldView, FieldViewProps } from './FieldView';

enum menuState {
    option,
    mermaidCode,
    drawing,
    gpt,
    justCreated,
}

@observer
export class DiagramBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
    public static LayoutString(fieldKey: string) {
        return FieldView.LayoutString(DiagramBox, fieldKey);
    }
    private _ref: React.RefObject<HTMLDivElement> = React.createRef();
    private _dragRef = React.createRef<HTMLDivElement>();
    constructor(props: FieldViewProps) {
        super(props);
        makeObservable(this);
    }
    @observable menuState = menuState.justCreated;
    @observable renderDiv: React.ReactNode;
    @observable inputValue = '';
    @observable loading = false;
    @observable errorMessage = '';
    @observable mermaidCode = '';

    @action handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
        this.inputValue = e.target.value;
        console.log(e.target.value);
    };
    async componentDidMount() {
        this._props.setContentViewBox?.(this);
        mermaid.initialize({
            securityLevel: 'loose',
            startOnLoad: true,
            darkMode: true,
            flowchart: { useMaxWidth: false, htmlLabels: true, curve: 'cardinal' },
            gantt: { useMaxWidth: true, useWidth: 2000 },
        });
        if (!this.Document.testValue) {
            this.Document.height = 500;
            this.Document.width = 500;
        }
        this.Document.testValue = 'a';
        this.mermaidCode = 'a';
        if (typeof this.Document.drawingMermaidCode === 'string' && this.Document.menuState === 'drawing') {
            this.renderMermaidAsync(this.Document.drawingMermaidCode);
        }
        console.log(this.Document.title);
        // this.renderMermaidAsync(this.timeline);
        // this is so that ever time a new doc, text node or ink node, is created, this.createMermaidCode will run which will create a save
        reaction(
            () => DocListCast(this.Document.data),
            () => this.convertDrawingToMermaidCode(),
            { fireImmediately: true }
        );
        reaction(
            () =>
                DocListCast(this.Document.data)
                    .filter(doc => doc.type === 'rich text')
                    .map(doc => (doc.text as RichTextField).Text),
            () => this.convertDrawingToMermaidCode(),
            { fireImmediately: true }
        );
    }
    componentDidUpdate = () => {
        if (typeof this.Document.drawingMermaidCode === 'string' && this.Document.menuState === 'drawing') {
            this.renderMermaidAsync(this.Document.drawingMermaidCode);
        }
        if (typeof this.Document.gptMermaidCode === 'string' && this.Document.menuState === 'gpt') {
            this.renderMermaidAsync(this.Document.gptMermaidCode);
        }
    };
    switchRenderDiv() {
        switch (this.Document.menuState) {
            case 'option':
                this.renderDiv = this.renderOption();
                break;
            case 'drawing':
                this.renderDiv = this.renderDrawing();
                break;
            case 'gpt':
                this.renderDiv = this.renderGpt();
                break;
            case 'mermaidCode':
                this.renderDiv = this.renderMermaidCode();
                break;
            default:
                this.menuState = menuState.option;
                this.renderDiv = this.renderOption();
                console.log(this.renderDiv);
        }
    }
    renderMermaid = async (str: string) => {
        try {
            const { svg, bindFunctions } = await this.mermaidDiagram(str);
            return { svg, bindFunctions };
        } catch (error) {
            console.error('Error rendering mermaid diagram:', error);
            return { svg: '', bindFunctions: undefined };
        }
    };
    mermaidDiagram = async (str: string) => mermaid.render('graph' + Date.now(), str);

    async renderMermaidAsync(mermaidCode: string) {
        try {
            const { svg, bindFunctions } = await this.renderMermaid(mermaidCode);
            const dashDiv = document.getElementById('dashDiv' + this.Document.title);
            if (dashDiv) {
                dashDiv.innerHTML = svg;
                if (bindFunctions) {
                    bindFunctions(dashDiv);
                }
            }
        } catch (error) {
            console.error('Error rendering Mermaid:', error);
        }
    }
    @action handleRenderClick = () => {
        this.mermaidCode = '';
        if (this.inputValue) {
            this.generateMermaidCode();
        }
    };
    @action async generateMermaidCode() {
        console.log('Generating Mermaid Code');
        this.loading = true;
        let prompt = '';
        prompt = 'Write this in mermaid code and only give me the mermaid code: ' + this.inputValue;
        console.log('there is no text save');
        // }
        const res = await gptAPICall(prompt, GPTCallType.MERMAID);
        this.loading = false;
        if (res === 'Error connecting with API.') {
            // If GPT call failed
            console.error('GPT call failed');
            this.errorMessage = 'GPT call failed; please try again.';
        } else if (res !== null) {
            // If GPT call succeeded, set htmlCode;;; TODO: check if valid html
            if (this.isValidCode(res)) {
                this.mermaidCode = res;
                console.log('GPT call succeeded:' + res);
                this.errorMessage = '';
            } else {
                console.error('GPT call succeeded but invalid html; please try again.');
                this.errorMessage = 'GPT call succeeded but invalid html; please try again.';
            }
        }
        this.renderMermaidAsync.call(this, this.removeWords(this.mermaidCode));
        this.Document.gptMermaidCode = this.removeWords(this.mermaidCode);
        this.loading = false;
    }
    isValidCode = (html: string) => true;
    removeWords(inputStrIn: string) {
        const inputStr = inputStrIn.replace('```mermaid', '');
        return inputStr.replace('```', '');
    }
    // method to convert the drawings on collection node side the mermaid code
    async convertDrawingToMermaidCode() {
        let mermaidCode = '';
        let diagramExists = false;
        if (this.Document.data instanceof List) {
            const docArray: Doc[] = DocListCast(this.Document.data);
            const rectangleArray = docArray.filter(doc => doc.title === 'rectangle' || doc.title === 'circle');
            const lineArray = docArray.filter(doc => doc.title === 'line' || doc.title === 'stroke');
            const textArray = docArray.filter(doc => doc.type === 'rich text');
            const timeoutPromise = () =>
                new Promise(resolve => {
                    setTimeout(resolve, 0);
                });
            await timeoutPromise();
            const inkStrokeArray = lineArray.map(doc => DocumentManager.Instance.getDocumentView(doc, this.DocumentView?.())).filter(inkView => inkView?.ComponentView instanceof InkingStroke);
            console.log(inkStrokeArray.length);
            console.log(lineArray.length);
            if (inkStrokeArray[0] && inkStrokeArray.length === lineArray.length) {
                mermaidCode = 'graph TD;';
                const inkingStrokeArray = inkStrokeArray.map(stroke => stroke?.ComponentView);
                for (let i = 0; i < rectangleArray.length; i++) {
                    const rectangle = rectangleArray[i];
                    for (let j = 0; j < lineArray.length; j++) {
                        const inkScaleX = (inkingStrokeArray[j] as InkingStroke)?.inkScaledData().inkScaleX;
                        const inkScaleY = (inkingStrokeArray[j] as InkingStroke)?.inkScaledData().inkScaleY;
                        const inkStrokeXArray = (inkingStrokeArray[j] as InkingStroke)
                            ?.inkScaledData()
                            .inkData.map(coord => coord.X)
                            .map(doc => doc * inkScaleX);
                        const inkStrokeYArray = (inkingStrokeArray[j] as InkingStroke)
                            ?.inkScaledData()
                            .inkData.map(coord => coord.Y)
                            .map(doc => doc * inkScaleY);
                        console.log(inkingStrokeArray.length);
                        console.log(lineArray.length);
                        // need to minX and minY to since the inkStroke.x and.y is not relative to the doc. so I have to do some calcluations
                        const minX: number = Math.min(...inkStrokeXArray);
                        const minY: number = Math.min(...inkStrokeYArray);
                        const startX = inkStrokeXArray[0] - minX + (lineArray[j]?.x as number);
                        const startY = inkStrokeYArray[0] - minY + (lineArray[j]?.y as number);
                        const endX = inkStrokeXArray[inkStrokeXArray.length - 1] - minX + (lineArray[j].x as number);
                        const endY = inkStrokeYArray[inkStrokeYArray.length - 1] - minY + (lineArray[j].y as number);
                        if (this.isPointInBox(rectangle, [startX, startY])) {
                            for (let k = 0; k < rectangleArray.length; k++) {
                                const rectangle2 = rectangleArray[k];
                                if (this.isPointInBox(rectangle2, [endX, endY]) && typeof rectangle.x === 'number' && typeof rectangle2.x === 'number') {
                                    diagramExists = true;
                                    const linkedDocs: Doc[] = LinkManager.Instance.getAllRelatedLinks(lineArray[j]).map(d => DocCast(LinkManager.getOppositeAnchor(d, lineArray[j])));
                                    console.log(linkedDocs.length);
                                    if (linkedDocs.length !== 0) {
                                        const linkedText = (linkedDocs[0].text as RichTextField).Text;
                                        mermaidCode += Math.abs(rectangle.x) + this.getTextInBox(rectangle, textArray) + '-->|' + linkedText + '|' + Math.abs(rectangle2.x) + this.getTextInBox(rectangle2, textArray) + ';';
                                    } else {
                                        mermaidCode += Math.abs(rectangle.x) + this.getTextInBox(rectangle, textArray) + '-->' + Math.abs(rectangle2.x) + this.getTextInBox(rectangle2, textArray) + ';';
                                    }
                                }
                            }
                        }
                    }
                }
                // this will save the text
                if (diagramExists) {
                    this.Document.drawingMermaidCode = mermaidCode;
                } else {
                    this.Document.drawingMermaidCode = '';
                }
            }
        }
    }
    testInkingStroke = () => {
        if (this.Document.data instanceof List) {
            const docArray: Doc[] = DocListCast(this.Document.data);
            const lineArray = docArray.filter(doc => doc.title === 'line' || doc.title === 'stroke');
            setTimeout(() => {
                const inkStrokeArray = lineArray.map(doc => DocumentManager.Instance.getDocumentView(doc, this.DocumentView?.())).filter(inkView => inkView?.ComponentView instanceof InkingStroke);
                console.log(inkStrokeArray);
            });
        }
    };
    getTextInBox = (box: Doc, richTextArray: Doc[]): string => {
        for (let i = 0; i < richTextArray.length; i++) {
            const textDoc = richTextArray[i];
            if (typeof textDoc.x === 'number' && typeof textDoc.y === 'number' && typeof box.x === 'number' && typeof box.height === 'number' && typeof box.width === 'number' && typeof box.y === 'number') {
                if (textDoc.x > box.x && textDoc.x < box.x + box.width && textDoc.y > box.y && textDoc.y < box.y + box.height) {
                    if (box.title === 'rectangle') {
                        return '(' + ((textDoc.text as RichTextField)?.Text ?? '') + ')';
                    }
                    if (box.title === 'circle') {
                        return '((' + ((textDoc.text as RichTextField)?.Text ?? '') + '))';
                    }
                }
            }
        }
        return '( )';
    };
    isPointInBox = (box: Doc, line: number[]): boolean => {
        if (typeof line[0] === 'number' && typeof box.x === 'number' && typeof box.width === 'number' && typeof box.height === 'number' && typeof box.y === 'number' && typeof line[1] === 'number') {
            return line[0] < box.x + box.width && line[0] > box.x && line[1] > box.y && line[1] < box.y + box.height;
        }
        return false;
    };
    autoResize(element: HTMLTextAreaElement): void {
        element.style.height = '5px';
        element.style.height = element.scrollHeight + 'px';
    }
    drawingButton = () => {
        this.Document.menuState = 'drawing';
    };
    gptButton = () => {
        this.Document.menuState = 'gpt';
    };
    mermaidButton = () => {
        this.Document.menuState = 'mermaidCode';
    };
    optionButton = () => {
        this.Document.menuState = 'option';
    };
    renderOption(): React.ReactNode {
        return (
            <div className="buttonCollections">
                <button type="button" onClick={this.drawingButton}>
                    Drawing - Create diagram from ink drawing
                </button>
                <button type="button" onClick={this.gptButton}>
                    GPT - Generate diagram with AI prompt
                </button>
                <button type="button" onClick={this.mermaidButton}>
                    Mermaid Editor - Create diagram with mermaid code
                </button>
            </div>
        );
    }
    renderDrawing(): React.ReactNode {
        console.log(this.Document.mermaidCode);
        return (
            <div ref={this._dragRef} className="DiagramBox-wrapper">
                <div className="content">
                    <div className="topBar">
                        <button className="backButtonDrawing" type="button" onClick={this.optionButton}>
                            Back
                        </button>
                        {!this.Document.mermaidCode && <p>Click the red pen icon to flip onto the collection side and draw a diagram with ink</p>}
                    </div>
                    <div id={'dashDiv' + this.Document.title} className="diagramBox" />
                </div>
            </div>
        );
    }

    renderGpt(): React.ReactNode {
        return (
            <div ref={this._dragRef} className="DiagramBox-wrapper">
                <div className="content">
                    <div className="search-bar">
                        <button className="backButton" type="button" onClick={this.optionButton}>
                            Back
                        </button>
                        <textarea value={this.inputValue} placeholder="Enter GPT prompt" onChange={this.handleInputChange} onInput={e => this.autoResize(e.target as HTMLTextAreaElement)} />
                        <button className="generateButton" type="button" onClick={this.handleRenderClick}>
                            Generate
                        </button>
                    </div>
                    <div id={'dashDiv' + this.Document.title} className="diagramBox" />
                </div>
            </div>
        );
    }

    renderMermaidCode(): React.ReactNode {
        return (
            <div ref={this._dragRef} className="DiagramBox-wrapper">
                <div className="content">
                    <div className="search-bar">
                        <button className="backButton" type="button" onClick={this.optionButton}>
                            Back
                        </button>
                        <textarea placeholder="Enter Mermaid Code" onChange={this.handleInputChangeEditor} onInput={e => this.autoResize(e.target as HTMLTextAreaElement)} />
                    </div>
                    <div id={'dashDiv' + this.Document.title} className="diagramBox" />
                </div>
            </div>
        );
    }
    handleInputChangeEditor = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
        if (typeof e.target.value === 'string') {
            this.renderMermaidAsync(e.target.value);
        }
    };
    timeline = `gantt
    title College Timeline
    dateFormat  YYYY-MM-DD
    section Semester 1
    Orientation           :done,    des1, 2023-08-01, 2023-08-03
    Classes Start         :active,  des2, 2023-08-04, 2023-12-15
    Midterm Exams         :         des3, 2023-10-15, 2023-10-20
    End of Semester       :         des4, 2023-12-16, 2023-12-20
    section Semester 2
    Classes Start         :         des5, 2024-01-10, 2024-05-15
    Spring Break          :         des6, 2024-03-15, 2024-03-22
    Midterm Exams         :         des7, 2024-03-25, 2024-03-30
    Final Exams           :         des8, 2024-05-10, 2024-05-15
    section Summer Break
    Internship            :         des9, 2024-06-01, 2024-08-31
    section Semester 3
    Classes Start         :         des10, 2024-09-01, 2025-12-15
    Midterm Exams         :         des11, 2024-11-15, 2024-11-20
    End of Semester       :         des12, 2025-12-16, 2025-12-20
    section Semester 4
    Classes Start         :         des13, 2025-01-10, 2025-05-15
    Spring Break          :         des14, 2025-03-15, 2025-03-22
    Midterm Exams         :         des15, 2025-03-25, 2025-03-30
    Final Exams           :         des16, 2025-05-10, 2025-05-15
    Graduation            :         des17, 2025-05-20, 2025-05-21`;
    render() {
        this.switchRenderDiv();
        return (
            <div ref={this._ref} className="DiagramBox">
                {this.renderDiv}
            </div>
        );
    }
}

Docs.Prototypes.TemplateMap.set(DocumentType.DIAGRAM, {
    layout: { view: DiagramBox, dataField: 'dadta' },
    options: { _height: 700, _width: 700, _layout_fitWidth: false, _layout_nativeDimEditable: true, _layout_reflowVertical: true, waitForDoubleClickToClick: 'always', _layout_reflowHorizontal: true, systemIcon: 'BsGlobe' },
});
