-- This file is part of AutoInvite
--
-- (C) 2014 Scott Yeskie (Sasky)
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

AutoInviteUI = {}
local ui = AutoInviteUI
local wm = WINDOW_MANAGER

function AutoInviteUI.create()
    local pre = "AAAAutoInviteUI"
    ui.main = wm:CreateTopLevelWindow(pre .. "Panel")
    ui.main:SetAnchor(TOPRIGHT, ZO_SharedRightBackground, TOPLEFT, -50, 50)
    ui.main:SetDimensions(280,450)

    ui.scroll = ui.main -- For using LAM controls
    ui.data = {}

    ui.bg = wm:CreateControl(pre .. "bg", ui.main, CT_BACKDROP)
    ui.bg:SetDimensions(280,450)
    ui.bg:SetAnchor(TOPLEFT, ui.main, TOPLEFT, 0, 0)
    ui.bg:SetCenterColor( 0, 0, 0, .7 )
    ui.bg:SetEdgeColor( 1, 1, 1, 0 )

    ui.title = LAMCreateControl.header(ui, {
        type = "header",
        name = "AutoInvite Options",
        width = "half"
    })
    ui.title:SetAnchor(TOPLEFT, ui.main, TOPLEFT, 5, 5)
    ui.title:SetWidth(270)
    ui.title.divider:ClearAnchors()
    ui.title.divider:SetAnchor(BOTTOM)
    ui.title.header:ClearAnchors()
    ui.title.header:SetAnchor(TOPLEFT)

    ui.enabled = LAMCreateControl.checkbox(ui, {
        type = "checkbox",
        name = "Enabled",
        tooltip = "Whether to enable AutoInvite",
        getFunc = function() return AutoInvite.listening end,
        setFunc = function(val)
            if val then AutoInvite.startListening() else AutoInvite.disable() end
        end,
        width = "half"
    })
    ui.enabled:SetDimensions(250, 26)
    ui.enabled:SetAnchor(TOPLEFT, ui.title, BOTTOMLEFT, 10, 5)

    ui.text = LAMCreateControl.editbox(ui, {
        type = "editbox",
        name = "Invite String",
        tooltip = "Text to check messages to auto-invite for",
        getFunc = function() return AutoInvite.cfg.watchStr end,
        setFunc = function(val) AutoInvite.cfg.watchStr = val end,
        width = "half"
    })
    ui.text:SetWidth(260)
    ui.text:SetAnchor(TOPLEFT, ui.enabled, BOTTOMLEFT, 0, 5)

    ui.max = LAMCreateControl.slider(ui, {
        type = "slider",
        name = "Max group size",
        tooltip = "Maximum number of players to invite to group",
        min = 4,
        max = 24,
        getFunc = function() return AutoInvite.cfg.maxSize end,
        setFunc = function(val) AutoInvite.cfg.maxSize = val end,
        default = 24,
        width = "half"
    })
    ui.max:SetAnchor(TOPLEFT, ui.text, BOTTOMLEFT, 0, 10)

--    ui.expr = LAMCreateControl.header(ui, {
--        type = "header",
--        name = "Experimental",
--        width = "half"
--    })
--    ui.expr:SetAnchor(TOPLEFT, ui.max, TOPLEFT, 0, 65)
--    ui.expr:SetWidth(270)
--    ui.expr.divider:ClearAnchors()
--    ui.expr.divider:SetAnchor(BOTTOM)
--    ui.expr.header:ClearAnchors()
--    ui.expr.header:SetAnchor(TOPLEFT)
--    ui.expr.header:SetColor(1.0, 0.3, 0.3, 1.0)

    ui.restart = LAMCreateControl.checkbox(ui, {
        type = "checkbox",
        name = "Restart",
        tooltip = "Restart AutoInvite if drop below max",
        getFunc = function() return AutoInvite.cfg.restart end,
        setFunc = function(val) AutoInvite.cfg.restart = val  end,
        width = "half"
    })
    ui.restart:SetDimensions(250, 26)
    ui.restart:SetAnchor(TOPLEFT, ui.max, BOTTOMLEFT, 0, 12)

    ui.cyr = LAMCreateControl.checkbox(ui, {
        type = "checkbox",
        name = "Cyrodiil Check",
        tooltip = "Only invite players that are in Cyrodiil.\n(This only runs if you are in Cyrodiil yourself.)",
        getFunc = function() return AutoInvite.cfg.cyrCheck end,
        setFunc = function(val) AutoInvite.cfg.cyrCheck = val end,
        width = "half"
    })
    ui.cyr:SetDimensions(250, 26)
    ui.cyr:SetAnchor(TOPLEFT, ui.restart, BOTTOMLEFT, 0, 5)

    ui.kick = LAMCreateControl.checkbox(ui, {
        type = "checkbox",
        name = "Auto kick",
        tooltip = "Kick players that go offline",
        getFunc = function() return AutoInvite.cfg.autoKick end,
        setFunc = function(val) AutoInvite.cfg.autoKick = val  end,
        width = "half"
    })
    ui.kick:SetDimensions(250, 26)
    ui.kick:SetAnchor(TOPLEFT, ui.cyr, BOTTOMLEFT, 0, 5)

    ui.kickTime = LAMCreateControl.slider(ui, {
        type = "slider",
        name = "Time before kick",
        tooltip = "Number of seconds to wait before kicking an offline player",
        min = 5,
        max = 600,
        getFunc = function() return AutoInvite.cfg.kickDelay end,
        setFunc = function(val) AutoInvite.cfg.kickDelay = val end,
        default = 300,
        width = "half"
    })
    ui.kickTime:SetAnchor(TOPLEFT, ui.kick, BOTTOMLEFT, 0, 5)

    ui.note = LAMCreateControl.description(ui, {
        type = "description",
        title = "Slash Commands",
        text = "/ai foo - start listening on 'foo'\n/ai - stop listening",
        width = "half"
    })
    ui.note:SetAnchor(TOPLEFT, ui.kickTime, BOTTOMLEFT, 0, 25)
    ui.note.desc:SetColor(.7,.7,.7,1)
end

function AutoInviteUI.menuShow()
    AutoInviteUI.showIfGroup()
    AutoInviteUI.refresh()
    EVENT_MANAGER:RegisterForUpdate("AutoInviteMenuCheck", 100, AutoInviteUI.showIfGroup)
end

function AutoInviteUI.showIfGroup()
    ui.main:SetHidden(ZO_GroupList:IsHidden())
end

function AutoInviteUI.menuHide()
    ui.main:SetHidden(true)
    EVENT_MANAGER:UnregisterForUpdate("AutoInviteMenuCheck")
end

function AutoInviteUI.refresh()
    ui.enabled:UpdateValue()
    ui.cyr:UpdateValue()
    ui.restart:UpdateValue()
    ui.max:UpdateValue()
    ui.text:UpdateValue()
end

function AutoInviteUI.init()
    if ui.created then return end
    ui.created = true
    AutoInviteUI.create()
    ui.main:SetHidden(true)

    --Register events
    EVENT_MANAGER:RegisterForEvent('AutoInvite', EVENT_ACTION_LAYER_POPPED, AutoInviteUI.menuHide)
    EVENT_MANAGER:RegisterForEvent('AutoInvite', EVENT_ACTION_LAYER_PUSHED, AutoInviteUI.menuShow)
end

SLASH_COMMANDS["/zzg"] = function()
    d(ZO_GroupList:IsHidden())
end