import { IStream } from "@futureporn/types";
import { IVod } from "@/app/lib/vods";
import Link from "next/link";
import Image from "next/legacy/image";
import { LocalizedDate } from "./localized-date";
import { FontAwesomeIcon, FontAwesomeIconProps } from "@fortawesome/react-fontawesome";
import { faTriangleExclamation, faCircleInfo, faThumbsUp, IconDefinition, faO, faX, faExternalLinkAlt } from "@fortawesome/free-solid-svg-icons";
import { faXTwitter } from "@fortawesome/free-brands-svg-icons";
import { notFound } from "next/navigation";
import ProtectedRoute from "./protected-route";

export interface IStreamProps {
    stream: IStream;
}
type Status = 'missing' | 'issue' | 'good';
interface StyleDef {
    heading: string;
    icon: IconDefinition;
    description: string;
    prompt: string;
}

function capitalizeFirstLetter(string: string): string {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

function hasNote(vod: IVod) {
    if (!!vod?.note) return true;
    else return false;
}

function determineStatus(stream: IStream): Status {
    if (stream.vods.length < 1) {
        return 'missing'
    } else {
        if (stream.vods.some((vod: IVod) => !hasNote(vod))) {
            return 'good';
        } else {
            return 'issue';
        }
    }
}

export default function StreamPage({ stream }: IStreamProps) {
    console.log('StreamPage function has been invoked! stream as follows')
    console.log(stream)
    if (!stream) notFound()
    const displayName = stream.vtuber.display_name;
    const date = new Date(stream.date);
    const selectedStatus = determineStatus(stream);

    const styleMap: Record<Status, StyleDef> = {
        'missing': {
            heading: 'is-danger',
            icon: faTriangleExclamation,
            description: "We don't have a VOD for this stream.",
            prompt: 'Know someone who does?'
        },
        'issue': {
            heading: 'is-warning',
            icon: faCircleInfo,
            description: "We have a VOD for this stream, but it's not full quality.",
            prompt: 'Have a better copy?'
        },
        'good': {
            heading: 'is-success',
            icon: faThumbsUp,
            description: "We have a VOD for this stream, and we think it's the best quality possible.",
            prompt: "Have one that's even better?"
        }
    };
    const { heading, icon, description, prompt } = styleMap[selectedStatus] || {};


    if (!stream) return <p>NotFound</p>
    // <NotFound></NotFound>

    // return <p>
    //     <pre>
    //         <code>
    //             {JSON.stringify(stream, null, 2)}

    //         </code>
    //     </pre>

    // </p>
    // const platformsList = [
    //     stream.attributes.isChaturbateStream ? 'Chaturbate' : null,
    //     stream.attributes.isFanslyStream ? 'Fansly' : null
    // ].filter(Boolean).join(', ');
    // platformsList = platformsArray.length > 0 ? platformsArray.join(', ') : 'None';
    
    // const platformsList = [
    //     (stream.attributes.isChaturbateStream && 'CB'), 
    //     (stream.attributes.isFanslyStream && 'Fansly')
    // ].filter(Boolean).join(', ')

    const platformsList = [
        (stream.is_chaturbate_stream && 'CB'), 
        (stream.is_fansly_stream && 'Fansly')
    ].filter(Boolean).join(', ') || '!!!';

    

    return (
        <>


            <div className="content">
                <div className="section">
                    <h1 className="title"><LocalizedDate date={date} /> {displayName} Stream Archive</h1>
                </div>

                <div className="section columns is-multiline">
                    <div className="column is-half">
                        <div className="box">
                            <div className="columns is-multiline">
                                <div className="column is-full">
                                    <table className="table">
                                        <thead>
                                            <tr>
                                                <th className="is-family-sans-serif">Description</th>
                                                <th className="is-family-sans-serif">Details</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>Platform</td>
                                                <td>{platformsList}</td>
                                            </tr>
                                            <tr>
                                                <td>UTC Datetime</td>
                                                <td><time dateTime={date.toISOString()}>{date.toISOString()}</time></td>
                                            </tr>
                                            <tr>
                                                <td>Local Datetime</td>
                                                <td>{date.toLocaleDateString()} {date.toLocaleTimeString()}</td>
                                            </tr>
                                        </tbody>
                                    </table>

                                </div>
                            </div>
                        </div>
                    </div>


                    <div className="column is-half">
                        <article className={`message ${heading}`}>
                            <div className="message-header">
                                <span>VOD {capitalizeFirstLetter(selectedStatus)}</span>
                            </div>
                            <div className="message-body has-text-centered">
                                <span className="title is-1"><FontAwesomeIcon icon={icon}></FontAwesomeIcon></span>
                                <p className="mt-3">{description}</p>
                                <ProtectedRoute requiredUserRole="patron" accessDenied={<span></span>} >
                                    <p className="mt-5">{prompt}<br />
                                    <Link href={`/upload?uuid=${stream.uuid}`}>Upload it here.</Link></p>
                                </ProtectedRoute>
                            </div>
                        </article>
                    </div>

                </div>


                {stream.vods.length !== 0 &&
                <div className="section">
                    <h1 className="title">VODs</h1>
                    <table className="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Upload Date</th>
                                {/* <th>Thumbnail</th>
                                <th>Duration</th> */}
                                <th>Tags</th>
                                <th>Timestamps</th>
                                <th>Note</th>
                            </tr>
                        </thead>
                        <tbody>
                            {stream.vods.map((vod: IVod) => (
                                <tr key={vod.id}>
                                    {/* <p>{JSON.stringify(vod, null, 2)}</p> */}
                                    <td><Link href={`/vt/${stream.vtuber.slug}/vod/${vod.uuid}`}>{vod.uuid}</Link></td>
                                    <td>{vod.publishedAt}</td>
                                    {/* <td>{(!!vod?.attributes?.thumbnail?.data?.attributes?.cdnUrl) ? <Image alt="" src={vod.thumbnail.cdn_url}></Image> : <FontAwesomeIcon icon={faX} />}</td>
                                    <td>{(!!vod?.attributes?.duration) ? vod.duration : <FontAwesomeIcon icon={faX} />}</td> */}
                                    <td>{vod.tagVodRelations.length}</td>
                                    <td>{vod.timestamps.length}</td>
                                    <td>{(!!vod.note) ? <FontAwesomeIcon icon={faO} /> : <FontAwesomeIcon icon={faX} />}</td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                </div>}

            </div>

        </>
    )
}
