--[[
    Addon: Easy Difficulty & Reset
    Modified & Ported to Addon by: SeolPPe 
    ## Author: SeolPPe (Original by zENK)  Azshara-SeolPPe
## Version: 1.0.0
## SavedVariables: EasyDiffDB
## IconTexture: Interface\Icons\INV_Misc_Dice_01
]]

local EDRL = {}
-- 2. 로컬라이징
if GetLocale() == "zhCN" then
    EDRL.DUNGEON = "副本"
    EDRL.RAID = "团本"
    EDRL.SIZE = "旧版团队规模"
    EDRL.NORMAL = "普 通"
    EDRL.HEROIC = "英 雄"
    EDRL.MYTHIC = "史 诗"
    EDRL.RESET = "重置所有副本"
    EDRL.RESET_MSG = "|cff00ff00[2ui]|r|cff00ff00全部副本已重置.|r"
    EDRL.NO_PERMISSION = "|cffff0000无权限操作(你不是队长).|r"
    EDRL.TEN = "10人"
    EDRL.TWENTY_FIVE = "25人"
    EDRL.DIFF_CHANGED = "|cff00ff00[2ui]|r副本难度设置为: "
else
    EDRL.DUNGEON = "Dungeon"
    EDRL.RAID = "Raid"
    EDRL.SIZE = "Legacy Size"
    EDRL.NORMAL = "Norm"
    EDRL.HEROIC = "Hero"
    EDRL.MYTHIC = "Myth"
    EDRL.RESET = "Reset All Instances"
    EDRL.RESET_MSG = "|cff00ff00All instances have been reset.|r"
    EDRL.NO_PERMISSION = "|cffff0000Leader/Assistant only.|r"
    EDRL.TEN = "10-Man"
    EDRL.TWENTY_FIVE = "25-Man"
    EDRL.DIFF_CHANGED = "|cff00ff00[EDR]|r Difficulty set to: "
end

-- 4. 메인 메뉴 프레임
local frame = CreateFrame("Frame", "EasyDiffMain", GameMenuFrame)
frame:SetSize(200, 70); frame:SetPoint("BOTTOM", GameMenuFrame, "TOP", 0, 8); frame:SetFrameStrata("HIGH")
--local bg = frame:CreateTexture(nil, "BACKGROUND"); bg:SetAllPoints(); bg:SetColorTexture(0, 0, 0, 0.7);

-- 6. 가시성 및 색상 동기화 함수
local buttons = { dungeon = {}, raid = {}, size = {} }

-- 7. 메뉴 내부 버튼 생성
local function CreateActionBtn(category, text, x, y, width, height, funcName, diffID)
    local btn = CreateFrame("Button", nil, frame)
    btn:SetSize(width, height); btn:SetPoint("TOPLEFT", frame, "TOPLEFT", x, y)
    local fs = btn:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    fs:SetPoint("CENTER"); fs:SetText(text); fs:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
    btn.fs = fs
    buttons[category][diffID] = btn
    
    btn:SetScript("OnClick", function()
        if IsInGroup() and not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then 
            print(EDRL.NO_PERMISSION)
            return 
        end
        if funcName == "Dungeon" then SetDungeonDifficultyID(diffID)
        elseif funcName == "Raid" then SetRaidDifficultyID(diffID)
        elseif funcName == "Legacy" then SetLegacyRaidDifficultyID(diffID) end
        
        -- 피드백: 버튼 클릭 시 안내 (실제 변경은 이벤트 발생 시 확인됨)
        --print(EDRL.DIFF_CHANGED .. "|cffffd100" .. text .. "|r")
    end)
    return btn
end

local function CreateHeader(text, y)
    local h = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    h:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, y); h:SetText(text); h:SetTextColor(1, 0.82, 0); h:SetFont(STANDARD_TEXT_FONT, 15, "OUTLINE")
end

-- 버튼들 생성
--CreateHeader(EDRL.DUNGEON, 0)
CreateActionBtn("dungeon", EDRL.NORMAL, 0, 0, 40, 20, "Dungeon", 1)
CreateActionBtn("dungeon", EDRL.HEROIC, 80, 0, 40, 20, "Dungeon", 2)
CreateActionBtn("dungeon", EDRL.MYTHIC, 160, 0, 40, 20, "Dungeon", 23)
--CreateHeader(EDRL.RAID, -25)
CreateActionBtn("raid", EDRL.NORMAL, 0, -25, 40, 20, "Raid", 14)
CreateActionBtn("raid", EDRL.HEROIC, 80, -25, 40, 20, "Raid", 15)
CreateActionBtn("raid", EDRL.MYTHIC, 160, -25, 40, 20, "Raid", 16)
--CreateHeader(EDRL.SIZE, -80)
CreateActionBtn("size", EDRL.TEN, 0, -50, 50, 20, "Legacy", 3)
CreateActionBtn("size", EDRL.TWENTY_FIVE, 150, -50, 50, 20, "Legacy", 4)

local resetBtn = CreateFrame("Button", nil, frame)
resetBtn:SetSize(150, 20); resetBtn:SetPoint("TOP", frame, "TOP", 0, -50)
local resetFs = resetBtn:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
resetFs:SetPoint("CENTER"); resetFs:SetText(EDRL.RESET); resetFs:SetFont(STANDARD_TEXT_FONT, 14, "OUTLINE"); resetFs:SetTextColor(1, 1, 1)
resetBtn:SetScript("OnClick", function() ResetInstances(); print(EDRL.RESET_MSG) end)

-- 8. 초기화 및 이벤트 등록
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
eventFrame:RegisterEvent("PLAYER_DIFFICULTY_CHANGED") -- 난이도 변경 감지 추가
eventFrame:SetScript("OnEvent", function()
    -- 2) 현재 서버 난이도와 버튼 색상 동기화
    local dID = GetDungeonDifficultyID()
    local rID = GetRaidDifficultyID()
    local lID = GetLegacyRaidDifficultyID()

    for id, btn in pairs(buttons.dungeon) do btn.fs:SetTextColor(id == dID and 0.2 or 1, id == dID and 1 or 0.3, id == dID and 0.2 or 0.2) end
    for id, btn in pairs(buttons.raid) do btn.fs:SetTextColor(id == rID and 0.2 or 1, id == rID and 1 or 0.3, id == rID and 0.2 or 0.2) end
    for id, btn in pairs(buttons.size) do btn.fs:SetTextColor(id == lID and 0.2 or 1, id == lID and 1 or 0.3, id == lID and 0.2 or 0.2) end
end)