'''
Author: Thund1r thund1r@foxmail.com
Date: 2022-09-22 14:26:19
LastEditTime: 2023-01-05 22:29:34
Description: æ—¥æœŸæ•°æ�®

Copyright (c) 2022 by Thund1r thund1r@foxmail.com, All Rights Reserved. 
'''
# -*- coding: utf8 -*-
import config
from datetime import datetime, date, timedelta
from zhdate import ZhDate
import random

nowdatetime = (datetime.utcnow() + timedelta(hours=8))
targetday_list = config.get_list("targetday")
targetname_list = config.get_list("targetname")
beginday_list = config.get_list("beginday")
beginname_list = config.get_list("beginname")
call = config.get("call")


# è®¡ç®—çºªå¿µæ—¥
def get_remain(target_day, target_name):
    ndt = nowdatetime
    today = date(ndt.year, ndt.month, ndt.day)
    this_year = datetime.now().year
    target_day_year = target_day.split("-")[0]
    if target_day_year[0] == "n":
        lunar_mouth = int(target_day.split("-")[1])
        lunar_day = int(target_day.split("-")[2])
        last_date = ZhDate(this_year-1, lunar_mouth,
                           lunar_day).to_datetime().date()
        this_date = ZhDate(this_year, lunar_mouth,
                           lunar_day).to_datetime().date()
        if today <= last_date:
            this_date = last_date
    else:
        solar_month = int(target_day.split("-")[1])
        solar_day = int(target_day.split("-")[2])
        this_date = date(this_year, solar_month, solar_day)
    if today == this_date:
        remain_day = 0
        remain_tip = f"ğŸŒŸ {target_name}å°±æ˜¯ä»Šå¤©å•¦ï¼�"
    elif today > this_date:
        if target_day_year[0] == "n":
            lunar_next_date = ZhDate(
                (this_year + 1), lunar_mouth, lunar_day).to_datetime().date()
            next_date = date(
                lunar_next_date.year, lunar_next_date.month, lunar_next_date.day)
        else:
            next_date = date(
                (this_year + 1), solar_month, solar_day)
        remain_day = int(str(next_date.__sub__(today)).split(" ")[0])
        remain_tip = f"ğŸ—“ï¸� è·�ç¦»{target_name}è¿˜æœ‰ {remain_day} å¤©"
    else:
        next_date = this_date
        remain_day = int(str(next_date.__sub__(today)).split(" ")[0])
        remain_tip = f"ğŸ—“ï¸� è·�ç¦»{target_name}è¿˜æœ‰ {remain_day} å¤©"
    return (remain_tip, remain_day)


# è®¡ç®—å�•æ—¥
def get_duration(begin_day, begin_name):
    ndt = nowdatetime
    today = date(ndt.year, ndt.month, ndt.day)
    begin_day_year = begin_day.split("-")[0]
    if begin_day_year[0] == "n":
        lunar_year = int(begin_day_year[1:])
        lunar_mouth = int(begin_day.split("-")[1])
        lunar_day = int(begin_day.split("-")[2])
        begin_date = ZhDate(lunar_year, lunar_mouth,
                            lunar_day).to_datetime().date()
    else:
        solar_year = int(begin_day.split("-")[0])
        solar_month = int(begin_day.split("-")[1])
        solar_day = int(begin_day.split("-")[2])
        begin_date = date(solar_year, solar_month, solar_day)
    if today == begin_date:
        duration_day = 0
        duration_tip = f"ğŸŒŸ {begin_name}å°±æ˜¯ä»Šå¤©å•¦ï¼�"
    elif today > begin_date:
        duration_day = int(str(today.__sub__(begin_date)).split(" ")[0])
        duration_tip = f"ğŸ—“ï¸� {begin_name}å·²ç»� {duration_day} å¤©"
    else:
        duration_day = int(str(begin_date.__sub__(today)).split(" ")[0])
        duration_tip = f"ğŸ—“ï¸� è·�ç¦»{begin_name}è¿˜æœ‰ {duration_day} å¤©"
    return (duration_tip, duration_day)


# è�·å�–ç¬¬ä¸€ä¸ªå…ƒç´ 
def get_elemzero(elem):
    return elem[0]


# è�·å�–ç¬¬äºŒä¸ªå…ƒç´ 
def get_elemone(elem):
    return elem[1]


# è�·å�–æ‰€æœ‰æ—¥æœŸæ��é†’æ•°æ�®
def get_map_days():
    days_list = []
    days_tip = ""
    target_res = ""
    if targetday_list or targetname_list:
        if len(targetday_list) == len(targetname_list):
            try:
                target_res = list(
                    map(get_remain, targetday_list, targetname_list))
                days_list.extend(target_res)
            except Exception as e:
                print("è�·å�–çºªå¿µæ—¥æ•°æ�®é”™è¯¯ï¼Œè¯·æ£€æŸ¥çºªå¿µæ—¥targetnameä¸�targetdayå¡«å†™æ˜¯å�¦æ­£ç¡®", e)
                return None
        else:
            print("è�·å�–çºªå¿µæ—¥æ•°æ�®é”™è¯¯ï¼Œè¯·æ£€æŸ¥çºªå¿µæ—¥targetnameä¸�targetdayæ•°é‡�æ˜¯å�¦ç›¸ç­‰")
    else:
        print("æœªé…�ç½®çºªå¿µæ—¥")
    begin_res = ""
    if beginday_list or beginname_list:
        if len(beginday_list) == len(beginname_list):
            try:
                begin_res = list(
                    map(get_duration, beginday_list, beginname_list))
                days_list.extend(begin_res)
            except Exception as e:
                print("è�·å�–å�•æ—¥æ•°æ�®é”™è¯¯ï¼Œè¯·æ£€æŸ¥å�•æ—¥beginnameä¸�begindayå¡«å†™æ˜¯å�¦æ­£ç¡®", e)
                return None
        else:
            print("è�·å�–å�•æ—¥æ•°æ�®é”™è¯¯ï¼Œæ£€æŸ¥å�•æ—¥beginnameä¸�begindayæ•°é‡�æ˜¯å�¦ç›¸ç­‰")
    else:
        print("æœªé…�ç½®å�•æ—¥")
    days_list = list(filter(None, days_list))
    if days_list:
        days_list.sort(key=get_elemone)
        res = list(map(get_elemzero, days_list))
        days_tip = "\n".join(res)
    return days_tip


# è�·å�–ä»Šå¤©
def get_today():
    ndt = nowdatetime
    d = ndt.strftime("%Y{y}%m{m}%d{d}").format(y='å¹´', m='æœˆ', d='æ—¥')
    w = int(ndt.strftime("%w"))
    week_list = ["æ˜ŸæœŸæ—¥", "æ˜ŸæœŸä¸€", "æ˜ŸæœŸäºŒ", "æ˜ŸæœŸä¸‰", "æ˜ŸæœŸå››", "æ˜ŸæœŸäº”", "æ˜ŸæœŸå…­"]
    today_date = f"{d} {week_list[w]} "
    now_time = ndt.strftime("%H:%M:%S")
    time_tip = ""
    if "00:00:00" <= now_time < "06:00:00":
        time_tip = "å‡Œæ™¨å¥½"
    elif "06:00:00" <= now_time < "09:00:00":
        time_tip = "æ—©ä¸Šå¥½"
    elif "09:00:00" <= now_time < "12:00:00":
        time_tip = "ä¸Šå�ˆå¥½"
    elif "12:00:00" <= now_time < "13:00:00":
        time_tip = "ä¸­å�ˆå¥½"
    elif "13:00:00" <= now_time < "18:00:00":
        time_tip = "ä¸‹å�ˆå¥½"
    elif "18:00:00" <= now_time < "23:59:59":
        time_tip = "æ™šä¸Šå¥½"
    time_tip = f"{time_tip} ~ {get_emoticon()}"
    today_tip = call + time_tip if call else time_tip
    return {
        "today_date": today_date,
        "today_tip": today_tip
    }


# è�·å�–éš�æœºé¢œæ–‡å­—
def get_emoticon():
    emoticon_list = ["(ï¿£â–½ï¿£)~*", "(ï½�ï¿£â–½ï¿£)ï½�", "ï¸¿(ï¿£ï¸¶ï¿£)ï¸¿", "~(ï¿£â–½ï¿£)~*", "(oï¾Ÿâ–½ï¾Ÿ)o", "ãƒ¾(âœ¿ï¾Ÿâ–½ï¾Ÿ)ãƒ�", "Ù©(à¹‘â�›á´—â�›à¹‘)Û¶", "ãƒ¾(â—�Â°âˆ‡Â°â—�)ï¾‰ï¾�", "ãƒ¾(à¹‘â•¹â—¡â•¹)ï¾‰", "(à¹‘Â´ã…‚`à¹‘)", "(*Â´ï¾Ÿâˆ€ï¾Ÿï½€)ï¾‰", "(Â´â–½`)ï¾‰", "ãƒ¾(â—�Â´âˆ€ï½€â—�)",
                     "(ï½¡â—•Ë‡âˆ€Ë‡â—•)", "(â‰–á´—â‰–)âœ§", "(â—•á´—â—•âœ¿)", "(â��Â´â—¡`â��)*âœ²ï¾Ÿ*", "(à¹‘Â¯âˆ€Â¯à¹‘)", "(*Â´ãƒ»ï½–ãƒ»)", "(ã�¥ï½¡â—•á´—á´—â—•ï½¡)ã�¥", "o(*ï¿£â–½ï¿£*)o", "(ï½€ãƒ»Ï‰ãƒ»Â´)", "( â€¢ Ì€Ï‰â€¢Ì� )âœ§", "ãƒ¾(=ï½¥Ï‰ï½¥=)o", "(ï¿£ï¼“ï¿£)a", "(ç�¬Â°Ï‰Â°ç�¬)", "ãƒ¾(â€¢Ï‰â€¢`ã€‚)", "ï½¡â—•á´—â—•ï½¡"]
    return random.choice(emoticon_list)
