import * as iink from 'iink-ts';
import { action, observable } from 'mobx';
import * as React from 'react';
import { Doc, DocListCast } from '../../fields/Doc';
import { InkData, InkField, InkTool } from '../../fields/InkField';
import { Cast, DateCast, ImageCast, NumCast, StrCast } from '../../fields/Types';
import { aggregateBounds } from '../../Utils';
import { DocumentType } from '../documents/DocumentTypes';
import { CollectionFreeFormView, MarqueeView } from './collections/collectionFreeForm';
import { InkingStroke } from './InkingStroke';
import './InkTranscription.scss';
import { Docs } from '../documents/Documents';
import { DocumentView } from './nodes/DocumentView';
import { Number } from 'mongoose';
import { NumberArray } from 'd3';
import { ImageField } from '../../fields/URLField';
import { gptHandwriting } from '../apis/gpt/GPT';
import * as fs from 'fs';
import { URLField } from '../../fields/URLField';
/**
 * Class component that handles inking in writing mode
 */
export class InkTranscription extends React.Component {
    static Instance: InkTranscription;

    @observable _mathRegister: any = undefined;
    @observable _mathRef: any = undefined;
    @observable _textRegister: any = undefined;
    @observable _textRef: any = undefined;
    @observable iinkEditor: any = undefined;
    private lastJiix: any;
    private currGroup?: Doc;
    private collectionFreeForm?: CollectionFreeFormView;

    constructor(props: Readonly<{}>) {
        super(props);

        InkTranscription.Instance = this;
    }

    componentWillUnmount() {
        // this._mathRef.removeEventListener('exported', (e: any) => this.exportInk(e, this._mathRef));
        // this._textRef.removeEventListener('exported', (e: any) => this.exportInk(e, this._textRef));
    }

    @action
    setMathRef = async (r: any) => {
        if (!this._textRegister && r) {
            let editor;
            const options = {
                configuration: {
                    server: {
                        scheme: 'https',
                        host: 'cloud.myscript.com',
                        applicationKey: 'c0901093-5ac5-4454-8e64-0def0f13f2ca',
                        hmacKey: 'f6465cca-1856-4492-a6a4-e2395841be2f',
                        protocol: 'WEBSOCKET',
                    },
                    recognition: {
                        type: 'TEXT',
                    },
                },
            };

            editor = new iink.Editor(r, options as any);

            await editor.initialize();

            this._textRegister = r;
            r?.addEventListener('exported', (e: any) => this.exportInk(e, this._textRef));

            return (this._textRef = r);
        }
    };
    @action
    setTextRef = async (r: any) => {
        if (!this._textRegister && r) {
            let editor;
            const options = {
                configuration: {
                    server: {
                        scheme: 'https',
                        host: 'cloud.myscript.com',
                        applicationKey: 'c0901093-5ac5-4454-8e64-0def0f13f2ca',
                        hmacKey: 'f6465cca-1856-4492-a6a4-e2395841be2f',
                        protocol: 'WEBSOCKET',
                    },
                    recognition: {
                        type: 'TEXT',
                    },
                },
            };

            editor = new iink.Editor(r, options as any);

            await editor.initialize();
            this.iinkEditor = editor;
            this._textRegister = r;
            r?.addEventListener('exported', (e: any) => this.exportInk(e, this._textRef));

            return (this._textRef = r);
        }
    };

    /**
     * Handles processing Dash Doc data for ink transcription.
     *
     * @param groupDoc the group which contains the ink strokes we want to transcribe
     * @param inkDocs the ink docs contained within the selected group
     * @param math boolean whether to do math transcription or not
     */
    transcribeInk = (groupDoc: Doc | undefined, inkDocs: Doc[], math: boolean) => {
        if (!groupDoc) return;
        const validInks = inkDocs.filter(s => s.type === DocumentType.INK);

        const strokes: InkData[] = [];

        const times: number[] = [];
        validInks
            .filter(i => Cast(i[Doc.LayoutFieldKey(i)], InkField))
            .forEach(i => {
                const d = Cast(i[Doc.LayoutFieldKey(i)], InkField, null);
                const inkStroke = DocumentView.getDocumentView(i)?.ComponentView as InkingStroke;
                strokes.push(d.inkData.map(pd => inkStroke.ptToScreen({ X: pd.X, Y: pd.Y })));
                times.push(DateCast(i.author_date).getDate().getTime());
            });
        console.log(strokes);
        console.log(
            `this.Multistrokes.push(
            new Multistroke(
                Gestures.Scribble,
                useBoundedRotationInvariance,
                new Array([
                    ` +
                this.convertPointsToString(strokes) +
                `
                ])
            )
        )
        `
        );
        //console.log(this.convertPointsToString(strokes));
        //console.log(this.convertPointsToString2(strokes));
        this.currGroup = groupDoc;
        const pointerData = strokes.map((stroke, i) => this.inkJSON(stroke, times[i]));
        const processGestures = false;
        if (math) {
            console.log('math');
            this.iinkEditor.importPointEvents(pointerData);
        } else {
            this.iinkEditor.importPointEvents(pointerData);
        }
    };
    convertPointsToString(points: InkData[]): string {
        return points[0].map(point => `new Point(${point.X}, ${point.Y})`).join(',');
    }
    convertPointsToString2(points: InkData[]): string {
        return points[0].map(point => `(${point.X},${point.Y})`).join(',');
    }

    /**
     * Converts the Dash Ink Data to JSON.
     *
     * @param stroke The dash ink data
     * @param time the time of the stroke
     * @returns json object representation of ink data
     */
    inkJSON = (stroke: InkData, time: number) => {
        interface strokeData {
            x: number;
            y: number;
            t: number;
            p: number;
        }
        let strokeObjects: strokeData[] = [];
        stroke.forEach(point => {
            let tempObject: strokeData = {
                x: point.X,
                y: point.Y,
                t: time,
                p: 1.0,
            };
            strokeObjects.push(tempObject);
        });
        return {
            pointerType: 'PEN',
            pointerId: 1,
            pointers: strokeObjects,
        };
    };

    /**
     * Creates subgroups for each word for the whole text transcription
     * @param wordInkDocMap the mapping of words to ink strokes (Ink Docs)
     */
    subgroupsTranscriptions = (wordInkDocMap: Map<string, Doc[]>) => {
        // iterate through the keys of wordInkDocMap
        wordInkDocMap.forEach(async (inkDocs: Doc[], word: string) => {
            const selected = inkDocs.slice();
            if (!selected) {
                return;
            }
            const ctx = await Cast(selected[0].embedContainer, Doc);
            if (!ctx) {
                return;
            }
            const docView: CollectionFreeFormView = DocumentView.getDocumentView(ctx)?.ComponentView as CollectionFreeFormView;
            // DocumentManager.Instance.getDocumentView(ctx)?.ComponentView as CollectionFreeFormView;

            if (!docView) return;
            const marqViewRef = docView._marqueeViewRef.current;
            if (!marqViewRef) return;
            this.groupInkDocs(selected, docView, word);
        });
    };

    /**
     * Event listener function for when the 'exported' event is heard.
     *
     * @param e the event objects
     * @param ref the ref to the editor
     */
    exportInk = async (e: any, ref: any) => {
        const exports = e.detail['application/vnd.myscript.jiix'];
        if (exports) {
            if (exports['type'] == 'Math') {
                const latex = exports['application/x-latex'];
                if (this.currGroup) {
                    this.currGroup.text = latex;
                    this.currGroup.title = latex;
                }

                ref.editor.clear();
            } else if (exports['type'] == 'Text') {
                if (exports['application/vnd.myscript.jiix']) {
                    this.lastJiix = JSON.parse(exports['application/vnd.myscript.jiix']);
                    // map timestamp to strokes
                    const timestampWord = new Map<number, string>();
                    this.lastJiix.words.map((word: any) => {
                        if (word.items) {
                            word.items.forEach((i: { id: string; timestamp: string; X: Array<number>; Y: Array<number>; F: Array<number> }) => {
                                const ms = Date.parse(i.timestamp);
                                timestampWord.set(ms, word.label);
                            });
                        }
                    });

                    const wordInkDocMap = new Map<string, Doc[]>();
                    if (this.currGroup) {
                        const docList = DocListCast(this.currGroup.data);
                        docList.forEach((inkDoc: Doc) => {
                            // just having the times match up and be a unique value (actual timestamp doesn't matter)
                            const ms = DateCast(inkDoc.author_date).getDate().getTime() + 14400000;
                            const word = timestampWord.get(ms);
                            if (!word) {
                                return;
                            }
                            const entry = wordInkDocMap.get(word);
                            if (entry) {
                                entry.push(inkDoc);
                                wordInkDocMap.set(word, entry);
                            } else {
                                const newEntry = [inkDoc];
                                wordInkDocMap.set(word, newEntry);
                            }
                        });
                        if (this.lastJiix.words.length > 1) this.subgroupsTranscriptions(wordInkDocMap);
                    }
                }
                const text = exports['label'];

                if (this.currGroup && text) {
                    DocumentView.getDocumentView(this.currGroup)?.ComponentView?.updateIcon?.();
                    this.currGroup.transcription = text;
                    this.currGroup.title = text;
                    let image = await this.getIcon();
                    const pathname = image?.url.href as string;
                    console.log(image?.url);
                    console.log(image);
                    const { href } = (image as URLField).url;
                    const hrefParts = href.split('.');
                    const hrefComplete = `${hrefParts[0]}_o.${hrefParts[1]}`;
                    let response;
                    try {
                        const hrefBase64 = await this.imageUrlToBase64(hrefComplete);
                        response = await gptHandwriting(hrefBase64);
                        console.log(response);
                    } catch (error) {
                        console.log('bad things have happened');
                    }
                    const textBoxText = 'iink: ' + text + '\n' + '\n' + 'ChatGPT: ' + response;
                    if (!this.currGroup.hasTextBox) {
                        const newDoc = Docs.Create.TextDocument(textBoxText, { title: '', x: this.currGroup.x as number, y: (this.currGroup.y as number) + (this.currGroup.height as number) });
                        newDoc.height = 200;
                        this.collectionFreeForm?.addDocument(newDoc);
                        this.currGroup.hasTextBox = true;
                    }
                    ref.editor.clear();
                }
            }
        }
    };
    async getIcon() {
        const docView = DocumentView.getDocumentView(this.currGroup);
        console.log(this.currGroup);
        if (docView) {
            console.log(docView);
            docView.ComponentView?.updateIcon?.();
            return new Promise<ImageField | undefined>(res => setTimeout(() => res(ImageCast(docView.Document.icon)), 1000));
        }
        return undefined;
    }
    imageUrlToBase64 = async (imageUrl: string): Promise<string> => {
        try {
            const response = await fetch(imageUrl);
            const blob = await response.blob();

            return new Promise((resolve, reject) => {
                const reader = new FileReader();
                reader.readAsDataURL(blob);
                reader.onloadend = () => resolve(reader.result as string);
                reader.onerror = error => reject(error);
            });
        } catch (error) {
            console.error('Error:', error);
            throw error;
        }
    };

    /**
     * Creates the ink grouping once the user leaves the writing mode.
     */
    createInkGroup() {
        // TODO nda - if document being added to is a inkGrouping then we can just add to that group
        if (Doc.ActiveTool === InkTool.Write) {
            CollectionFreeFormView.collectionsWithUnprocessedInk.forEach(ffView => {
                // TODO: nda - will probably want to go through ffView unprocessed docs and then see if any of the inksToGroup docs are in it and only use those
                const selected = ffView.unprocessedDocs;
                const newCollection = this.groupInkDocs(
                    selected.filter(doc => doc.embedContainer),
                    ffView
                );
                ffView.unprocessedDocs = [];

                InkTranscription.Instance.transcribeInk(newCollection, selected, false);
            });
        }
        CollectionFreeFormView.collectionsWithUnprocessedInk.clear();
    }

    /**
     * Creates the groupings for a given list of ink docs on a specific doc view
     * @param selected: the list of ink docs to create a grouping of
     * @param docView: the view in which we want the grouping to be created
     * @param word: optional param if the group we are creating is a word (subgrouping individual words)
     * @returns a new collection Doc or undefined if the grouping fails
     */
    groupInkDocs(selected: Doc[], docView: CollectionFreeFormView, word?: string): Doc | undefined {
        this.collectionFreeForm = docView;
        const bounds: { x: number; y: number; width?: number; height?: number }[] = [];

        // calculate the necessary bounds from the selected ink docs
        selected.map(
            action(d => {
                const x = NumCast(d.x);
                const y = NumCast(d.y);
                const width = NumCast(d._width);
                const height = NumCast(d._height);
                bounds.push({ x, y, width, height });
            })
        );

        // calculate the aggregated bounds
        const aggregBounds = aggregateBounds(bounds, 0, 0);
        const marqViewRef = docView._marqueeViewRef.current;

        // set the vals for bounds in marqueeView
        if (marqViewRef) {
            marqViewRef._downX = aggregBounds.x;
            marqViewRef._downY = aggregBounds.y;
            marqViewRef._lastX = aggregBounds.r;
            marqViewRef._lastY = aggregBounds.b;
        }

        // map through all the selected ink strokes and create the groupings
        selected.map(
            action(d => {
                const dx = NumCast(d.x);
                const dy = NumCast(d.y);
                delete d.x;
                delete d.y;
                delete d.activeFrame;
                delete d._timecodeToShow; // bcz: this should be automatic somehow.. along with any other properties that were logically associated with the original collection
                delete d._timecodeToHide; // bcz: this should be automatic somehow.. along with any other properties that were logically associated with the original collection
                // calculate pos based on bounds
                if (marqViewRef?.Bounds) {
                    d.x = dx - marqViewRef.Bounds.left - marqViewRef.Bounds.width / 2;
                    d.y = dy - marqViewRef.Bounds.top - marqViewRef.Bounds.height / 2;
                }
                return d;
            })
        );
        docView.props.removeDocument?.(selected);
        // Gets a collection based on the selected nodes using a marquee view ref
        const newCollection = MarqueeView.getCollection(selected, undefined, true, { top: 1, left: 1, width: 1, height: 1 });
        if (newCollection) {
            newCollection.width = NumCast(newCollection._width);
            newCollection.height = NumCast(newCollection._height);
            // if the grouping we are creating is an individual word
            if (word) {
                newCollection.title = word;
            }
        }

        // nda - bug: when deleting a stroke before leaving writing mode, delete the stroke from unprocessed ink docs
        newCollection && docView.props.addDocument?.(newCollection);
        if (newCollection) {
            newCollection.hasTextBox = false;
        }
        return newCollection;
    }

    render() {
        return (
            <div className="ink-transcription">
                <div className="math-editor" ref={this.setMathRef} touch-action="none"></div>
                <div className="text-editor" ref={this.setTextRef} touch-action="none"></div>
            </div>
        );
    }
}
