from django.contrib.humanize.templatetags.humanize import intcomma

from allianceauth.services.hooks import get_extension_logger

from buybackprogram.app_settings import (
    BUYBACKPROGRAM_PRICE_AGE_WARNING_LIMIT,
    BUYBACKPROGRAM_PRICE_SOURCE_NAME,
)

logger = get_extension_logger(__name__)

# ---------------------------------------------------------------------------
# Colour palette aligned with our CSS theme
# ---------------------------------------------------------------------------
_RED = "#dc3545"  # danger
_ORANGE = "#fd7e14"  # warning
_GREEN = "#20c997"  # success (teal)
_BLUE = "#0d6efd"  # primary / info
_PURPLE = "#6f42c1"  # watchlist
_MUTED = "#6c757d"  # secondary / best-price notes


def note_missing_jita_buy(buy_price, name):
    if buy_price == 0:
        return {
            "icon": "fa-question-circle",
            "color": _RED,
            "message": "%s has no buy orders in %s"
            % (name, BUYBACKPROGRAM_PRICE_SOURCE_NAME),
        }
    return False


def note_price_dencity_tax(name, price_dencity, price_dencity_tax):
    if price_dencity_tax:
        return {
            "icon": "fa-weight-hanging",
            "color": _ORANGE,
            "message": "%s has price density of %s ISK/m³ — %s%% low price density tax applied"
            % (name, round(price_dencity, 2), price_dencity_tax),
        }
    return False


def note_item_disallowed(item_disallowed, name):
    if item_disallowed:
        return {
            "icon": "fa-ban",
            "color": _RED,
            "message": "%s is disallowed in this program" % name,
        }
    return False


def note_unpublished_item(item_type):
    if not item_type.published:
        return {
            "icon": "fa-ban",
            "color": _RED,
            "message": "%s is not a published item. It may be expired or an invalid paste."
            % item_type.name,
        }
    if not item_type.eve_market_group:
        return {
            "icon": "fa-ban",
            "color": _RED,
            "message": "%s has no market information. Likely a mission item or special commodity."
            % item_type.name,
        }
    return {
        "icon": "fa-ban",
        "color": _RED,
        "message": "%s could not be valued." % item_type.name,
    }


def note_missing_typematerials(type_materials, name):
    if type_materials.count() == 0:
        logger.error(
            "TypeMaterials not found for %s. Did you forget to run buybackprogram_load_data?"
            % name
        )
        return {
            "icon": "fa-exclamation-triangle",
            "color": _ORANGE,
            "message": "Refined price valuation is active but TypeMaterials for %s are missing. Run buybackprogram_load_data."
            % name,
        }
    return False


def note_item_specific_tax(name, item_tax):
    if item_tax:
        if item_tax > 0:
            return {
                "icon": "fa-percentage",
                "color": _ORANGE,
                "message": "%s has an additional %s%% item-specific tax applied"
                % (name, item_tax),
            }
        else:
            return {
                "icon": "fa-percentage",
                "color": _GREEN,
                "message": "%s has a %s%% item-specific discount applied"
                % (name, item_tax),
            }
    return False


def note_no_price_data(name):
    return {
        "icon": "fa-question-circle",
        "color": _RED,
        "message": "%s has no price data" % name,
    }


def note_refined_price_used(name):
    return {
        "icon": "fa-industry",
        "color": _BLUE,
        "message": "Best price: using refined value for %s" % name,
    }


def note_compressed_price_used(name):
    return {
        "icon": "fa-compress-arrows-alt",
        "color": _BLUE,
        "message": "Best price: using compressed value for %s" % name,
    }


def note_npc_price(name):
    return {
        "icon": "fa-robot",
        "color": _MUTED,
        "message": "Using NPC buy price for %s instead of %s prices"
        % (name, BUYBACKPROGRAM_PRICE_SOURCE_NAME),
    }


def note_raw_price_used(name):
    return {
        "icon": "fa-icicles",
        "color": _BLUE,
        "message": "Best price: using raw ore value for %s" % name,
    }


def note_missing_npc_price(name):
    return {
        "icon": "fa-question-circle",
        "color": _RED,
        "message": "%s is missing price data. Run `buybackprogram_load_prices` to fix."
        % name,
    }


def note_price_outdated(updated, name):
    return {
        "icon": "fa-clock",
        "color": _ORANGE,
        "message": "Price data for %s is more than %s hours old"
        % (name, BUYBACKPROGRAM_PRICE_AGE_WARNING_LIMIT),
    }


def note_missing_from_contract(name):
    return {
        "icon": "fa-unlink",
        "color": _RED,
        "message": "%s is missing from the created contract" % name,
    }


def note_missing_from_tracking(name):
    return {
        "icon": "fa-unlink",
        "color": _ORANGE,
        "message": "%s is missing from the original calculation" % name,
    }


def note_quantity_missing_from_contract(name):
    return {
        "icon": "fa-calculator",
        "color": _RED,
        "message": "Quantity for %s in the contract does not match the calculation"
        % name,
    }


def note_quantity_missing_from_tracking(name):
    return {
        "icon": "fa-calculator",
        "color": _ORANGE,
        "message": "Quantity for %s in the calculation does not match the tracking"
        % name,
    }


def note_static_price_used(item_name, price):
    return {
        "icon": "fa-tag",
        "color": _GREEN,
        "header": "Static Price",
        "message": "Using static price of %s ISK for %s" % (intcomma(price), item_name),
    }


def note_watchlisted_item(name):
    return {
        "icon": "fa-search",
        "color": _PURPLE,
        "message": "%s is on the watchlist and requires manual review" % name,
    }
