from module.base.decorator import cached_property
from module.logger import logger
from module.ocr.ocr import Digit
from module.shop.assets import *
from module.shop.base import ShopBase, ShopItemGrid
from module.shop.ui import ShopUI

OCR_SHOP_GOLD_COINS = Digit(SHOP_GOLD_COINS, letter=(239, 239, 239), name='OCR_SHOP_GOLD_COINS')
OCR_SHOP_GEMS = Digit(SHOP_GEMS, letter=(255, 243, 82), name='OCR_SHOP_GEMS')


class GeneralShop(ShopBase, ShopUI):
    _shop_gold_coins = 0
    _shop_gems = 0

    @cached_property
    def shop_filter(self):
        """
        Returns:
            str:
        """
        return self.config.GeneralShop_Filter.strip()

    @cached_property
    def shop_general_items(self):
        """
        Returns:
            ShopItemGrid:
        """
        shop_grid = self.shop_grid
        shop_general_items = ShopItemGrid(shop_grid, templates={}, amount_area=(60, 74, 96, 95))
        shop_general_items.load_template_folder('./assets/shop/general')
        shop_general_items.load_cost_template_folder('./assets/shop/cost')
        return shop_general_items

    def shop_items(self):
        """
        Shared alias for all shops
        If there are server-lang
        differences, reference
        shop_guild/medal for @Config
        example

        Returns:
            ShopItemGrid:
        """
        return self.shop_general_items

    def shop_currency(self):
        """
        Ocr shop general currency
        (gold coins and gems)

        Returns:
            int: gold coin amount
        """
        self._shop_gold_coins = OCR_SHOP_GOLD_COINS.ocr(self.device.image)
        self._shop_gems = OCR_SHOP_GEMS.ocr(self.device.image)
        logger.info(f'Gold coins: {self._shop_gold_coins}, Gems: {self._shop_gems}')
        return self._shop_gold_coins

    def shop_check_item(self, item):
        """
        Args:
            item: Item to check

        Returns:
            bool: whether item can be bought
        """
        if item.cost == 'Coins':
            if item.price > self._shop_gold_coins:
                return False
            return True

        if self.config.GeneralShop_UseGems:
            if item.cost == 'Gems':
                if item.price > self._shop_gems:
                    return False
                return True

        return False

    def shop_check_custom_item(self, item):
        """
        Optional def to check a custom item that
        cannot be template matched as color and
        design constantly changes i.e. equip skin box

        Args:
            item: Item to check

        Returns:
            bool: whether item is custom
        """
        if self.config.GeneralShop_BuySkinBox:
            if (not item.is_known_item()) and item.amount == 1 and item.cost == 'Coins' and item.price == 7000:
                logger.info(f'Item {item} is considered to be an equip skin box')
                if self._shop_gold_coins >= item.price:
                    return True

        return False

    def run(self):
        """
        Run General Shop
        """
        # Base case; exit run if filter empty
        if not self.shop_filter:
            return

        # When called, expected to be in
        # corrected General Shop interface
        logger.hr('General Shop', level=1)

        # Execute buy operations
        # Refresh if enabled and available
        refresh = self.config.GeneralShop_Refresh
        for _ in range(2):
            success = self.shop_buy()
            if not success:
                break
            if refresh and self.shop_refresh():
                continue
            break
