-- ==================================================
-- Class CreditCardData
-- ==================================================
CreditCardData = {}
CreditCardData.__index = CreditCardData

-- Constructor
function CreditCardData:new(item)
    local o = {}
    setmetatable(o, self)
    o.item = item

    local key = item:getID() or tostring(item:getName())
    local modData = item:getModData()
    modData.CreditCardData = modData.CreditCardData or {}

    local data = modData.CreditCardData[key]

    if data then
        -- LOAD EXISTING DATA
        o.firstname         = data.firstname
        o.lastname          = data.lastname
        o.cardNumber        = data.cardNumber
        o.expireDate        = data.expireDate
        o.signatureTexture  = data.signatureTexture
        o.logoTexture       = data.logoTexture
        o.design            = data.design
        o.blooded           = data.blooded or false
        o.bloodTexture      = data.bloodTexture
    else
        -- GENERATE NEW DATA
        o.firstname         = CreditCardData:getNames(item)
        o.lastname          = select(2, CreditCardData:getNames(item))
        o.cardNumber        = CreditCardData:getCardNumber()
        o.expireDate        = CreditCardData:getExpireDate()
        o.signatureTexture  = CreditCardData:getSignatureTexture(o.lastname)
        o.logoTexture       = CreditCardData:getLogoTexture()
        o.design            = CreditCardData:getRandomDesign()
        o.blooded           = CreditCardData:getRandomBlooded()
        o.bloodTexture      = o.blooded and CreditCardData:getRandomBloodTexture() or nil

        modData.CreditCardData[key] = {
            firstname        = o.firstname,
            lastname         = o.lastname,
            cardNumber       = o.cardNumber,
            expireDate       = o.expireDate,
            signatureTexture = o.signatureTexture,
            logoTexture      = o.logoTexture,
            design           = o.design,
            blooded          = o.blooded,
            bloodTexture     = o.bloodTexture,
        }

        print("[CreditCardData] New card design assigned:", o.design)
    end

    return o
end


function CreditCardData:getRandomDesign()
    local list = CreditCardLayouts.List
    local index = ZombRand(#list) + 1
    return list[index]
end

function CreditCardData:getNames(item)
    local itemname = item:getName()
    local firstname, lastname = string.match(itemname, ": (%w+)%s+(%w+)")
    return firstname or "John", lastname or "Doe"
end

function CreditCardData:getCardNumber()

    -- format : XXX-XXXX (3 chiffres - 4 chiffres)
    local part1 = string.format("%03d", ZombRand(0, 9999))
    local part2 = string.format("%04d", ZombRand(0, 999))
    local part3 = string.format("%04d", ZombRand(0, 999))
    local part4 = string.format("%04d", ZombRand(0, 999))

    return part1 .. " " .. part2 .. " " .. part3 .. " " .. part4
end

--___________________________________SIGNATURE_______________________________________

function CreditCardData:getSignatureTexture(lastname)
    local basePath = "media/textures/signatures/"
    local lname = string.lower(lastname)

    -- Take the first surname letter
    local firstLetter = string.sub(lname, 1, 1)

    -- Building the path: A/Anderson.png, B/Brown.png etc.
    local fileName = lname .. ".png"
    local fullPath = basePath .. firstLetter .. "/" .. fileName

    -- Check if texture exists)
    if getTexture(fullPath) then
        return fullPath
    else
        return basePath .. "default.png"
    end
end

--------------------------------------------------
-- Logo
--------------------------------------------------
function CreditCardData:getLogoTexture()
    local list = {
        "media/textures/ui/creditcard_logo/creditcard_logo-1.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-2.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-3.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-4.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-5.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-6.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-7.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-8.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-9.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-10.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-11.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-12.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-13.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-14.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-15.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-16.png",
        "media/textures/ui/creditcard_logo/creditcard_logo-17.png",

    }
    return list[ZombRand(#list) + 1]
end

local function randomAppliedDate()
    local months = {"01","02","03","04","05","06","07","08","09","10","11","12"}
    local month = months[ZombRand(#months) + 1]
    local year = ZombRand(93, 97) -- 1990 à 1994 inclus
    return month, year
end

function CreditCardData:getExpireDate()
    local month, year = randomAppliedDate()
    self._appliedMonth = month
    self._appliedYear = year
    return month .. "-" .. string.format("%02d", year % 100)
end

--------------------------------------------------
-- Blood
--------------------------------------------------
function CreditCardData:getRandomBlooded()
    -- 25% chance to be bloody
    return ZombRand(100) < 25
end

function CreditCardData:getRandomBloodTexture()
    local list = {
        "media/textures/blood/card_blood_1.png",
        "media/textures/blood/card_blood_2.png",
        "media/textures/blood/card_blood_3.png",
    }
    return list[ZombRand(#list) + 1]
end

-- ==================================================
-- Fonction générale : retourne toutes les infos
-- ==================================================
function CreditCardData:getAll()
    return {
        firstname     = self.firstname,
        lastname      = self.lastname,
        cardNumber    = self.cardNumber,
        expireDate    = self.expireDate,
    }
end
