# -*- coding: utf-8 -*-
import os
import random
import sys
import datetime


sys.path.append(os.getcwd())
from common.feishu_utils import Feishu


class Material():
    """
    获取汇总表所有负责人列表
    """
    @classmethod
    def feishu_list(cls):
        summary = Feishu.get_values_batch("summary", "bc154d")
        list = []
        for row in summary[1:]:
            mark = row[0]
            name = row[1]
            feishu_id = row[3]
            feishu_sheet = row[4]
            cookie_sheet = row[5]
            number = {"mark": mark, "name": name, "feishu_id": feishu_id, "feishu_sheet": feishu_sheet, "cookie_sheet": cookie_sheet}
            if mark:
                list.append(number)
            else:
                return list
        return list

    """
    获取对应负责人任务明细
    """
    @classmethod
    def get_task_data(cls, feishu_id, feishu_sheet):
        data = Feishu.get_values_batch(feishu_id, feishu_sheet)
        processed_list = []

        for row in data[1:]:
            channel_id = row[1]
            channel_url = row[2]
            piaoquan_id = row[3]
            number = row[4]
            video_share = row[5]
            video_ending = row[6]
            crop_tool = row[7]
            gg_duration = row[8]
            title = row[9]

            def count_items(item, separator):
                if item and item not in {'None', ''}:
                    return len(item.split(separator))
                return 0

            video_id_total = count_items(str(channel_url), ',')
            title_total = count_items(str(title), '/')
            video_ending_total = count_items(str(video_ending), ',')

            values = [channel_id, video_id_total, piaoquan_id, video_share, video_ending_total, crop_tool, gg_duration, title_total]
            filtered_values = [str(value) for value in values if value is not None and value != "None"]
            task_mark = "_".join(map(str, filtered_values))

            if piaoquan_id and piaoquan_id not in {'None', ''}:
                number_dict = {
                    "task_mark": task_mark,
                    "channel_id": channel_id,
                    "channel_url": channel_url,
                    "piaoquan_id": piaoquan_id,
                    "number": number,
                    "title": title,
                    "video_share": video_share,
                    "video_ending": video_ending,
                    "crop_total": crop_tool,
                    "gg_duration_total": gg_duration,
                }
                processed_list.append(number_dict)
            else:
                return processed_list

        return processed_list

    """
    获取对应片尾+srt
    """
    @classmethod
    def get_pwsrt_data(cls, feishu_id, feishu_sheet, video_ending):
        data = Feishu.get_values_batch(feishu_id, feishu_sheet)

        for row in data[1:]:
            pw_mark = row[0]
            pw_id = row[1]
            pw_srt = row[2]
            if pw_id != 'None' and pw_id != '' and pw_id != None:
                if pw_mark == video_ending:
                    number = {"pw_id": pw_id, "pw_srt": pw_srt}
                    return number
        return ''

    """
    获取对应固定字幕
    """
    @classmethod
    def get_pzsrt_data(cls, feishu_id, feishu_sheet, video_share_name):
        data = Feishu.get_values_batch(feishu_id, feishu_sheet)

        for row in data[1:]:
            pz_mark = row[0]
            pz_zm = row[1]
            if pz_zm != 'None' and pz_zm != '' and pz_zm != None:
                if pz_mark == video_share_name:
                    return pz_zm
        return ''



    """
    获取 cookie 信息
    """
    @classmethod
    def get_cookie_data(cls, feishu_id, cookie_sheet, channel):
        data = Feishu.get_values_batch(feishu_id, cookie_sheet)
        for row in data[1:]:
            channel_mask = row[0]
            cookie = row[1]
            if channel_mask == channel:
                return cookie