"""
سیستم تم و رنگ‌بندی دکمه‌ها
رنگ دکمه‌های InlineKeyboard با ایموجی رنگی کنترل میشه
"""
import database as db
from telegram import InlineKeyboardMarkup, InlineKeyboardButton

# رنگ‌بندی با ایموجی - هر رنگ یه ست ایموجی داره
COLOR_SETS = {
    'green':   ['🟢', '💚', '✅', '🌿'],
    'blue':    ['🔵', '💙', '🌀', '🔷'],
    'red':     ['🔴', '❤️', '🚨', '🔶'],
    'purple':  ['🟣', '💜', '🔮', '✨'],
    'gold':    ['🟡', '⭐', '🌟', '💛'],
    'default': [],
}

def get_color_indicator(color, index=0):
    """یه ایموجی رنگی برمیگردونه"""
    s = COLOR_SETS.get(color, [])
    if not s:
        return ''
    return s[index % len(s)] + ' '

def build_main_menu(user_id=None, is_admin=False, is_vip=False):
    """
    منوی اصلی با InlineKeyboard - مثل ربات‌های حرفه‌ای
    رنگ و ایموجی از دیتابیس خونده میشه
    """
    from config import ADMIN_IDS
    theme = db.get_theme()
    mc = theme.get('color_main', 'green')
    sc = theme.get('color_secondary', 'default')

    # ایموجی رنگی برای دکمه‌های اصلی (سبز/رنگ اصلی)
    ci = get_color_indicator(mc, 0)
    # ایموجی برای دکمه‌های ثانویه (خاکستری/پیش‌فرض)
    si = get_color_indicator(sc, 0)

    def main_btn(text_key, cb):
        return InlineKeyboardButton(ci + theme.get(text_key, text_key), callback_data=cb)
    def sec_btn(text_key, cb):
        return InlineKeyboardButton(si + theme.get(text_key, text_key) if si else theme.get(text_key, text_key), callback_data=cb)

    buttons = [
        [main_btn('btn_buy', 'menu_buy'), main_btn('btn_orders', 'menu_orders')],
        [main_btn('btn_configs', 'menu_configs'), sec_btn('btn_wallet', 'menu_wallet')],
        [sec_btn('btn_support', 'menu_support'), sec_btn('btn_referral', 'menu_referral')],
        [sec_btn('btn_reviews', 'menu_reviews'), sec_btn('btn_payment', 'menu_payment')],
        [sec_btn('btn_about', 'menu_about')],
    ]

    if is_admin:
        buttons.append([InlineKeyboardButton('⚙️ ' + theme.get('btn_admin','پنل ادمین'), callback_data='menu_admin')])

    return InlineKeyboardMarkup(buttons)

def build_welcome_text(user_name, is_vip=False, lang='fa'):
    """متن خوش‌آمدگویی با استایل حرفه‌ای"""
    theme = db.get_theme()
    bot_name = theme.get('bot_name', 'ربات پرمیوم')
    version = theme.get('bot_version', '1.0.0')
    welcome_emoji = theme.get('welcome_emoji', '👋')
    mc = theme.get('color_main', 'green')
    color_dot = get_color_indicator(mc, 0).strip()
    vip_badge = ' 👑' if is_vip else ''

    text = (
        f"{welcome_emoji} به *{bot_name}* خوش آمدید!\n\n"
        f"🚀 نسخه {bot_name} — {version}\n\n"
        f"👤 کاربر: {user_name}{vip_badge}\n\n"
        f"یک گزینه را انتخاب کنید:"
    )
    return text

def get_theme_preview():
    """پیش‌نمایش تم فعلی برای ادمین"""
    theme = db.get_theme()
    mc = theme.get('color_main', 'green')
    sc = theme.get('color_secondary', 'default')
    ci = get_color_indicator(mc, 0).strip()
    si = get_color_indicator(sc, 0).strip() if sc != 'default' else '⬜'

    return (
        f"🎨 تم فعلی:\n\n"
        f"🤖 نام ربات: {theme.get('bot_name')}\n"
        f"📌 نسخه: {theme.get('bot_version')}\n"
        f"👋 ایموجی خوش‌آمد: {theme.get('welcome_emoji')}\n\n"
        f"🔵 رنگ اصلی: {ci} {mc}\n"
        f"⚪ رنگ ثانویه: {si} {sc}\n\n"
        f"دکمه‌ها:\n"
        f"{ci} {theme.get('btn_buy')}\n"
        f"{ci} {theme.get('btn_orders')}\n"
        f"{ci} {theme.get('btn_configs')}\n"
        f"{'⬜' if sc=='default' else si} {theme.get('btn_wallet')}\n"
        f"{'⬜' if sc=='default' else si} {theme.get('btn_support')}\n"
    )
