import os
import numpy as np

from concurrent.futures import ThreadPoolExecutor

from .depth_img import depth_image_list
from .rmbg import mask_image_list
from .mask_depth import depth_apply_mask_pilimg_list
from .read_folder import read_images_in_order
from .frame_extract import frame_extractor
from modules.video_processing.image_qa_doubao import show_detect_doubao
from .common import filter_json_files_for_show, find_show_cut
from modules.video_processing.video_cut import moviepy_cut
from .add_music import add_music_to_video
from .aide_cut import aide_cut
from .path_config import PathConfig

path_config = PathConfig()

from .logger_config import setup_logger

logger = setup_logger(__name__)


def depth_for_cut(image_list):
    """
    定位走秀视频片段

    Args:
        image_list (list): 抽帧原图像列表

    Returns:
        masked_images: 深度图掩码图像列表
        depth_means: 深度图深度均值列表
    """
    depths = depth_image_list(image_list)
    masks = mask_image_list(image_list)
    
    masked_depths, depth_means = depth_apply_mask_pilimg_list(depths, masks, image_list)
    

    return masked_depths, depth_means

def show_cut(audio_json):
    
    # 0、裁切助播讲品视频片
    video_path = aide_cut(audio_json)

    # 1、抽帧保存：每隔60秒抽一帧
    frame_extractor(video_path, interval_sec=3, output_dir=path_config.get_path('frame_for_show'))
    logger.info("抽帧：done!")

    # 2、单帧理解：图像是否有人，多少个人，在图像中的位置
    images = read_images_in_order(path_config.get_path('frame_for_show'))
    show_detect_doubao(images)
    logger.info("帧理解：done!!!")

    # 3、筛选：找到一个人站在画面中央的图像
    filter_images = filter_json_files_for_show(str(path_config.get_path('caption_for_show')))
    # files = read_images_in_order("./data/key_frame/for_show/")
    # filter_images = files
    logger.info("查找人站中央：done!")
    print(f"{filter_images}")

    # 4、计算筛选图像的深度均值
    masked_depths, depth_means = depth_for_cut(filter_images)
    logger.info("计算深度均值")

    # 5、基于深度均值查找裁切图像索引
    logger.info(f"深度图差值阈值：{int(np.mean(depth_means) / 8)}")
    cut_index = find_show_cut(depth_means, n = int(np.mean(depth_means) / 8))

    # 6、计算裁切时间戳
    cut_start = int(filter_images[cut_index[0]].split('_')[-1].split('.')[0])
    cut_end = int(filter_images[cut_index[1]].split('_')[-1].split('.')[0])
    logger.info(f"定位走秀时间戳：{cut_start} - {cut_end}")

    # 7、裁切出走秀片段
    output_path = f"output/video_clips/show-{os.path.splitext(os.path.basename(video_path))[0]}.mp4"
    moviepy_cut(video_path, output_path, cut_start, cut_end)
    logger.info(f"裁切走秀片段：{output_path}")

    # 8、添加背景音乐
    music_video = output_path.replace("show", "music-show")
    add_music_to_video(output_path, "data/music_file/music.mp3", fadein_duration=1, fadeout_duration=1, output_path=music_video)
    logger.info(f"添加背景音乐：{music_video}")

    return music_video

if __name__ == "__main__":

    show_cut("data/sub_video/clip.mp4")

    # TODO:是否手拿衣服，是否手拿平板？
