-- 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 = "AutoInviteUI"
    ui.main = wm:CreateControl(nil, ZO_GroupList, CT_CONTROL)
    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.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)

    ui.hide = wm:CreateControlFromVirtual(nil, ui.main, "ZO_DefaultButton")
    ui.hide:SetAnchor(TOPRIGHT, ui.main, TOPRIGHT, 7, 2)
    ui.hide:SetWidth(40)
    ui.hide:SetText("X")
    ui.hide:SetScale(0.8)
    ui.hide:SetHandler("OnMouseEnter", ZO_Options_OnMouseEnter)
    ui.hide:SetHandler("OnMouseExit", ZO_Options_OnMouseExit)
    ui.hide:SetHandler("OnClicked", AutoInviteUI.menuHide)

    ui.show = wm:CreateControlFromVirtual(nil, ZO_GroupList, "ZO_DefaultButton")
    ui.show:SetAnchor(TOPLEFT, ZO_GroupList, TOPLEFT, 120, -40)
    ui.show:SetWidth(160)
    ui.show:SetText("AutoInvite Settings")
    ui.show:SetHandler("OnMouseEnter", ZO_Options_OnMouseEnter)
    ui.show:SetHandler("OnMouseExit", ZO_Options_OnMouseExit)
    ui.show:SetHandler("OnClicked", AutoInviteUI.menuShow)
end

function AutoInviteUI.menuShow()
    ui.main:SetHidden(false)
    ui.show:SetHidden(true)
    AutoInvite.cfg.showPanel = true
end

function AutoInviteUI.menuHide()
    ui.main:SetHidden(true)
    ui.show:SetHidden(false)
    AutoInvite.cfg.showPanel = false
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()
    if AutoInvite.cfg.showPanel then
        ui.show:SetHidden(true)
    else
        ui.main:SetHidden(true)
    end
end

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