import openpyxl

from utils.openai_api import get_tokens_length


def read_xlsx_file(file_name):
    workbook = openpyxl.load_workbook(file_name)
    sheet = workbook.active
    data = []

    for row in sheet.iter_rows(min_row=2, values_only=True):
        data.append({
            '发表时间': row[1],
            '作者': row[2],
            '评级': row[3],
            '标题': row[4],
            '内容': row[5]
        })

    return data[2:]


def process_data(max_length,
                 data,
                 message,
                 length,
                 count_now=0,
                 reply=""):
    temp = []
    result_message = ""
    data = data[count_now:]

    while len(data) > 0:
        temp_message = message
        ele = data.pop(0)
        temp_string = "({}, {})".format(ele["标题"], ele["内容"])
        temp.append(temp_string)
        temp_message = temp_message.format(
            reply_before=reply,
            comment_num=count_now,
            comment_num_start=count_now + 1,
            comment_num_end=count_now + len(temp),
            all_num=length,
            data_string="[{}]".format(", ".join(temp)))

        if len(temp_message) > max_length:
            break
        result_message = temp_message
    return result_message, len(temp)
