import json
import re

from pot_libs.logger import log
from pot_libs.aiohttp_util.aiohttp_utils import AioHttpUtils
from pot_libs.settings import SETTING
import base64
from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.constants import S030_TOPIC, WG_TOPIC, TD_TBL_POSTFIX


async def get_td_engine_data(url, sql):
    if "?" not in url:
        url += "?tz=Asia/Shanghai"
    token = get_token()
    log.info(f"token:{token},sql:{sql}")
    resp_str, status = await AioHttpUtils().post_data(
        url, data=sql, timeout=50,
        headers={"Authorization": f"Basic {token}"}
    )
    results = json.loads(resp_str)
    if results["code"] != 0:
        return False, None
    return True, results


def get_token():
    user_password = f"{SETTING.td_user}:{SETTING.td_pwd}"
    token = base64.b64encode(user_password.encode()).decode()
    return token


async def insert_into_tidb(schema, tables, value):
    sql = (
        f'''
         INSERT INTO {schema}.{tables} VALUES({value});
        '''
    )
    async with MysqlUtil() as conn:
        datas = await conn.fetchone(sql)
    return datas if datas else {}


def test_td_engine():
    """
    td_eignne insert into TiDB
    """
    from pot_libs.settings import SETTING
    import requests
    token = get_token()
    url = f"{SETTING.stb_url}db_water?tz=Asia/Shanghai"
    print(token)
    h = {"Authorization": f"Basic {token}"}
    sql = f"select * from db_water.water_stb limit 10 "
    r = requests.post(url, data=sql, headers=h)
    print(r.status_code)
    print(r.content)
    a_list, a_dict = [], {}
    for data in json.loads(r.content)['data']:
        index = 0
        for head in json.loads(r.content)['head']:
            a_dict.update({head: data[index]})
            index += 1
        a_list.append(a_dict)
    print(a_list)


# td 3.0
def td3_tbl_compate(td_tables):
    if len(td_tables) > 1:
        return tuple(td_tables)

    return f"('{td_tables[0]}')"


def parse_td_columns(rsp_data):
    head = []
    if not rsp_data.get("column_meta"):
        return []
    for col in rsp_data["column_meta"]:
        r = re.findall(r'last_row\((.*)\)', col[0])
        tbl_field = r[0] if r else col[0]
        head.append(tbl_field)
    return head


def get_td_table_name(topic, tbl_id):
    db_name = SETTING.mysql_db
    if topic in S030_TOPIC:
        tbl_name = f"mt{tbl_id}_{TD_TBL_POSTFIX[topic]}"
        if db_name == "bromake":
            tbl_name += "_bromake"
        elif db_name == "electric_ops":
            tbl_name += "_electric_ops"
    elif topic in WG_TOPIC:
        tbl_name = f"{topic}{tbl_id}"
    else:
        return None

    return tbl_name
