-- ============================================================
-- Komo Hold'Em — Cards
-- Deck data + the visual spec from the locked design:
--   faces   : theme texture (Midnight / Alliance / Horde)
--   stroke  : 1px blue->purple->pink on number cards,
--             gold gradient on A K Q J (approximated in-game
--             with pre-rendered border textures)
--   corners : rank silver-white; suit gold on royals, suit color
--             otherwise
--   center  : big suit symbol in suit color (2-10, A),
--             big letter in gold (K Q J)
--   suits   : hearts/diamonds red, clubs white, spades silver
--   back    : CardBack.tga (K over purple tech + royal flush fan)
-- ============================================================
local _, KH = ...
KH.Cards = {}
local Cards = KH.Cards

Cards.SUITS = { "S", "H", "D", "C" } -- spades hearts diamonds clubs
Cards.RANKS = { "2","3","4","5","6","7","8","9","10","J","Q","K","A" }
Cards.RANK_VALUE = {
    ["2"]=2, ["3"]=3, ["4"]=4, ["5"]=5, ["6"]=6, ["7"]=7, ["8"]=8,
    ["9"]=9, ["10"]=10, ["J"]=11, ["Q"]=12, ["K"]=13, ["A"]=14,
}

Cards.SUIT_GLYPH = { S = "♠", H = "♥", D = "♦", C = "♣" }
Cards.ROYAL = { A = true, K = true, Q = true, J = true }

-- glyph colors (r,g,b) per the locked deck
Cards.SUIT_COLOR = {
    H = { 0.937, 0.447, 0.408 },  -- red    #EF7268
    D = { 0.937, 0.447, 0.408 },  -- red
    C = { 1.000, 1.000, 1.000 },  -- white
    S = { 0.941, 0.957, 0.973 },  -- silver #F0F4F8
}
Cards.RANK_COLOR  = { 1, 1, 1 }                     -- corner ranks: silver-white
Cards.GOLD_COLOR  = { 0.965, 0.867, 0.522 }         -- royal corners/centers #F6DD85

-- textures (exported at packaging time; power-of-two 256x512,
-- card art region 240x342 inside)
Cards.TEX = {
    FACE = {
        midnight = "Interface\\AddOns\\KomoHoldEm\\Media\\FaceMidnight",
        alliance = "Interface\\AddOns\\KomoHoldEm\\Media\\FaceAlliance",
        horde    = "Interface\\AddOns\\KomoHoldEm\\Media\\FaceHorde",
    },
    BACK          = "Interface\\AddOns\\KomoHoldEm\\Media\\CardBack",
    STROKE_NUMBER = "Interface\\AddOns\\KomoHoldEm\\Media\\StrokeBrand",  -- blue->purple->pink
    STROKE_ROYAL  = "Interface\\AddOns\\KomoHoldEm\\Media\\StrokeGold",
}

-- card id = rank .. suit, e.g. "AS", "10H"
function Cards.Id(rank, suit) return rank .. suit end
function Cards.Split(id)
    local rank, suit = id:match("^(.-)([SHDC])$")
    return rank, suit
end

-- ------------------------------------------------------------
-- Deck (host-side only; clients never hold the deck)
-- ------------------------------------------------------------
function Cards:NewDeck()
    local deck = {}
    for _, s in ipairs(self.SUITS) do
        for _, r in ipairs(self.RANKS) do
            deck[#deck + 1] = self.Id(r, s)
        end
    end
    return deck
end

-- Fisher–Yates
function Cards:Shuffle(deck)
    for i = #deck, 2, -1 do
        local j = math.random(i)
        deck[i], deck[j] = deck[j], deck[i]
    end
    return deck
end

function Cards:Draw(deck)
    return table.remove(deck)
end

-- ------------------------------------------------------------
-- Center glyph rule from the design:
--   A         -> big suit symbol, gold
--   K / Q / J -> big letter, gold
--   2-10      -> big suit symbol, suit color
-- Returns glyphText, r, g, b
-- ------------------------------------------------------------
function Cards:CenterGlyph(id)
    local rank, suit = self.Split(id)
    if rank == "K" or rank == "Q" or rank == "J" then
        return rank, unpack(self.GOLD_COLOR)
    elseif rank == "A" then
        return self.SUIT_GLYPH[suit], unpack(self.GOLD_COLOR)
    end
    return self.SUIT_GLYPH[suit], unpack(self.SUIT_COLOR[suit])
end

function Cards:CornerColors(id)
    local rank, suit = self.Split(id)
    local rankColor = self.RANK_COLOR
    local suitColor = self.ROYAL[rank] and self.GOLD_COLOR or self.SUIT_COLOR[suit]
    return rankColor, suitColor
end
