'''
Author: Thund1r thund1r@foxmail.com
Date: 2022-09-22 14:34:31
LastEditTime: 2022-10-18 10:59:14
Description: å¤„ç�†æ‰€æœ‰æ•°æ�®ä¸�ç½‘é¡µ

Copyright (c) 2022 by Thund1r thund1r@foxmail.com, All Rights Reserved. 
'''
# -*- coding: utf8 -*-
import config
import os
import api
import days
import diy
import weather
import covid

link = config.get("link")
pictype = config.get("pictype")
msgtype = str(config.get("msgtype")) if config.get("msgtype") else "1"
agentid = config.get("agentid")


# å¤„ç�†æ‰€æœ‰ä¿¡æ�¯
def handle_msg():
    info_list = []
    multi_list = []
    pic_type = pictype
    own_link = link
    own_pic = diy.get_my_pic()
    own_title = diy.get_my_title()
    own_content = diy.get_my_content()
    info_list.append(own_content)

    # æ�¥å�—ä»Šæ—¥æ—¥æœŸæ•°æ�®
    today_data = days.get_today()
    today_date = today_data["today_date"]

    # æ�¥æ”¶å¿…åº”æ•°æ�®
    bing_pic = ""
    bing_tip = ""
    bing_flag = 1
    bing_data = api.get_bing()
    if bing_data:
        bing_pic = bing_data["bing_pic"]
        bing_tip = bing_data["bing_tip"]

    art_title = today_date
    art_content = own_content
    art_pic = api.get_random_pic()
    if own_pic or own_title or pic_type:
        if own_pic:
            art_pic = own_pic
        if own_title:
            art_title += "\n" + own_title
        multi_list.append(handle_multi(
            art_title, art_title, art_content, art_pic, None))

    elif bing_tip and bing_pic:
        art_pic = bing_pic
        art_title = art_title + "\n" + bing_tip
        multi_list.append(handle_multi(
            art_title, art_title, art_content, art_pic, None))
        bing_flag = 0
    art_pic = art_pic if pic_type != "none" else None

    # ä¸‹é�¢åŠ å…¥å�„æ•°æ�®çš„é¡ºåº�å�³åœ¨å�¡ç‰‡ä¸Šæ˜¾ç¤ºçš„é¡ºåº�
    # ä¸�éœ€è¦�çš„æ•°æ�®è¯·åœ¨ä¸‹é�¢å¯¹åº”çš„æ®µè�½æ“�ä½œ
    # ä¸�éœ€è¦�å‡ºç�°åœ¨å�•å›¾æ–‡çš„è¯·åˆ é™¤info_list.append(XXX)
    # ä¸�éœ€è¦�å‡ºç�°åœ¨å¤šå›¾æ–‡çš„è¯·åˆ é™¤multi_list.append(XXX)
    # éƒ½ä¸�è¦�çš„æ•°æ�®ç›´æ�¥åˆ é™¤ä¸€æ•´æ®µå�³å�¯

    # åŠ å…¥å¤©æ°”æ•°æ�®
    weather_tip = weather.get_map_weather()
    if weather_tip:
        info_list.append(weather_tip)
        multi_list.append(handle_multi(
            weather_tip, "Weather", weather_tip, None, None))

    # åŠ å…¥æ—¥æœŸæ��é†’æ•°æ�®
    days_tip = days.get_map_days()
    if days_tip:
        info_list.append(days_tip)
        multi_list.append(handle_multi(
            days_tip, "Days", days_tip, None, None))

    # åŠ å…¥ç–«æƒ…æ•°æ�®
    yq_tip = covid.get_map_yq()
    if yq_tip:
        info_list.append(yq_tip)
        multi_list.append(handle_multi(
            yq_tip, "COVID-19", yq_tip, None, None))

    # åŠ å…¥å¿…åº”æ•°æ�®
    if bing_flag and bing_pic and bing_tip:
        multi_list.append(handle_multi(
            "ğŸ–¼ï¸� "+bing_tip, "Bing", "ğŸ–¼ï¸� "+bing_tip, bing_pic, None))

    # åŠ å…¥é‡‘å±±è¯�éœ¸æ•°æ�®
    ciba_data = api.get_ciba()
    if ciba_data:
        ciba_tip = ciba_data["ciba_tip"]
        ciba_pic = ciba_data["ciba_pic"]
        info_list.append(ciba_tip)
        multi_list.append(handle_multi(
            ciba_tip, "iCiba", ciba_tip, ciba_pic, None))

    # åŠ å…¥ONEä¸€ä¸ªæ•°æ�®
    one_data = api.get_one()
    if one_data:
        one_tip = one_data["one_tip"]
        one_pic = one_data["one_pic"]
        info_list.append(one_tip)
        multi_list.append(handle_multi(
            one_tip, "ONEÂ·ä¸€ä¸ª", one_tip, one_pic, None))

    # åŠ å…¥è‡ªå®šä¹‰XXXæ•°æ�®å�¯ä»¥æ”¾ç½®åœ¨ä¸‹æ–¹ï¼ŒåŠ¡å¿…æ³¨æ„�ç¼©è¿›ï¼Œæ ¼å¼�å�‚è€ƒä¸Šæ–¹çš„åŠ å…¥ONEä¸€ä¸ªæ•°æ�®
    # # åŠ å…¥XXXå›¾æ–‡æ•°æ�®
    # XXX_data = api.get_XXX()
    # if XXX_data:
    #     XXX_tip = XXX_data["XXX_tip"]
    #     # æ²¡æœ‰picå°±åˆ é™¤ä¸‹é�¢è¿™ä¸€å�¥
    #     XXX_pic = XXX_data["XXX_pic"]
    #     # å�•å›¾æ–‡æ·»åŠ æ•°æ�®ï¼Œä¸�éœ€è¦�å°±åˆ é™¤ä¸‹é�¢è¿™ä¸€å�¥
    #     info_list.append(XXX_tip)
    #     # å¤šå›¾æ–‡æ·»åŠ æ•°æ�®ï¼Œä¸�éœ€è¦�å°±åˆ é™¤ä¸‹é�¢è¿™ä¸€æ•´å�¥
    #     multi_list.append(handle_multi(
    #         out_title, inner_title, content, pic, link))
    #     # out_titleå¤šå›¾æ–‡å�¡ç‰‡æ ‡é¢˜, inner_titleå¤šå›¾æ–‡å±•ç¤ºé¡µå†…æ ‡é¢˜, contentå¤šå›¾æ–‡å†…å®¹, picå¤šå›¾æ–‡å¤´å›¾, linkè‡ªå®šä¹‰è·³è½¬é“¾æ�¥
    #     # æ²¡æœ‰çš„å�‚æ•°å°±å¡«Noneï¼Œä¸�èƒ½äº”ä¸ªå�‚æ•°éƒ½æ²¡æœ‰

    # å¤„ç�†æ–‡æœ¬æ ¼å¼�
    art_content = "\n\n".join(info_list)
    html_content = art_content.replace("\n", "\\n")
    page_content = replace_symbol(art_content)
    page_title = replace_symbol(art_title)
    page_pic = art_pic

    art_url = None
    beta_url = None
    if own_link:
        beta_url = f"{own_link}?t={page_title}&p={page_pic}&c={page_content}"
        art_url = beta_url if len(
            beta_url) < 1000 else beta_url[:1000]+"Â·Â·Â·Â·Â·Â·"

    article = ""
    if msgtype == "1":
        article = [{
            "title": art_title,
            "description": art_content,
            "url": art_url,
            "picurl": art_pic
        }]
    else:
        article = list(filter(None, multi_list))

    # å°�è£…ä¼�ä¸šå¾®ä¿¡æ•°æ�®
    wecom_data = {
        "touser": "@all",
        "toparty": "",
        "totag": "",
        "msgtype": "news",
        "agentid": agentid,
        "news": {
            "articles": article
        },
        "enable_id_trans": 0,
        "enable_duplicate_check": 0,
        "duplicate_check_interval": 1800
    }

    # å°�è£…æµ‹è¯•å�·æ•°æ�®
    beta_data = {
        "art_url": beta_url,
        "art_content": art_content
    }

    # å°�è£…htmlå�¯ç”¨æ•°æ�®
    html_data = {
        "p": art_pic,
        "t": art_title,
        "c": html_content
    }

    # å°�è£…æ‰€æœ‰æ•°æ�®
    msg_data = {
        "wecom_data": wecom_data,
        "html_data": html_data,
        "beta_data": beta_data
    }

    return msg_data


# å¤„ç�†å¤šå›¾æ–‡å†…å®¹
def handle_multi(out_title, inner_title, content, pic, art_link):
    if msgtype != "2":
        return None
    if out_title or inner_title or content or pic or art_link:
        if out_title is None:
            if content:
                out_title = content
            elif inner_title:
                out_title = inner_title
            else:
                out_title = "æŸ¥çœ‹å›¾ç‰‡"
        picurl = pic or api.get_random_pic()
        inner_title = replace_symbol(inner_title)
        content = replace_symbol(content)

        multi_url = None
        if art_link is None:
            own_link = link
            if own_link:
                multi_url = f"{own_link}?t={inner_title}&p={picurl}&c={content}"
                if len(multi_url) > 1000:
                    multi_url = multi_url[:1000]+"Â·Â·Â·Â·Â·Â·"
        else:
            multi_url = art_link

        return {
            "title": out_title,
            "url": multi_url,
            "picurl": picurl
        }
    else:
        print("å¤šå›¾æ–‡æ²¡æœ‰ä»»ä½•å†…å®¹ï¼Œç”Ÿæˆ�é“¾æ�¥å¤±è´¥")
        return None

# å¤„ç�†ä¸ªåˆ«å­—ç¬¦æ›¿æ�¢


def replace_symbol(str_data):
    return str_data.replace("&", "%26").replace(
        "'", "%27").replace("\n", "\\n") if str_data else None


# å¤„ç�†å›¾æ–‡è¯¦æƒ…é¡µ
def handle_html(html_data):
    with open(os.path.join(os.path.dirname(__file__), "show.html"), 'r', encoding='utf-8') as f:
        html = f.read()
    p = html_data.get("p")
    t = html_data.get("t")
    c = html_data.get("c")
    if p and p != "none" and p != "None":
        html = html.replace('class="pic" style="display:none;',
                            'class="pic" style="').replace("<&p&>", p)
    if t and t != "none" and t != "None":
        t = t.replace("\\n", "<br/>")
        html = html.replace('class="title" style="display:none;',
                            'class="title" style="').replace("<&t&>", t)
    if c and c != "none" and c != "None":
        c = c.replace("\\n", "<br/>")
        html = html.replace('class="content" style="display:none;',
                            'class="content" style="').replace("<&c&>", c)
    return html
