#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (C) Shrimadhav U K

import os
if bool(os.environ.get("WEBHOOK", False)):
    from sample_config import Config
else:
    from config import Config
from pyrogram import Client, filters
from translation import Translation
import database.database as sql
from database.database import *

@Client.on_message(filters.private & filters.photo)
async def save_photo(bot, update):
    if update.media_group_id is not None:
        # album is sent
        download_location = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + "/" + str(update.media_group_id) + "/"
        # create download directory, if not exist
        if not os.path.isdir(download_location):
            os.makedirs(download_location)
        await sql.df_thumb(update.from_user.id, update.message_id)
        await bot.download_media(
            message=update,
            file_name=download_location
        )
    else:
        # received single photo
        download_location = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg"
        await sql.df_thumb(update.from_user.id, update.message_id)
        await bot.download_media(
            message=update,
            file_name=download_location
        )
        await bot.send_message(
            chat_id=update.chat.id,
            text="**Thumbnail saved successfully**",
            reply_to_message_id=update.message_id
        )

@Client.on_message(filters.private & filters.command(["delthumb"]))
async def delete_thumbnail(bot, update):
    thumb_image_path = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg"
    #download_location = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id)
    try:
        await sql.del_thumb(update.from_user.id)
        os.remove(thumb_image_path)
        #os.remove(download_location + ".json")
    except:
        pass
    await bot.send_message(
        chat_id=update.chat.id,
        text ="**✅ Custom Thumbnail cleared succesfully**",
        reply_to_message_id=update.message_id
    )

@Client.on_message(filters.private & filters.command(["showthumb"]))
async def show_thumb(bot, update):
    thumb_image_path = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg"
    if not os.path.exists(thumb_image_path):
        mes = await thumb(update.from_user.id)
        if mes != None:
            m = await bot.get_messages(update.chat.id, mes.msg_id)
            await m.download(file_name=thumb_image_path)
            thumb_image_path = thumb_image_path
        else:
            thumb_image_path = None
    if thumb_image_path is not None:
        try:
            await bot.send_photo(
                chat_id=update.chat.id,
                photo=thumb_image_path
            )
        except:
            pass
    elif thumb_image_path is None:
        await bot.send_message(
            chat_id=update.chat.id,
            text="no thumbnail found",
            reply_to_message_id=update.message_id
        )
