"""
FastTGBot - суперпростой Telegram бот
"""

import telebot
from telebot.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardRemove

_bot = None
_h = []  # обработчики

class FastTGBot:
    def __init__(self, token):
        global _bot
        self.bot = telebot.TeleBot(token)
        _bot = self
        
        @self.bot.message_handler(func=lambda m: True)
        def handler(m):
            for cond, act in _h:
                if cond == m.text or (callable(cond) and cond(m.text)):
                    if callable(act): act(m)
                    else: self.bot.reply_to(m, str(act))
    
    def run(self):
        print("✅ Бот работает!")
        self.bot.infinity_polling()

# ========== ОСНОВНЫЕ ==========

def ifm(cond, act):
    """если сообщение = cond"""
    _h.append((cond, act))

def snd(txt):
    """отправить текст"""
    def _snd(m):
        _bot.bot.reply_to(m, txt)
    return _snd

# ========== КНОПКИ ==========

def btn(txt, act):
    """одна кнопка"""
    ifm(txt, act)
    def _btn(m):
        mk = ReplyKeyboardMarkup(resize_keyboard=True)
        mk.add(KeyboardButton(txt))
        _bot.bot.send_message(m.chat.id, "👇", reply_markup=mk)
    return _btn

def btns(spisok, act):
    """несколько кнопок
    
    Пример: btns(["Кнопка1", "Кнопка2", "Кнопка3"], snd("Нажал!"))
    """
    for b in spisok:
        ifm(b, act)
    
    def _btns(m):
        mk = ReplyKeyboardMarkup(resize_keyboard=True)
        for b in spisok:
            mk.add(KeyboardButton(b))
        _bot.bot.send_message(m.chat.id, "👇", reply_markup=mk)
    return _btns

def inb(txt, data, act):
    """инлайн кнопка"""
    @_bot.bot.callback_query_handler(func=lambda c: c.data == data)
    def cb(c):
        if callable(act): act(c.message)
        else: _bot.bot.send_message(c.message.chat.id, str(act))
        _bot.bot.answer_callback_query(c.id)
    
    def _inb(m):
        mk = InlineKeyboardMarkup()
        mk.add(InlineKeyboardButton(txt, callback_data=data))
        _bot.bot.send_message(m.chat.id, "👇", reply_markup=mk)
    return _inb

def inbs(spisok, act):
    """несколько инлайн кнопок
    
    Пример: inbs([("Да", "yes"), ("Нет", "no")], snd("Ответ получен"))
    """
    for txt, data in spisok:
        @_bot.bot.callback_query_handler(func=lambda c, d=data: c.data == d)
        def cb(c, d=data):
            if callable(act): act(c.message)
            else: _bot.bot.send_message(c.message.chat.id, str(act))
            _bot.bot.answer_callback_query(c.id)
    
    def _inbs(m):
        mk = InlineKeyboardMarkup()
        for txt, data in spisok:
            mk.add(InlineKeyboardButton(txt, callback_data=data))
        _bot.bot.send_message(m.chat.id, "👇", reply_markup=mk)
    return _inbs

def clr():
    """убрать кнопки"""
    def _clr(m):
        _bot.bot.send_message(m.chat.id, "Кнопки убраны", reply_markup=ReplyKeyboardRemove())
    return _clr

# ========== МЕДИА ==========

def pic(p):
    """фото"""
    def _pic(m):
        if p.startswith('http'):
            _bot.bot.send_photo(m.chat.id, p)
        else:
            with open(p, 'rb') as f:
                _bot.bot.send_photo(m.chat.id, f)
    return _pic

def aud(a):
    """аудио"""
    def _aud(m):
        if a.startswith('http'):
            _bot.bot.send_audio(m.chat.id, a)
        else:
            with open(a, 'rb') as f:
                _bot.bot.send_audio(m.chat.id, f)
    return _aud

def sti(s):
    """стикер"""
    def _sti(m):
        if s.startswith('http'):
            _bot.bot.send_sticker(m.chat.id, s)
        else:
            with open(s, 'rb') as f:
                _bot.bot.send_sticker(m.chat.id, f)
    return _sti

# ========== ДЕЙСТВИЯ ==========

def typ():
    """печатает..."""
    def _typ(m):
        _bot.bot.send_chat_action(m.chat.id, 'typing')
    return _typ

def wait(sek, act):
    """подождать"""
    import time
    def _wait(m):
        time.sleep(sek)
        if callable(act): act(m)
        else: _bot.bot.reply_to(m, str(act))
    return _wait

def has(txt, act):
    """если текст содержит"""
    def check(m):
        return txt in m.text
    _h.append((check, act))

__version__ = "0.1.0"