# gui/video_manager.py

import os

import streamlit as st

from rcdl.core.config import Config
from rcdl.core.models import Status, Media
from rcdl.core.db import DB


previous_statuses = {}


def set_status(media: Media, status: Status):
    key = media.post_id + media.url
    previous_statuses[key] = media.status
    media.status = status
    with DB() as db:
        db.update_media(media)
    print(f"Set {media.post_id} to {status.value}")

    for m in st.session_state.medias:
        if m.post_id == media.post_id and m.url == media.url:
            m.status = status
            break


def video_manager():
    st.title("Video Manager")

    # load db
    if "medias" not in st.session_state:
        with DB() as db:
            st.session_state.medias = db.query_medias_by_status_sorted(
                [Status.DOWNLOADED, Status.OPTIMIZED],
                sort_by="file_size",
                ascending=True,
            )

    medias = st.session_state.medias
    if not medias:
        st.info("No media found")
        return

    # session state
    if "media_index" not in st.session_state:
        st.session_state.media_index = 0

    idx = st.session_state.media_index
    media = medias[idx]

    # media info
    st.subheader(f"Media {idx + 1} / {len(medias)}")

    with DB() as db:
        post = db.query_post_by_id(media.post_id)
        if post is None:
            st.info("No matching post found")
            return

    col_video, col_info = st.columns([1, 2])
    with col_info:
        col1, col2 = st.columns(2)
        with col1:
            st.write("**Post ID:**", media.post_id)
            st.write("**Service:**", media.service)
            st.write("**User:**", post.user)
            st.write("**Duration:**", media.duration, "s")
            st.write("**Sequence:**", media.sequence)
            st.write("**Size:**", round(media.file_size / (1024 * 1024), 1), "MB")
            st.write("**Status:**", media.status)
            key = media.post_id + media.url
            if key in previous_statuses:
                st.write("**PREV STATUS:**", previous_statuses[key])
            st.write("**Path:**", media.file_path)
            st.write("**Created at**:", media.created_at[0:16])

        with col2:
            # controls
            c1, c2, c3 = st.columns(3)
            with c1:
                if st.button("⏮ Prev", disabled=idx == 0):
                    st.session_state.media_index -= 1
                    st.rerun()
            with c2:
                if st.button("⏭ Next", disabled=idx >= len(medias) - 1):
                    st.session_state.media_index += 1
                    st.rerun()
            with c3:
                if st.button("Remove"):
                    set_status(media, Status.TO_BE_DELETED)
                    st.rerun()
                if st.button("Not Remove"):
                    key = media.post_id + media.url
                    if key in previous_statuses:
                        set_status(media, previous_statuses[key])
                    else:
                        print("Not in previous status")
                    st.rerun()

    # video player
    full_path = os.path.join(Config.creator_folder(post.user), media.file_path)
    if os.path.exists(full_path):
        with col_video:
            with st.container():
                st.video(
                    full_path,
                    autoplay=True,
                    loop=True,
                )
    else:
        st.error(f"Video file {full_path} not found on disk")
