EbonholdHub.BuildTabs = {}

local UI = EbonholdHub.UI
local C  = UI.Colors
local ST = EbonholdHub.SiteTheme
local SW = EbonholdHub.SiteWidgets
local SC = ST and ST.C
local SL = ST and ST.Layout
local SIDEBAR_W = SL and SL.SIDEBAR_W or 280

local FOOTER_HEIGHT = 36
local SITE_TAB_H = 36

local viewFrame
local contentArea
local sidebarArea
local tabBar
local siteTabState
local saveBtn, cancelBtn
local state = { context = nil }

local function RefreshButtons()
end

function EbonholdHub.BuildTabs.OnBuildSaved()
    state.context = { mode = "edit", build = EbonholdHub.Build.GetActive() }
    RefreshButtons()
end

function EbonholdHub.BuildTabs.OnDraftPersisted()
    state.context = { mode = "edit", build = EbonholdHub.Build.GetActive() }
end

local function BeforeTabSwitch()
    EbonholdHub.BuildForm.Unmount()
    EbonholdHub.TalentsView.Unmount()
    EbonholdHub.GearView.Unmount()
    EbonholdHub.WeightsView.Unmount()
    EbonholdHub.SettingsView.Unmount()
    if EbonholdHub.AdvisorView then EbonholdHub.AdvisorView.Unmount() end
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.PersistDraft then
        EbonholdHub.BuildForm.PersistDraft()
    end
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.RefreshSidebar then
        EbonholdHub.BuildForm.RefreshSidebar()
    end
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.UnmountDescriptionTab then
        EbonholdHub.BuildForm.UnmountDescriptionTab()
    end
end

local function ShowOverview()
    BeforeTabSwitch()
    tabBar.SelectTab(1, false)
    EbonholdHub.BuildForm.Mount(contentArea, state.context)
end

local function ShowTalents()
    BeforeTabSwitch()
    tabBar.SelectTab(2, false)
    EbonholdHub.TalentsView.Mount(contentArea)
end

local function ShowGear()
    BeforeTabSwitch()
    tabBar.SelectTab(3, false)
    EbonholdHub.GearView.Mount(contentArea)
end

local function ShowEchoes()
    BeforeTabSwitch()
    tabBar.SelectTab(4, false)
    EbonholdHub.WeightsView.Mount(contentArea)
end

local function ShowAutomation()
    BeforeTabSwitch()
    tabBar.SelectTab(5, false)
    EbonholdHub.SettingsView.Mount(contentArea)
end

local function ShowAdvisor()
    BeforeTabSwitch()
    tabBar.SelectTab(6, false)
    if EbonholdHub.AdvisorView then
        EbonholdHub.AdvisorView.Mount(contentArea)
    end
end

local function ShowDescription()
    BeforeTabSwitch()
    tabBar.SelectTab(7, false)
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.MountDescriptionTab then
        EbonholdHub.BuildForm.MountDescriptionTab(contentArea)
    end
end

local function CreateContentArea(parent, footerBar)
    local inner = CreateFrame("Frame", nil, parent)
    inner:SetPoint("TOPLEFT",     parent, "TOPLEFT",     0, 0)
    inner:SetPoint("TOPRIGHT",    parent, "TOPRIGHT",    0, 0)
    inner:SetPoint("BOTTOMLEFT",  footerBar, "TOPLEFT",   0,  6)
    inner:SetPoint("BOTTOMRIGHT", footerBar, "TOPRIGHT",  0,  6)

    sidebarArea = CreateFrame("Frame", nil, inner)
    sidebarArea:SetWidth(SIDEBAR_W)
    sidebarArea:SetPoint("TOPLEFT", inner, "TOPLEFT", 0, 0)
    sidebarArea:SetPoint("BOTTOMLEFT", inner, "BOTTOMLEFT", 0, 0)

    local divider = inner:CreateTexture(nil, "ARTWORK")
    divider:SetTexture(ST.FLAT)
    divider:SetVertexColor(unpack(SC.border))
    divider:SetWidth(1)
    divider:SetPoint("TOPLEFT", sidebarArea, "TOPRIGHT", 0, 0)
    divider:SetPoint("BOTTOMLEFT", sidebarArea, "BOTTOMRIGHT", 0, 0)

    local main = CreateFrame("Frame", nil, inner)
    main:SetPoint("TOPLEFT", sidebarArea, "TOPRIGHT", 1, 0)
    main:SetPoint("BOTTOMRIGHT", inner, "BOTTOMRIGHT", 0, 0)
    SW.Fill(main, SC.mainBg)

    local siteTabBar = CreateFrame("Frame", nil, main)
    siteTabBar:SetPoint("TOPLEFT", 0, 0)
    siteTabBar:SetPoint("TOPRIGHT", 0, 0)
    siteTabBar:SetHeight(SITE_TAB_H)
    SW.Fill(siteTabBar, SC.tabBarBg)
    local tabSep = siteTabBar:CreateTexture(nil, "ARTWORK")
    tabSep:SetTexture(ST.FLAT)
    tabSep:SetVertexColor(unpack(SC.border))
    tabSep:SetHeight(1)
    tabSep:SetPoint("BOTTOMLEFT", 0, 0)
    tabSep:SetPoint("BOTTOMRIGHT", 0, 0)

    siteTabState = { selectedIndex = 1 }
    local siteTabBtns = {}
    local tabDefs = {
        { name = "Affixes",     w = 80 },
        { name = "Talents",     w = 80 },
        { name = "Best Gear",   w = 95 },
        { name = "Echoes",      w = 80 },
        { name = "Auto Pick",   w = 90 },
        { name = "Advisor",     w = 80 },
        { name = "Description", w = 100 },
    }
    local prevTabBtn
    for i, def in ipairs(tabDefs) do
        local btn = SW.CreateTabButton(siteTabBar, def.name, i, siteTabState)
        btn:SetWidth(def.w)
        if prevTabBtn then
            btn:SetPoint("LEFT", prevTabBtn, "RIGHT", 4, 0)
        else
            btn:SetPoint("LEFT", SL.PAD, 0)
        end
        prevTabBtn = btn
        siteTabBtns[i] = btn
    end

    local tabContent = CreateFrame("Frame", nil, main)
    tabContent:SetPoint("TOPLEFT", siteTabBar, "BOTTOMLEFT", 0, 0)
    tabContent:SetPoint("BOTTOMRIGHT", 0, 0)

    local bar = {}
    bar.selectedIndex = 1

    function bar.SelectTab(index, fireCallback)
        index = index or 1
        siteTabState.selectedIndex = index
        bar.selectedIndex = index
        for i, btn in ipairs(siteTabBtns) do
            if i == index then
                btn._underline:Show()
                btn._label:SetTextColor(unpack(SC.accent))
            else
                btn._underline:Hide()
                btn._label:SetTextColor(unpack(SC.textMuted))
            end
        end
        if fireCallback ~= false and bar.OnTabSelected then
            bar.OnTabSelected(index)
        end
    end

    function siteTabState.SelectTab(index)
        bar.SelectTab(index, true)
    end

    tabBar = bar
    return tabContent
end

function EbonholdHub.BuildTabs.GetSidebarArea()
    return sidebarArea
end

local function BuildViewFrame()
    local f = CreateFrame("Frame", "EbonholdHubBuildTabs", UIParent)

    local footerBar = CreateFrame("Frame", nil, f)
    footerBar:SetPoint("BOTTOMLEFT", f, "BOTTOMLEFT", 0, 0)
    footerBar:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", 0, 0)
    footerBar:SetHeight(FOOTER_HEIGHT)
    UI.Fill(footerBar, C.bgHeader)

    local footerSep = footerBar:CreateTexture(nil, "ARTWORK")
    footerSep:SetTexture("Interface\\Buttons\\WHITE8X8")
    footerSep:SetVertexColor(unpack(C.border))
    footerSep:SetPoint("TOPLEFT", footerBar, "TOPLEFT", 0, 0)
    footerSep:SetPoint("TOPRIGHT", footerBar, "TOPRIGHT", 0, 0)
    footerSep:SetHeight(1)

    contentArea = CreateContentArea(f, footerBar)

    tabBar.OnTabSelected = function(index)
        if index == 1 then ShowOverview()
        elseif index == 2 then ShowTalents()
        elseif index == 3 then ShowGear()
        elseif index == 4 then ShowEchoes()
        elseif index == 5 then ShowAutomation()
        elseif index == 6 then ShowAdvisor()
        elseif index == 7 then ShowDescription()
        end
    end

    saveBtn = UI.CreateButton(footerBar, 90, 28, "Save")
    saveBtn:SetPoint("RIGHT", footerBar, "RIGHT", -10, 0)
    saveBtn:SetScript("OnClick", function() EbonholdHub.BuildForm.Save() end)

    cancelBtn = UI.CreateButton(footerBar, 90, 28, "Cancel")
    cancelBtn:SetPoint("RIGHT", saveBtn, "LEFT", -6, 0)
    cancelBtn:SetScript("OnClick", function() EbonholdHub.BuildForm.Cancel() end)

    local exportBtn = UI.CreateButton(footerBar, 90, 28, "Export")
    exportBtn:SetPoint("LEFT", footerBar, "LEFT", 10, 0)
    exportBtn:SetScript("OnClick", function()
        local build = EbonholdHub.Build.GetActive()
        if build then
            EbonholdHub.ExportImport.ShowExportDialog(build)
        end
    end)

    return f
end

function EbonholdHub.BuildTabs.EnableEchoesTab()
end

local view = {}

function view.Show(container, context)
    viewFrame:SetParent(container)
    viewFrame:ClearAllPoints()
    viewFrame:SetAllPoints(container)

    state.context = context or { mode = "create" }

    EbonholdHub.TalentsView.Unmount()
    EbonholdHub.GearView.Unmount()
    EbonholdHub.WeightsView.Unmount()
    EbonholdHub.SettingsView.Unmount()
    EbonholdHub.BuildForm.Mount(contentArea, state.context)
    tabBar.SelectTab(1, false)
    viewFrame:Show()
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.RefreshSidebar then
        EbonholdHub.BuildForm.RefreshSidebar()
    end
end

function view.Hide()
    EbonholdHubDB._isEditingBuild = nil
    EbonholdHubDB.pendingAffixLoadout = nil
    EbonholdHub.EquipmentLoadout.ClearPending()
    EbonholdHub.BuildForm.Unmount()
    EbonholdHub.TalentsView.Unmount()
    EbonholdHub.GearView.Unmount()
    EbonholdHub.WeightsView.Unmount()
    EbonholdHub.SettingsView.Unmount()
    if EbonholdHub.AdvisorView then EbonholdHub.AdvisorView.Unmount() end
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.UnmountDescriptionTab then
        EbonholdHub.BuildForm.UnmountDescriptionTab()
    end
    if viewFrame then viewFrame:Hide() end
end

function EbonholdHub.BuildTabs.Init()
    viewFrame = BuildViewFrame()
    viewFrame:Hide()
    if EbonholdHub.BuildForm and EbonholdHub.BuildForm.InitSidebar and sidebarArea then
        EbonholdHub.BuildForm.InitSidebar(sidebarArea)
    end
    EbonholdHub.ViewRouter.Register("buildTabs", view)
end
