import { Button, IconButton, Size, Type } from 'browndash-components';
import * as React from 'react';
import { BsBookmark, BsBookmarkFill } from 'react-icons/bs';
import { MdTravelExplore } from 'react-icons/md';
import { Doc } from '../../../../fields/Doc';
import { StrCast } from '../../../../fields/Types';
import { LightboxView } from '../../LightboxView';
import { Colors } from '../../global/globalEnums';
import { NewLightboxView } from '../NewLightboxView';
import { EditableText } from '../components/EditableText';
import { getType } from '../utils';
import './LightboxHeader.scss';
import { INewLightboxHeader } from './utils';

export function NewLightboxHeader(props: INewLightboxHeader) {
    const { height = 100, width } = props;
    const [doc, setDoc] = React.useState<Doc | undefined>(LightboxView.LightboxDoc);
    const [editing, setEditing] = React.useState<boolean>(false);
    const [title, setTitle] = React.useState<JSX.Element | null>(null);
    React.useEffect(() => {
        const lbDoc = LightboxView.LightboxDoc;
        setDoc(lbDoc);
        if (lbDoc) {
            setTitle(
                <EditableText
                    editing={editing}
                    text={StrCast(lbDoc.title)}
                    onEdit={(newText: string) => {
                        if (lbDoc) lbDoc.title = newText;
                    }}
                    setEditing={setEditing}
                />
            );
        }
    }, [LightboxView.LightboxDoc]);

    const [saved, setSaved] = React.useState<boolean>(false);

    if (!doc) return null;
    return (
        <div className="newLightboxHeader-container" onPointerDown={e => e.stopPropagation()} style={{ minHeight: height, height: height, width: width }}>
            <div className="title-container">
                <div className="lb-label">Title</div>
                {title}
            </div>
            <div className="type-container">
                <div className="lb-label">Type</div>
                <div className="type">{getType(StrCast(doc.type))}</div>
            </div>
            <div style={{ gridColumn: 2, gridRow: 1, height: '100%', display: 'flex', justifyContent: 'flex-end', alignItems: 'center' }}>
                <IconButton size={Size.XSMALL} onClick={() => setSaved(!saved)} color={Colors.DARK_GRAY} icon={saved ? <BsBookmarkFill /> : <BsBookmark />} />
                <IconButton size={Size.XSMALL} onClick={() => setSaved(!saved)} color={Colors.DARK_GRAY} icon={saved ? <BsBookmarkFill /> : <BsBookmark />} />
            </div>
            <div style={{ gridColumn: 2, gridRow: 2, height: '100%', display: 'flex', justifyContent: 'flex-end', alignItems: 'center' }}>
                <Button onClick={() => NewLightboxView.SetExploreMode(!NewLightboxView.ExploreMode)} size={Size.XSMALL} color={Colors.DARK_GRAY} type={Type.SEC} text="t-SNE 2D Embeddings" icon={<MdTravelExplore />} />
            </div>
        </div>
    );
}
