-- Create a frame for the initial screen
local initialFrame = CreateFrame("Frame", "BlackjackInitialFrame", UIParent, "BasicFrameTemplateWithInset")
initialFrame:SetSize(400, 200)
initialFrame:SetPoint("CENTER")
initialFrame:Hide() -- Initially hidden

-- Add a title to the initial frame
initialFrame.title = initialFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
initialFrame.title:SetPoint("TOP", initialFrame, 0, -6)
initialFrame.title:SetText("Welcome to Blackjack!")

-- Create a "Join Table" button to start the game
initialFrame.joinButton = CreateFrame("Button", nil, initialFrame, "GameMenuButtonTemplate")
initialFrame.joinButton:SetPoint("CENTER", initialFrame, 0, 0)
initialFrame.joinButton:SetSize(120, 30)
initialFrame.joinButton:SetText("Join Table")
initialFrame.joinButton:Disable()

-- Create a font string to display the join instructions
initialFrame.join1Text = initialFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
initialFrame.join1Text:SetPoint("TOP", initialFrame, 0, -45)
initialFrame.join1Text:SetText("You will be able to play") -- Get the character's name
-- Set the font size and text
initialFrame.join1Text:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE") -- Adjust the font and size as needed

-- Create a font string to display the join instructions
initialFrame.join2Text = initialFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
initialFrame.join2Text:SetPoint("TOP", initialFrame, 0, -60)
initialFrame.join2Text:SetText("once the dealer has opened up a table.") -- Get the character's name
-- Set the font size and text
initialFrame.join2Text:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE") -- Adjust the font and size as needed

-- Create a font string to display the developer
initialFrame.join3Text = initialFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
initialFrame.join3Text:SetPoint("BOTTOM", initialFrame, 0, 32)
initialFrame.join3Text:SetText("v1.0 - Developed by") -- Get the character's name
-- Set the font size and text
initialFrame.join3Text:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE") -- Adjust the font and size as needed
initialFrame.join3Text:SetTextColor(1, 0.5, 0) -- RGB values for blue (0, 0, 1)

-- Create a font string to display the developer
initialFrame.join4Text = initialFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
initialFrame.join4Text:SetPoint("BOTTOM", initialFrame, 0, 20)
initialFrame.join4Text:SetText("Blackjackk-Earthshaker") -- Get the character's name
-- Set the font size and text
initialFrame.join4Text:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE") -- Adjust the font and size as needed
initialFrame.join4Text:SetTextColor(1, 0.5, 0) -- RGB values for blue (0, 0, 1)

-- Enable click-and-drag functionality for the dealerFrame
initialFrame:SetMovable(true)
initialFrame:EnableMouse(true)
initialFrame:RegisterForDrag("LeftButton")

-- Set the frame's script for handling the drag
initialFrame:SetScript("OnDragStart", function(self)
    self:StartMoving()
end)

initialFrame:SetScript("OnDragStop", function(self)
    self:StopMovingOrSizing()
end)


-- Create a frame for the player addon
local playerFrame = CreateFrame("Frame", "BlackjackPlayerFrame", UIParent, "BasicFrameTemplateWithInset")
playerFrame:SetSize(1280, 720)
playerFrame:SetPoint("CENTER")
playerFrame:Hide() -- Initially hidden

-- Add a title to the frame
playerFrame.title = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.title:SetPoint("TOP", playerFrame, 0, -6)
playerFrame.title:SetText("Blackjack Player")

-- Create a texture for the background image
local backgroundImage = playerFrame:CreateTexture(nil, "BACKGROUND")
backgroundImage:SetSize(2048, 1024) -- Set the size as needed (width x height)
backgroundImage:SetPoint("TOP", playerFrame, "TOP", 0, 168) -- Anchor to the bottom of dealerFrame
backgroundImage:SetTexture("Interface\\AddOns\\BJPlayer\\Images\\blackjackplayer.tga") -- Adjust the path to your image
backgroundImage:SetBlendMode("BLEND") -- Optional: Adjust blend mode if needed
backgroundImage:SetDrawLayer("ARTWORK") -- Set the draw layer to "ARTWORK"

-- Enable click-and-drag functionality for the dealerFrame
playerFrame:SetMovable(true)
playerFrame:EnableMouse(true)
playerFrame:RegisterForDrag("LeftButton")

-- Set the frame's script for handling the drag
playerFrame:SetScript("OnDragStart", function(self)
    self:StartMoving()
end)

playerFrame:SetScript("OnDragStop", function(self)
    self:StopMovingOrSizing()
end)

-- Create a font string to display the join instructions
playerFrame.RulesText1 = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.RulesText1:SetPoint("BOTTOMLEFT", playerFrame, 20, 55)
playerFrame.RulesText1:SetJustifyH("LEFT")
playerFrame.RulesText1:SetText('1 = Ace (1 or 11)\n2 = 2\n3 = 3\n4 = 4\n5 = 5\n6 = 6\n7 = 7\n8 = 8\n9 = 9\n10 = 10\n11 = Jack (10)\n12 = Queen (10)\n13 = King (10)\n\nThe dealer will "/roll 1-13" for each card') -- Get the character's name
-- Set the font size and text
playerFrame.RulesText1:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE") -- Adjust the font and size as needed

-- Create a font string to display the join instructions
playerFrame.DisclaimerText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.DisclaimerText:SetPoint("BOTTOMRIGHT", playerFrame, -20, 20)
playerFrame.DisclaimerText:SetJustifyH("RIGHT")
playerFrame.DisclaimerText:SetText("Please play responsibly. Only stake what you can\nafford to lose. When the fun stops - stop immediately.") -- Get the character's name
playerFrame.DisclaimerText:SetTextColor(1 , 0, 0)
-- Set the font size and text
playerFrame.DisclaimerText:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE") -- Adjust the font and size as needed

-- Create a font string to display under development text
playerFrame.SplitText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.SplitText:SetPoint("BOTTOM", playerFrame, 138, 60)
playerFrame.SplitText:SetJustifyH("LEFT")
playerFrame.SplitText:SetText("<-- under development")
-- Set the font size and text
playerFrame.SplitText:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE") -- Adjust the font and size as needed

-- Create a font string to display under development text
playerFrame.RulesTextDevelopment = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.RulesTextDevelopment:SetPoint("BOTTOMRIGHT", playerFrame, -122, 91)
playerFrame.RulesTextDevelopment:SetJustifyH("LEFT")
playerFrame.RulesTextDevelopment:SetText("under development -->")
-- Set the font size and text
playerFrame.RulesTextDevelopment:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE") -- Adjust the font and size as needed

-- Create a font string to display under development text
playerFrame.InsuranceText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.InsuranceText:SetPoint("BOTTOM", playerFrame, 122, 31)
playerFrame.InsuranceText:SetJustifyH("LEFT")
playerFrame.InsuranceText:SetText("<-- under development")
-- Set the font size and text
playerFrame.InsuranceText:SetFont("Fonts\\FRIZQT__.TTF", 10, "OUTLINE") -- Adjust the font and size as needed

-- Add a hit button to start dealing cards
playerFrame.hitButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.hitButton:SetPoint("BOTTOM", playerFrame, -33, 80)
playerFrame.hitButton:SetSize(65, 30)
playerFrame.hitButton:SetText("Hit")
playerFrame.hitButton:Disable()

-- Add a stand button to start dealing cards
playerFrame.standButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.standButton:SetPoint("BOTTOM", playerFrame, 33, 80)
playerFrame.standButton:SetSize(65, 30)
playerFrame.standButton:SetText("Stand")
playerFrame.standButton:Disable()

-- Add a double button to start dealing cards
playerFrame.doubleButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.doubleButton:SetPoint("BOTTOM", playerFrame, -33, 50)
playerFrame.doubleButton:SetSize(65, 30)
playerFrame.doubleButton:SetText("Double")
playerFrame.doubleButton:Disable()

-- Add a split button to start dealing cards
playerFrame.splitButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.splitButton:SetPoint("BOTTOM", playerFrame, 33, 50)
playerFrame.splitButton:SetSize(65, 30)
playerFrame.splitButton:SetText("Split")
playerFrame.splitButton:Disable()

-- Add an insuranace button to start dealing cards
playerFrame.insuranaceButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.insuranaceButton:SetPoint("BOTTOM", playerFrame, 0, 20)
playerFrame.insuranaceButton:SetSize(100, 30)
playerFrame.insuranaceButton:SetText("Insurance")
playerFrame.insuranaceButton:Disable()

-- Add a leave table button
playerFrame.leaveTableButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.leaveTableButton:SetPoint("BOTTOMRIGHT", playerFrame, -20, 50)
playerFrame.leaveTableButton:SetSize(100, 30)
playerFrame.leaveTableButton:SetText("Leave Table")

-- Add a rules button
playerFrame.rulesButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
playerFrame.rulesButton:SetPoint("BOTTOMRIGHT", playerFrame, -20, 80)
playerFrame.rulesButton:SetSize(100, 30)
playerFrame.rulesButton:SetText("Rules")
playerFrame.rulesButton:Disable()

-- Create a font string to display the character's name
playerFrame.name1Text = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.name1Text:SetPoint("BOTTOM", playerFrame, 0, 116)
playerFrame.name1Text:SetText(UnitName("player")) -- Get the character's name
-- Set the font size and text
playerFrame.name1Text:SetFont("Fonts\\FRIZQT__.TTF", 22, "OUTLINE") -- Adjust the font and size as needed

-- Add a font string for displaying the timer
playerFrame.timerText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
playerFrame.timerText:SetPoint("TOPRIGHT", playerFrame, "TOPRIGHT", -230, -150)
playerFrame.timerText:SetFont("Fonts\\FRIZQT__.TTF", 18, "OUTLINE") -- Adjust the font and size as needed

-- Create an edit box for entering the bet amount
local betEditBox = CreateFrame("EditBox", nil, playerFrame, "InputBoxTemplate")
betEditBox:SetSize(57, 30)
betEditBox:SetPoint("BOTTOM", playerFrame, -33, 140)
betEditBox:SetAutoFocus(false)
betEditBox:SetNumeric(true) -- Ensure the input is numeric

local tableLimitText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
tableLimitText:SetPoint("BOTTOMLEFT", playerFrame, 20, 20) -- Adjust the positioning as needed
tableLimitText:SetFont("Fonts\\FRIZQT__.TTF", 14, "OUTLINE") -- Adjust the font and size as needed
tableLimitText:SetText("Table Limits: 100 G - 1500 G") -- Set the text


local soundFilePath1 = "Interface\\AddOns\\BJPlayer\\Sounds\\1.mp3"
local soundFilePath2 = "Interface\\AddOns\\BJPlayer\\Sounds\\2.mp3"
local soundFilePath3 = "Interface\\AddOns\\BJPlayer\\Sounds\\3.mp3"
local soundFilePath4 = "Interface\\AddOns\\BJPlayer\\Sounds\\4.mp3"
local soundFilePath5 = "Interface\\AddOns\\BJPlayer\\Sounds\\5.mp3"
local soundFilePath6 = "Interface\\AddOns\\BJPlayer\\Sounds\\6.mp3"
local soundFilePath7 = "Interface\\AddOns\\BJPlayer\\Sounds\\7.mp3"
local soundFilePath8 = "Interface\\AddOns\\BJPlayer\\Sounds\\8.mp3"
local soundFilePath9 = "Interface\\AddOns\\BJPlayer\\Sounds\\9.mp3"
local soundFilePath10 = "Interface\\AddOns\\BJPlayer\\Sounds\\10.mp3"
local soundFilePath11 = "Interface\\AddOns\\BJPlayer\\Sounds\\11.mp3"
local soundFilePath12 = "Interface\\AddOns\\BJPlayer\\Sounds\\12.mp3"
local soundFilePath13 = "Interface\\AddOns\\BJPlayer\\Sounds\\13.mp3"
local soundFilePath14 = "Interface\\AddOns\\BJPlayer\\Sounds\\14.mp3"
local soundFilePath15 = "Interface\\AddOns\\BJPlayer\\Sounds\\15.mp3"
local soundFilePath16 = "Interface\\AddOns\\BJPlayer\\Sounds\\16.mp3"
local soundFilePath17 = "Interface\\AddOns\\BJPlayer\\Sounds\\17.mp3"
local soundFilePath18 = "Interface\\AddOns\\BJPlayer\\Sounds\\18.mp3"
local soundFilePath19 = "Interface\\AddOns\\BJPlayer\\Sounds\\19.mp3"
local soundFilePath20 = "Interface\\AddOns\\BJPlayer\\Sounds\\20.mp3"
local soundFilePath21 = "Interface\\AddOns\\BJPlayer\\Sounds\\21.mp3"

local soundFilePath1or11 = "Interface\\AddOns\\BJPlayer\\Sounds\\1 or 11.mp3"
local soundFilePath2or12 = "Interface\\AddOns\\BJPlayer\\Sounds\\2 or 12.mp3"
local soundFilePath3or13 = "Interface\\AddOns\\BJPlayer\\Sounds\\3 or 13.mp3"
local soundFilePath4or14 = "Interface\\AddOns\\BJPlayer\\Sounds\\4 or 14.mp3"
local soundFilePath5or15 = "Interface\\AddOns\\BJPlayer\\Sounds\\5 or 15.mp3"
local soundFilePath6or16 = "Interface\\AddOns\\BJPlayer\\Sounds\\6 or 16.mp3"
local soundFilePath7or17 = "Interface\\AddOns\\BJPlayer\\Sounds\\7 or 17.mp3"
local soundFilePath8or18 = "Interface\\AddOns\\BJPlayer\\Sounds\\8 or 18.mp3"
local soundFilePath9or19 = "Interface\\AddOns\\BJPlayer\\Sounds\\9 or 19.mp3"
local soundFilePath10or20 = "Interface\\AddOns\\BJPlayer\\Sounds\\10 or 20.mp3"

local soundFilePathPlayerBlackjack = "Interface\\AddOns\\BJPlayer\\Sounds\\PlayerBlackjack.mp3"
local soundFilePathPlayerWin = "Interface\\AddOns\\BJPlayer\\Sounds\\PlayerWin.mp3"
local soundFilePathPlayerLoss = "Interface\\AddOns\\BJPlayer\\Sounds\\PlayerLoss.mp3"
local soundFilePathPlayerBust = "Interface\\AddOns\\BJPlayer\\Sounds\\PlayerBust.mp3"

local soundFilePathDealerBlackjack = "Interface\\AddOns\\BJPlayer\\Sounds\\DealerBlackjack.mp3"
local soundFilePathDealerBust = "Interface\\AddOns\\BJPlayer\\Sounds\\DealerBust.mp3"

local soundFilePathPush = "Interface\\AddOns\\BJPlayer\\Sounds\\Push.mp3"

local soundFilePathHit = "Interface\\AddOns\\BJPlayer\\Sounds\\Hit.mp3"
local soundFilePathStand = "Interface\\AddOns\\BJPlayer\\Sounds\\Stand.mp3"
local soundFilePathDouble = "Interface\\AddOns\\BJPlayer\\Sounds\\Double.mp3"

local soundFilePathDealingCard = "Interface\\AddOns\\BJPlayer\\Sounds\\DealingCard.mp3"
local soundFilePathConfirmBet = "Interface\\AddOns\\BJPlayer\\Sounds\\ConfirmBet.mp3"


-- Set the minimum and maximum bet limits
local minBet = 100
local maxBet = 1500
local betAmount = 0

-- Create a "Confirm Bet" button
local confirmBetButton = CreateFrame("Button", nil, playerFrame, "GameMenuButtonTemplate")
confirmBetButton:SetPoint("BOTTOM", playerFrame, 33, 140)
confirmBetButton:SetSize(65, 30)
confirmBetButton:SetText("Bet")


-- Function to handle the "Confirm Bet" button click
local function ConfirmBetClick()
    betAmount = tonumber(betEditBox:GetText()) -- Get the entered bet amount
    if betAmount and betAmount >= minBet and betAmount <= maxBet then
        -- Valid bet amount, send the message with the bet
        SendChatMessage("{square} I want to place a " ..betAmount.. "G bet", "PARTY")
        local chatFrame = SELECTED_DOCK_FRAME
        ChatFrame_OpenChat("/trade", chatFrame)
        local editBox = _G[chatFrame:GetName().."EditBox"]
        editBox:Insert("/trade")
        ChatEdit_SendText(editBox)
        -- Disable the edit box and button
        betEditBox:SetFocus(false)
        betEditBox:ClearFocus()
        betEditBox:Disable()
        confirmBetButton:Disable()
        PlaySoundFile(soundFilePathConfirmBet)
    else
        print("Invalid bet amount. Please enter a value between " .. minBet .. "G and " .. maxBet .. "G.")
    end
end


-- Set up a script for the "Confirm Bet" button
confirmBetButton:SetScript("OnClick", ConfirmBetClick)

playerFrame.leaveTableButton:SetScript("OnClick", function()
    -- Handle the "leaving" action, communicate with players, and change game state
    SendChatMessage("{circle} I am leaving the table.", "PARTY")
    playerFrame:Hide()
    initialFrame:Show()
end)

playerFrame.rulesButton:SetScript("OnClick", function()
    


end)

-- Slash command handler for the player
SlashCmdList["BJ"] = function(msg)
    if msg == "join" then
        initialFrame:Show()
        -- Handle the "/blackjack join" command
    else
        print("Invalid command. Usage: /bj join")
    end
end

-- Register the slash command
SLASH_BJ1 = "/bj"



-- Function to enable the "Join Table" button
local function EnableJoinButton()
    initialFrame.joinButton:Enable()
end

-- Function to disable the "Join Table" button and hide the Blackjack Player frame
local function PlayerFrameHide()
    playerFrame:Hide()
    initialFrame.joinButton:Disable()
    initialFrame:Show()
end

-- Function to enable the "Join Table" button
local function betReset()
    confirmBetButton:Enable()
    betEditBox:Enable()
    betEditBox:SetText("")
    playerFrame.hitButton:Disable()
    playerFrame.standButton:Disable()
    playerFrame.doubleButton:Disable()
end

local function playerTurn()
    playerFrame.hitButton:Enable()
    playerFrame.standButton:Enable()
    playerFrame.doubleButton:Enable()
end

playerFrame.numberPlayerText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.numberPlayerText:SetPoint("BOTTOM", playerFrame, "BOTTOM", -2, 180)
playerFrame.numberPlayerText:SetFont("Fonts\\FRIZQT__.TTF", 36, "OUTLINE") -- Adjust the font and size as needed
playerFrame.numberPlayerText:SetText("")

playerFrame.numberDealerText = playerFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
playerFrame.numberDealerText:SetPoint("TOP", playerFrame, "TOP", -65, -148)
playerFrame.numberDealerText:SetFont("Fonts\\FRIZQT__.TTF", 36, "OUTLINE") -- Adjust the font and size as needed
playerFrame.numberDealerText:SetText("")

local cardNames = {
    ["AD.tga"] = "Ace of Diamonds",
    ["2D.tga"] = "2 of Diamonds",
    ["3D.tga"] = "3 of Diamonds",
    ["4D.tga"] = "4 of Diamonds",
    ["5D.tga"] = "5 of Diamonds",
    ["6D.tga"] = "6 of Diamonds",
    ["7D.tga"] = "7 of Diamonds",
    ["8D.tga"] = "8 of Diamonds",
    ["9D.tga"] = "9 of Diamonds",
    ["10D.tga"] = "10 of Diamonds",
    ["JD.tga"] = "Jack of Diamonds",
    ["QD.tga"] = "Queen of Diamonds",
    ["KD.tga"] = "King of Diamonds",
    ["AH.tga"] = "Ace of Hearts",
    ["2H.tga"] = "2 of Hearts",
    ["3H.tga"] = "3 of Hearts",
    ["4H.tga"] = "4 of Hearts",
    ["5H.tga"] = "5 of Hearts",
    ["6H.tga"] = "6 of Hearts",
    ["7H.tga"] = "7 of Hearts",
    ["8H.tga"] = "8 of Hearts",
    ["9H.tga"] = "9 of Hearts",
    ["10H.tga"] = "10 of Hearts",
    ["JH.tga"] = "Jack of Hearts",
    ["QH.tga"] = "Queen of Hearts",
    ["KH.tga"] = "King of Hearts",
    ["AC.tga"] = "Ace of Clubs",
    ["2C.tga"] = "2 of Clubs",
    ["3C.tga"] = "3 of Clubs",
    ["4C.tga"] = "4 of Clubs",
    ["5C.tga"] = "5 of Clubs",
    ["6C.tga"] = "6 of Clubs",
    ["7C.tga"] = "7 of Clubs",
    ["8C.tga"] = "8 of Clubs",
    ["9C.tga"] = "9 of Clubs",
    ["10C.tga"] = "10 of Clubs",
    ["JC.tga"] = "Jack of Clubs",
    ["QC.tga"] = "Queen of Clubs",
    ["KC.tga"] = "King of Clubs",
    ["AS.tga"] = "Ace of Spades",
    ["2S.tga"] = "2 of Spades",
    ["3S.tga"] = "3 of Spades",
    ["4S.tga"] = "4 of Spades",
    ["5S.tga"] = "5 of Spades",
    ["6S.tga"] = "6 of Spades",
    ["7S.tga"] = "7 of Spades",
    ["8S.tga"] = "8 of Spades",
    ["9S.tga"] = "9 of Spades",
    ["10S.tga"] = "10 of Spades",
    ["JS.tga"] = "Jack of Spades",
    ["QS.tga"] = "Queen of Spades",
    ["KS.tga"] = "King of Spades",
    -- Add more card entries as needed
}

local cardValues = {
    ["AD.tga"] = {1, 11},
    ["2D.tga"] = {2},
    ["3D.tga"] = {3},
    ["4D.tga"] = {4},
    ["5D.tga"] = {5},
    ["6D.tga"] = {6},
    ["7D.tga"] = {7},
    ["8D.tga"] = {8},
    ["9D.tga"] = {9},
    ["10D.tga"] = {10},
    ["JD.tga"] = {10},
    ["QD.tga"] = {10},
    ["KD.tga"] = {10},
    ["AH.tga"] = {1, 11},
    ["2H.tga"] = {2},
    ["3H.tga"] = {3},
    ["4H.tga"] = {4},
    ["5H.tga"] = {5},
    ["6H.tga"] = {6},
    ["7H.tga"] = {7},
    ["8H.tga"] = {8},
    ["9H.tga"] = {9},
    ["10H.tga"] = {10},
    ["JH.tga"] = {10},
    ["QH.tga"] = {10},
    ["KH.tga"] = {10},
    ["AC.tga"] = {1, 11},
    ["2C.tga"] = {2},
    ["3C.tga"] = {3},
    ["4C.tga"] = {4},
    ["5C.tga"] = {5},
    ["6C.tga"] = {6},
    ["7C.tga"] = {7},
    ["8C.tga"] = {8},
    ["9C.tga"] = {9},
    ["10C.tga"] = {10},
    ["JC.tga"] = {10},
    ["QC.tga"] = {10},
    ["KC.tga"] = {10},
    ["AS.tga"] = {1, 11},
    ["2S.tga"] = {2},
    ["3S.tga"] = {3},
    ["4S.tga"] = {4},
    ["5S.tga"] = {5},
    ["6S.tga"] = {6},
    ["7S.tga"] = {7},
    ["8S.tga"] = {8},
    ["9S.tga"] = {9},
    ["10S.tga"] = {10},
    ["JS.tga"] = {10},
    ["QS.tga"] = {10},
    ["KS.tga"] = {10},
}



-- Create a table to store the drawn cards in order
local drawnCards = {}

-- Initialize the turn counter
local turn = 0
local initialCardPosition = { x = 4, y = 215 }
local secondCardPosition = { x = -6, y = 235 }
local thirdCardPosition = { x = -16, y = 255 }
local fourthCardPosition = { x = -26, y = 275 }
local fifthCardPosition = { x = -36, y = 295 }
local sixthCardPosition = { x = -46, y = 315 }
local seventhCardPosition = { x = -16, y = 255 }
local initialDealerCardPosition = { x = 0, y = -130 }
local secondDealerCardPosition = { x = 15, y = -130 }
local thirdDealerCardPosition = { x = 30, y = -130 }
local fourthDealerCardPosition = { x = 45, y = -130 }
local fifthDealerCardPosition = { x = 60, y = -130 }
local sixthDealerCardPosition = { x = 75, y = -130 }

local countdownTimer
local countdownDuration = 20

local function UpdateTimerDisplay(secondsRemaining)
    playerFrame.timerText:SetText("Timer: " .. secondsRemaining .. " s remaining")
end

local function OnTimerUpdate()
    countdownDuration = countdownDuration - 1

    if countdownDuration <= 0 then
        -- Time's up! Execute your code here when the timer hits 0
        SendChatMessage("Stand", "PARTY")
        playerFrame.hitButton:Disable()
        playerFrame.standButton:Disable()
        playerFrame.doubleButton:Disable()
        turn = 8
        countdownTimer:Cancel()
        playerFrame.timerText:SetPoint("TOPRIGHT", playerFrame, "TOPRIGHT", -280, -150)
        playerFrame.timerText:SetText("Time is up!")
        playerFrame.timerText:SetJustifyH("CENTER")
        playerFrame.timerText:SetTextColor(1, 0, 0)
    else
        UpdateTimerDisplay(countdownDuration)
    end
end

local function StartCountdownTimer()
    if countdownTimer then
        countdownTimer:Cancel()
    end
    countdownTimer = C_Timer.NewTicker(1, OnTimerUpdate, countdownDuration)
    playerFrame.timerText:SetPoint("TOPRIGHT", playerFrame, "TOPRIGHT", -230, -150)
    playerFrame.timerText:Show()
    playerFrame.timerText:SetTextColor(1, 1, 1)
end

-- Function to add a drawn card to the player's cards and display it
local function AddPlayerCard(cardName)
    if cardName and cardNames[cardName] then
        local cardTexture = playerFrame:CreateTexture(nil, "OVERLAY")
        cardTexture:SetSize(64, 128)
        table.insert(drawnCards, cardTexture)  -- Store the texture in the table

        -- Position the card based on the turn count
        if turn == 0 then
            -- Turn 0 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, initialCardPosition.x, initialCardPosition.y)
        elseif turn == 1 then
            -- Turn 1 - deal to the dealer
            cardTexture:SetPoint("TOP", playerFrame, initialDealerCardPosition.x, initialDealerCardPosition.y)
        elseif turn == 2 then
            -- Turn 2 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, secondCardPosition.x, secondCardPosition.y)
        elseif turn == 3 then
            -- Turn 3 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, thirdCardPosition.x, thirdCardPosition.y)
        elseif turn == 4 then
            -- Turn 4 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, fourthCardPosition.x, fourthCardPosition.y)
        elseif turn == 5 then
            -- Turn 5 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, fifthCardPosition.x, fifthCardPosition.y)
        elseif turn == 6 then
            -- Turn 6 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, sixthCardPosition.x, sixthCardPosition.y)
        elseif turn == 7 then
            -- Turn 7 - deal to the player
            cardTexture:SetPoint("BOTTOM", playerFrame, seventhCardPosition.x, seventhCardPosition.y)
        elseif turn == 8 then
            -- Turn 8 - deal to the dealer
            cardTexture:SetPoint("TOP", playerFrame, secondDealerCardPosition.x, secondDealerCardPosition.y)   
        elseif turn == 9 then
            -- Turn 9 - deal to the dealer
            cardTexture:SetPoint("TOP", playerFrame, thirdDealerCardPosition.x, thirdDealerCardPosition.y)   
        elseif turn == 10 then
            -- Turn 10 - deal to the dealer
            cardTexture:SetPoint("TOP", playerFrame, fourthDealerCardPosition.x, fourthDealerCardPosition.y)   
        elseif turn == 11 then
            -- Turn 11 - deal to the dealer
            cardTexture:SetPoint("TOP", playerFrame, fifthDealerCardPosition.x, fifthDealerCardPosition.y)           
        elseif turn == 12 then
            -- Turn 12 - deal to the dealer
            cardTexture:SetPoint("TOP", playerFrame, sixthDealerCardPosition.x, sixthDealerCardPosition.y)           
        end


        cardTexture:SetTexture("Interface\\AddOns\\BJPlayer\\Images\\" .. cardName)
        cardTexture:SetDrawLayer("OVERLAY")

        -- Increment the turn counter
        turn = turn + 1
    end
end


-- Function to handle incoming chat messages
local function HandleChatMessage(message)
    -- Check if the message contains card names
    for cardName, cardFullName in pairs(cardNames) do
        if string.find(message, cardFullName) then
            AddPlayerCard(cardName)
        end
        playerFrame.numberPlayerText:Show()
        playerFrame.numberDealerText:Show()
    end

local BlackjackText = "Blackjack"

local PlayerTotal = 0
local DealerTotal = 0
local playerName = UnitName("player")

local _, _, number = string.find(message, "(%d+)")
    if string.find(message, playerName) and string.find(message, "1 or 11") and turn == 1 then -- Player: Ace
        playerFrame.numberPlayerText:SetText("1 or 11")
        PlaySoundFile(soundFilePath1or11)
    elseif string.find(message, playerName) and string.find(message, "2 or 12") then -- Player: Ace + Ace
        playerFrame.numberPlayerText:SetText("2 or 12")
        PlaySoundFile(soundFilePath2or12)
    elseif string.find(message, playerName) and string.find(message, "3 or 13") then -- Player: Ace + 2   or   2 + Ace
        playerFrame.numberPlayerText:SetText("3 or 13")
        PlaySoundFile(soundFilePath3or13)
    elseif string.find(message, playerName) and string.find(message, "4 or 14") then -- Player: Ace + 3   or   3 + Ace
        playerFrame.numberPlayerText:SetText("4 or 14")
        PlaySoundFile(soundFilePath4or14)
    elseif string.find(message, playerName) and string.find(message, "5 or 15") then -- Player: Ace + 4   or   4 + Ace
        playerFrame.numberPlayerText:SetText("5 or 15")
        PlaySoundFile(soundFilePath5or15)
    elseif string.find(message, playerName) and string.find(message, "6 or 16") then -- Player: Ace + 5   or   5 + Ace
        playerFrame.numberPlayerText:SetText("6 or 16")
        PlaySoundFile(soundFilePath6or16)
    elseif string.find(message, playerName) and string.find(message, "7 or 17") then -- Player: Ace + 6   or   6 + Ace
        playerFrame.numberPlayerText:SetText("7 or 17")
        PlaySoundFile(soundFilePath7or17)
    elseif string.find(message, playerName) and string.find(message, "8 or 18") then -- Player: Ace + 7   or   7 + Ace
        playerFrame.numberPlayerText:SetText("8 or 18")
        PlaySoundFile(soundFilePath8or18)
    elseif string.find(message, playerName) and string.find(message, "9 or 19") then -- Player: Ace + 8   or   8 + Ace
        playerFrame.numberPlayerText:SetText("9 or 19")
        PlaySoundFile(soundFilePath9or19)
    elseif string.find(message, playerName) and string.find(message, "10 or 20") then -- Player: Ace + 9   or   9 + Ace
        playerFrame.numberPlayerText:SetText("10 or 20")
        PlaySoundFile(soundFilePath10or20)
    elseif string.find(message, playerName) and string.find(message, "21") then -- Player: 21
        playerFrame.numberPlayerText:SetText("21")
        PlaySoundFile(soundFilePath21)
    elseif number and tonumber(number) >= 2 and tonumber(number) <= 20 and string.find(message, playerName) then -- Player Total: 2 to 21
        playerFrame.numberPlayerText:SetText(PlayerTotal + number)
        if string.find(message,"received the") and turn == 1 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        elseif string.find(message,"You have") and turn == 3 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        elseif string.find(message,"You have") and turn == 4 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        elseif string.find(message,"You have") and turn == 5 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        elseif string.find(message,"You have") and turn == 6 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        elseif string.find(message,"You have") and turn == 7 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        elseif string.find(message,"You have") and turn == 8 then
            if PlayerTotal + number == 2 then
                PlaySoundFile(soundFilePath2)
            elseif PlayerTotal + number == 3 then
                PlaySoundFile(soundFilePath3)
            elseif PlayerTotal + number == 4 then
                PlaySoundFile(soundFilePath4)
            elseif PlayerTotal + number == 5 then
                PlaySoundFile(soundFilePath5)
            elseif PlayerTotal + number == 6 then
                PlaySoundFile(soundFilePath6)
            elseif PlayerTotal + number == 7 then
                PlaySoundFile(soundFilePath7)
            elseif PlayerTotal + number == 8 then
                PlaySoundFile(soundFilePath8)
            elseif PlayerTotal + number == 9 then
                PlaySoundFile(soundFilePath9)
            elseif PlayerTotal + number == 10 then
                PlaySoundFile(soundFilePath10)
            elseif PlayerTotal + number == 11 then
                PlaySoundFile(soundFilePath11)
            elseif PlayerTotal + number == 12 then
                PlaySoundFile(soundFilePath12)
            elseif PlayerTotal + number == 13 then
                PlaySoundFile(soundFilePath13)
            elseif PlayerTotal + number == 14 then
                PlaySoundFile(soundFilePath14)
            elseif PlayerTotal + number == 15 then
                PlaySoundFile(soundFilePath15)
            elseif PlayerTotal + number == 16 then
                PlaySoundFile(soundFilePath16)
            elseif PlayerTotal + number == 17 then
                PlaySoundFile(soundFilePath17)
            elseif PlayerTotal + number == 18 then
                PlaySoundFile(soundFilePath18)
            elseif PlayerTotal + number == 19 then
                PlaySoundFile(soundFilePath19)
            elseif PlayerTotal + number == 20 then
                PlaySoundFile(soundFilePath20)
            else
            end
        end
    elseif string.find(message, playerName) and string.find(message, "Blackjack") then
        playerFrame.numberPlayerText:SetText(BlackjackText)
        playerFrame.numberPlayerText:SetTextColor(1, 1, 0) -- RGB values for yellow
        PlaySoundFile(soundFilePathPlayerBlackjack)
        playerFrame.hitButton:Disable()
        playerFrame.standButton:Disable()
        playerFrame.doubleButton:Disable()
        countdownDuration = 20
        playerFrame.timerText:SetText("")
        playerFrame.timerText:Hide()
    elseif string.find(message, "22") and string.find(message, playerName) then -- Player (BUST) Total: 22
        playerFrame.numberPlayerText:SetText("22")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "23") and string.find(message, playerName) then -- Player (BUST) Total: 23
        playerFrame.numberPlayerText:SetText("23")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "24") and string.find(message, playerName) then -- Player (BUST) Total: 24
        playerFrame.numberPlayerText:SetText("24")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "25") and string.find(message, playerName) then -- Player (BUST) Total: 25
        playerFrame.numberPlayerText:SetText("25")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "26") and string.find(message, playerName) then -- Player (BUST) Total: 26
        playerFrame.numberPlayerText:SetText("26")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "27") and string.find(message, playerName) then -- Player (BUST) Total: 27
        playerFrame.numberPlayerText:SetText("27")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "28") and string.find(message, playerName) then -- Player (BUST) Total: 28
        playerFrame.numberPlayerText:SetText("28")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "29") and string.find(message, playerName) then -- Player (BUST) Total: 29
        playerFrame.numberPlayerText:SetText("29")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "30") and string.find(message, playerName) then -- Player (BUST) Total: 30
        playerFrame.numberPlayerText:SetText("30")
        playerFrame.numberPlayerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathPlayerBust)
    elseif string.find(message, "Dealer") and string.find(message, "1 or 11") and turn == 2 then -- Dealer: Ace
        playerFrame.numberDealerText:SetText("1 or 11")
        PlaySoundFile(soundFilePath1or11)
    elseif string.find(message, "Dealer") and string.find(message, "2 or 12") then -- Dealer: Ace + Ace
        playerFrame.numberDealerText:SetText("2 or 12")
        PlaySoundFile(soundFilePath2or12)
    elseif string.find(message, "Dealer") and string.find(message, "3 or 13") then -- Dealer: Ace + 2   or   2 + Ace
        playerFrame.numberDealerText:SetText("3 or 13")
        PlaySoundFile(soundFilePath3or13)
    elseif string.find(message, "Dealer") and string.find(message, "4 or 14") then -- Dealer: Ace + 3   or   3 + Ace
        playerFrame.numberDealerText:SetText("4 or 14")
        PlaySoundFile(soundFilePath4or14)
    elseif string.find(message, "Dealer") and string.find(message, "5 or 15") then -- Dealer: Ace + 4   or   4 + Ace
        playerFrame.numberDealerText:SetText("5 or 15")
        PlaySoundFile(soundFilePath5or15)
    elseif string.find(message, "Dealer") and string.find(message, "6 or 16") then -- Dealer: Ace + 5   or   5 + Ace
        playerFrame.numberDealerText:SetText("6 or 16")
        PlaySoundFile(soundFilePath6or16)
    elseif number and tonumber(number) >=2 and tonumber(number) <= 21 and string.find(message, "Dealer") then -- Dealer Total: 2 to 21
        playerFrame.numberDealerText:SetText(DealerTotal + number)
        if string.find(message, "Dealer has 20") then
            PlaySoundFile(soundFilePath20)
        elseif string.find(message, "Dealer has 2") then
            PlaySoundFile(soundFilePath2)
        elseif string.find(message, "Dealer has 3") then
            PlaySoundFile(soundFilePath3)
        elseif string.find(message, "Dealer has 4") then
            PlaySoundFile(soundFilePath4)
        elseif string.find(message, "Dealer has 5") then
            PlaySoundFile(soundFilePath5)
        elseif string.find(message, "Dealer has 6") then
            PlaySoundFile(soundFilePath6)
        elseif string.find(message, "Dealer has 7") then
            PlaySoundFile(soundFilePath7)
        elseif string.find(message, "Dealer has 8") then
            PlaySoundFile(soundFilePath8)
        elseif string.find(message, "Dealer has 9") then
            PlaySoundFile(soundFilePath9)
        elseif string.find(message, "Dealer has 10") then
            PlaySoundFile(soundFilePath10)
        elseif string.find(message, "Dealer has 11") then
            PlaySoundFile(soundFilePath11)
        elseif string.find(message, "Dealer has 12") then
            PlaySoundFile(soundFilePath12)
        elseif string.find(message, "Dealer has 13") then
            PlaySoundFile(soundFilePath13)
        elseif string.find(message, "Dealer has 14") then
            PlaySoundFile(soundFilePath14)
        elseif string.find(message, "Dealer has 15") then
            PlaySoundFile(soundFilePath15)
        elseif string.find(message, "Dealer has 16") then
            PlaySoundFile(soundFilePath16)
        elseif string.find(message, "Dealer has 17") then
            PlaySoundFile(soundFilePath17)
        elseif string.find(message, "Dealer has 18") then
            PlaySoundFile(soundFilePath18)
        elseif string.find(message, "Dealer has 19") then
            PlaySoundFile(soundFilePath19)
        else
        end
    elseif string.find(message, "21") and string.find(message, "Dealer") then
        dealerFrame.numberDealerText:SetText("21")
        PlaySoundFile(soundFilePath21)
    elseif string.find(message, "Dealer") and string.find(message, "Blackjack") then
        playerFrame.numberDealerText:SetText(BlackjackText)
        playerFrame.numberDealerText:SetTextColor(1, 1, 0) -- RGB values for yellow
        PlaySoundFile(soundFilePathDealerBlackjack)
    elseif string.find(message, "22") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 22
        playerFrame.numberDealerText:SetText("22")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "23") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 23
        playerFrame.numberDealerText:SetText("23")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "24") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 24
        playerFrame.numberDealerText:SetText("24")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "25") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 25
        playerFrame.numberDealerText:SetText("25")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "26") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 26
        playerFrame.numberDealerText:SetText("26")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "27") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 27
        playerFrame.numberDealerText:SetText("27")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "28") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 28
        playerFrame.numberDealerText:SetText("28")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "29") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 29
        playerFrame.numberDealerText:SetText("29")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    elseif string.find(message, "30") and string.find(message, "Dealer") then -- Dealer (BUST) Total: 30
        playerFrame.numberDealerText:SetText("30")
        playerFrame.numberDealerText:SetTextColor(1, 0, 0) -- RGB values for red
        PlaySoundFile(soundFilePathDealerBust)
    end
end

-- Function to remove card textures
local function RemoveCardTextures()
    for i = 1, #drawnCards do
        drawnCards[i]:Hide() -- Hide the card texture
        drawnCards[i]:SetTexture(nil)  -- Set the texture to nil
    end
    drawnCards = {} -- Reset the table of card textures
    playerFrame.numberPlayerText:Hide()
    playerFrame.numberDealerText:Hide()

    -- Reset PlayerTotal
    PlayerTotal = 0
    playerFrame.numberPlayerText:SetText("")

    -- Reset DealerTotal
    DealerTotal = 0
    playerFrame.numberDealerText:SetText("")
end

-- Register a chat event to listen for party chat messages
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("CHAT_MSG_PARTY_LEADER")

-- Event handler to listen for party chat messages
eventFrame:SetScript("OnEvent", function(self, event, message, sender)
    -- Check if the message is from the dealer ("Blackjackk-Earthshaker") or the party leader
    if sender == "Blackjackk-Earthshaker" or UnitIsGroupLeader(sender, "party") then
        HandleChatMessage(message)
        -- If it's the dealer or party leader, check the message content
        if message == "I have opened the table." then
            EnableJoinButton()
        elseif message == "I have closed the table." then
            PlayerFrameHide()
            betReset()
        elseif message == "I have started a new round." then
            betReset()
            RemoveCardTextures()  -- Call the function to remove card textures
            playerFrame.numberPlayerText:SetTextColor(1, 1, 1)
            playerFrame.numberDealerText:SetTextColor(1, 1, 1)
            turn = 0
            countdownDuration = 20
            playerFrame.timerText:SetText("")
            playerFrame.timerText:Hide()
        elseif string.find(message, "push!") then
            PlaySoundFile(soundFilePathPush)
        elseif string.find(message, "bust") and string.find(message, "You") then
            PlaySoundFile(soundFilePathPlayerBust)
        elseif string.find(message, "has less than") and string.find(message, "You") then
            PlaySoundFile(soundFilePathPlayerLoss)
        elseif string.find(message, "has more than") and string.find(message, "You") then
            PlaySoundFile(soundFilePathPlayerWin)
        elseif string.find(message, "You win this") then
            PlaySoundFile(soundFilePathPlayerWin)
        elseif string.find(message, "You lost this") then
            PlaySoundFile(soundFilePathPlayerLoss)
        elseif string.find(message, "Dealer") and turn >= 8 then
            countdownDuration = 20
            playerFrame.timerText:SetText("")
            playerFrame.timerText:Hide()
        elseif string.find(message, "You have 21") and turn <= 8 then
            turn = 8
        else
        end

        -- Check if the message contains your character's name
        local playerName = UnitName("player")
        if string.find(message, "What is your decision, " .. playerName .. "?") then
            -- Enable the hit button when your name is mentioned
            playerTurn()
            StartCountdownTimer()

        end
    end
end)

-- Register a chat event to listen for party chat messages
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("CHAT_MSG_PARTY")

local playerName = UnitName("player")

eventFrame:SetScript("OnEvent", function(self, event, message, sender)
    if string.find(message, "I want to double my bet to") then
        turn = 7
    end
end)
    
playerFrame.hitButton:SetScript("OnClick", function()
    -- Handle the "hit" button action
    SendChatMessage("Hit", "PARTY")
    playerFrame.hitButton:Disable()
    playerFrame.standButton:Disable()
    playerFrame.doubleButton:Disable()
    playerFrame.timerText:SetText("")
    playerFrame.timerText:Hide()
    countdownDuration = 20
end)

playerFrame.standButton:SetScript("OnClick", function()
    SendChatMessage("Stand", "PARTY")
    turn = 8
    playerFrame.hitButton:Disable()
    playerFrame.standButton:Disable()
    playerFrame.doubleButton:Disable()
    playerFrame.timerText:SetText("")
    playerFrame.timerText:Hide()
    countdownDuration = 20
    if playerFrame.numberPlayerText:GetText() == "1 or 11" then
        playerFrame.numberPlayerText:SetText("11")
    elseif playerFrame.numberPlayerText:GetText() == "2 or 12" then
        playerFrame.numberPlayerText:SetText("12")
    elseif playerFrame.numberPlayerText:GetText() == "3 or 13" then
        playerFrame.numberPlayerText:SetText("13")
    elseif playerFrame.numberPlayerText:GetText() == "4 or 14" then
        playerFrame.numberPlayerText:SetText("14")
    elseif playerFrame.numberPlayerText:GetText() == "5 or 15" then
        playerFrame.numberPlayerText:SetText("15")
    elseif playerFrame.numberPlayerText:GetText() == "6 or 16" then
        playerFrame.numberPlayerText:SetText("16")
    elseif playerFrame.numberPlayerText:GetText() == "7 or 17" then
        playerFrame.numberPlayerText:SetText("17")
    elseif playerFrame.numberPlayerText:GetText() == "8 or 18" then
        playerFrame.numberPlayerText:SetText("18")
    elseif playerFrame.numberPlayerText:GetText() == "9 or 19" then
        playerFrame.numberPlayerText:SetText("19")
    elseif playerFrame.numberPlayerText:GetText() == "10 or 20" then
        playerFrame.numberPlayerText:SetText("20")
    end
end)

-- Set up a script for the "Doubke" button
playerFrame.doubleButton:SetScript("OnClick", function()
    local betDoubleAmount = 0
    betAmount = tonumber(betEditBox:GetText()) -- Get the entered bet amount
    if betAmount and betAmount >= minBet and betAmount <= maxBet then
        betDoubleAmount = betAmount * 2
        SendChatMessage("{square} I want to double my bet to " ..betDoubleAmount.. "G", "PARTY")
        betEditBox:SetText(betDoubleAmount)
        playerFrame.hitButton:Disable()
        playerFrame.standButton:Disable()
        playerFrame.doubleButton:Disable() 
        playerFrame.timerText:SetText("")
        playerFrame.timerText:Hide()
        countdownDuration = 20
    end
end)

-- Add a click event handler to the "Join Table" button
initialFrame.joinButton:SetScript("OnClick", function(self, button, down)
    if button == "LeftButton" then
        -- Hide the initial frame
        initialFrame:Hide()
        -- Show the playerFrame
        playerFrame:Show()
        SendChatMessage("{circle} I have joined the table.", "PARTY")
        -- You can add more code here to initialize the playerFrame or start the game.
    end
end)
