import profiles.ProfileStep as ProfileStep
import locales
from locales import DEFAULT_LANG
from data import get_connection
from profiles.Profile import Profile
from bot import group_log

def get_full_profile_by_tg_id(tg_id: int):

    tg = get_row("tg", tg_id)

    po_id = int(tg["po_id"])
    language = tg["language"]
    ui_message_id = int(tg["ui_message_id"])
    step = tg["step"]
    link = tg["link"]
    rang = tg["rang"]
    streak = tg["streak"]
    sub_1 = tg["sub_1"]
    sub_2 = tg["sub_2"]
    sub_3 = tg["sub_3"]
    ref_id = tg["ref_id"]

    po = get_row("po", po_id)

    if not po:

        reg = False
        ftd = False
        sum_dep = 0

    else:
        reg = bool(po["reg"])
        ftd = bool(po["ftd"])
        sum_dep = int(po["sum_dep"])

    p = Profile(tg_id, po_id, reg, ftd, sum_dep, language, ui_message_id, step, link, rang, streak, sub_1, sub_2, sub_3, ref_id)

    return p

def get_full_profile_by_po_id(po_id: int):

    po = get_row("po", po_id)

    tg_id = int(po["tg_id"])
    reg = bool(po["reg"])
    ftd = bool(po["ftd"])
    sum_dep = int(po["sum_dep"])

    tg = get_row("tg", tg_id)

    language = tg["language"]
    ui_message_id = int(tg["ui_message_id"])
    step = tg["step"]
    link = tg["link"]
    rang = tg["rang"]
    streak = tg["streak"]
    sub_1 = tg["sub_1"]
    sub_2 = tg["sub_2"]
    sub_3 = tg["sub_3"]
    ref_id = tg["ref_id"]

    p = Profile(tg_id, po_id, reg, ftd, sum_dep, language, ui_message_id, step, link, rang, streak, sub_1, sub_2, sub_3, ref_id)

    return p

def get_row(s: str, id: int):
    conn = get_connection()
    cur = conn.cursor()

    cur.execute(f"SELECT * FROM {s} WHERE {s + '_id'} = ?", (int(id),))
    row = cur.fetchone()
    conn.close()
    return row

def update(profile: Profile):
    conn = get_connection()
    cur = conn.cursor()
    cur.execute("""
                    UPDATE tg SET
                    po_id = ?,
                    language = ?,
                    ui_message_id = ?,
                    step = ?,
                    link = ?,
                    rang = ?,
                    streak = ?,
                    sub_1 = ?,
                    sub_2 = ?,
                    sub_3 = ?,
                    ref_id = ?
                    WHERE tg_id = ?
    """,
                (profile.po_id, profile.language, profile.ui_message_id, profile.step, profile.link, profile.rang, profile.streak, profile.sub_1, profile.sub_2, profile.sub_3, profile.ref_id, profile.tg_id))
    conn.commit()
    conn.close()

def reg(tg_id: int, message_id, bot, ref_id):
    tg = get_row("tg", tg_id)

    if not tg:
        p = Profile(tg_id, 0, False, False, 0, DEFAULT_LANG, message_id, ProfileStep.START, '', 0, 0, '', '', '', ref_id)
        conn = get_connection()
        cur = conn.cursor()

        cur.execute("INSERT INTO tg (tg_id, po_id, language, ui_message_id, step, link, rang, streak, sub_1, sub_2, sub_3, ref_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                    (p.tg_id, p.po_id, p.language, p.ui_message_id, p.step, '', 0, 0, '', '', '', ref_id))

        conn.commit()
        conn.close()
        bot.send_message(group_log, str(p.tg_id)+' зарегался')
    else:
        p = get_full_profile_by_tg_id(tg_id)
        try:
            bot.delete_message(
                chat_id = p.tg_id,
                message_id = p.ui_message_id
            )
        except Exception as e:
            print(e)
            pass
        try:
            bot.delete_message(
                chat_id = p.tg_id,
                message_id = p.ui_message_id - 1
            )
        except:
            pass
    p.ui_message_id = message_id
    update(p)

    return p

def link_po(profile: Profile):
    conn = get_connection()
    cur = conn.cursor()

    cur.execute(f"SELECT * FROM po WHERE tg_id = ?", (int(profile.tg_id),))
    row = cur.fetchone()
    conn.close()

    if row:

        profile.po_id = int(row["po_id"])
        profile.reg = bool(row["reg"])
        profile.ftd = bool(row["ftd"])
        profile.sum_dep = int(row["sum_dep"])

    update(profile)
    return profile


