
---------------------------------------------------------------------
-- PC Options
---------------------------------------------------------------------

menu.options = mdkCreateMenu(0.5, 0.3, 0.06, 0.06, 0.01, JUSTIFY_CENTER, 1, strings.options)

function BuildOptionsMenu()
    mdkClearMenu(menu.options)

    item = mdkCreateMenuItemW(menu.options, strings.opt_video)
    item.Select = function(self)
        BuildVideoMenu()
        menu.SwitchDlg("optvideo")
        mdkShowMouse(1,0)
    end

    item = mdkCreateMenuItemW(menu.options, strings.opt_audio)
    item.Select = function(self)
        BuildAudioMenu()
        menu.SwitchDlg("optaudio")
        mdkShowMouse(1,0)
    end

    item = mdkCreateMenuItemW(menu.options, strings.opt_controls)
    item.Select = function(self)
        BuildControlsMenu()
        menu.SwitchDlg("optcontrols")
        mdkShowMouse(1,0)
    end

    item = mdkCreateMenuItemW(menu.options, strings.opt_game)
    item.Select = function(self)
        BuildGameMenu()
        menu.SwitchDlg("optgame")
        mdkShowMouse(1,0)
    end

    item = mdkCreateMenuItemW(menu.options, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.03)
    item.Select = function()
        menu.options.Cancel()
    end

end


---------------------------------------------------------------------
-- Game
---------------------------------------------------------------------

menu.optgame = mdkCreateMenu(0.66, 0.25, 0.06, 0.06, 0.01, JUSTIFY_RIGHT, 1, strings.opt_game)

function BuildGameMenu()

    local item

    mdkClearMenu(menu.optgame)

    item = mdkCreateMenuItemW(menu.optgame, strings.opt_movietext)
    mdkMenuItemAddCheckBox(item, mdkDialogGetVisible())
    item.Select = function(item, n)
        mdkDialogSetVisible(n)
    end

    item = mdkCreateMenuItemW(menu.optgame, strings.opt_showperf)
    mdkMenuItemAddCheckBox(item, mdkGetShowStatus())
    item.Select = function(item, n)
        mdkToggleShowStatus()
    end

    item = mdkCreateMenuItemW(menu.optgame, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.03)
    item.Select = function()
        menu.optgame.Cancel()
    end

end

menu.optgame.Cancel = function()
    mdkSaveOptions()
    menu.SwitchDlg("options", 0)
    mdkShowMouse(1,0)
end


---------------------------------------------------------------------
-- Video
---------------------------------------------------------------------

menu.optvideo = mdkCreateMenu(0.382, 0.2, 0.06, 0.06, 0.01, JUSTIFY_RIGHT, 1, strings.opt_video)

function VidMenuItemChanged()
    if (not menu.optvideo.changed) then
        menu.optvideo.changed = 1
        mdkMenuItemEnable(menu.optvideo.applyitem, 1)
    end
end

function BuildVideoMenu()

    local n,i,item

    mdkClearMenu(menu.optvideo)
    menu.optvideo.changed = nil

	item = mdkCreateMenuItemW(menu.optvideo, strings.opt_driver)
	mdkMenuItemAddComboBox(item)
	n = chVideoGetDriverTotal()
	i = 0
	while (i < n) do
		local name = chVideoGetDriverName(i)
		name = AbbreviateString(name, 21)
		mdkMenuItemAddComboString(item, name)
		i = i + 1
	end
	mdkMenuItemSetWidgitValue(item, chVideoGetDriverIndex())
	menu.optvideo.driver = item
	item.Select = VidMenuItemChanged
	item.Inc = VidMenuItemChanged
	item.Dec = VidMenuItemChanged

	if (chIsWindowsME() == 1) then
		mdkMenuItemEnable(item,0)
	end


    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_resolution)
    mdkMenuItemAddComboBox(item)
    n = chVideoGetResTotal()
    i = 0
    while (i < n) do
        local w = chVideoGetResWidth(i)
        local h = chVideoGetResHeight(i)
        mdkMenuItemAddComboString(item, format("%dx%d", w, h))
        i = i + 1
    end
    mdkMenuItemSetWidgitValue(item, chVideoGetResWidthHeightAsId(chGetVideoWidth(),chGetVideoHeight()))
    menu.optvideo.res = item
    item.Select = VidMenuItemChanged
    item.Inc = VidMenuItemChanged
    item.Dec = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_bitdepth)
    mdkMenuItemAddComboBox(item)
    mdkMenuItemAddComboString(item, "16")
    mdkMenuItemAddComboString(item, "32")
    mdkMenuItemSetWidgitValue(item, chVideoGetBppAsId())
	menu.optvideo.bitdepth = item
    item.Select = VidMenuItemChanged
    item.Inc = VidMenuItemChanged
    item.Dec = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_texquality)
    menu.optvideo.texmax = chVideoGetTextureMax()
    mdkMenuItemAddSlider(item, 1/(menu.optvideo.texmax-1), 1)
	texquality = (chVideoGetTexQuality() - 0.4) / (0.2 * (menu.optvideo.texmax - 1.0))
    mdkMenuItemSetWidgitValue(item, texquality)
    menu.optvideo.texquality = item
    item.Select = VidMenuItemChanged
    item.Inc = VidMenuItemChanged
    item.Dec = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_filtering)
    mdkMenuItemAddComboBox(item)
    mdkMenuItemAddComboStringW(item, strings.flt_none)
    mdkMenuItemAddComboStringW(item, strings.flt_bilinear)
    mdkMenuItemAddComboStringW(item, strings.flt_trilinear)
    mdkMenuItemSetWidgitValue(item, chVideoGetFilterMode())
	menu.optvideo.filter = item
    item.Select = VidMenuItemChanged
    item.Inc = VidMenuItemChanged
    item.Dec = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_shadow)
    mdkMenuItemAddCheckBox(item, 0)
	mdkMenuItemSetWidgitValue(item, omSceneGetShadows())
	menu.optvideo.shadows = item
    item.Select = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_tandl)
    mdkMenuItemAddCheckBox(item, 0)
	mdkMenuItemSetWidgitValue(item, chGetVideoUseTandL())
	menu.optvideo.tandl = item
    item.Select = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_fullscreen)
    mdkMenuItemAddCheckBox(item, 0)
	mdkMenuItemSetWidgitValue(item, chVideoGetFullScreen())
	menu.optvideo.fullscreen = item
    item.Select = VidMenuItemChanged

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_apply)
    item.Select = function(item)
        local t = menu.optvideo
        local w,h,driver
		driver = mdkMenuItemGetWidgitValue(t.driver)
        w = chVideoGetResWidth(mdkMenuItemGetWidgitValue(t.res))
        h = chVideoGetResHeight(mdkMenuItemGetWidgitValue(t.res))
		bit = ((mdkMenuItemGetWidgitValue(t.bitdepth))+1)*16
		z = 16
		full = mdkMenuItemGetWidgitValue(t.fullscreen)
		filter = mdkMenuItemGetWidgitValue(t.filter)
		texquality = 0.4 + 0.2 * mdkMenuItemGetWidgitValue(t.texquality)*(t.texmax-1)

        --print(format("%d %d %d", w, h, driver))
        chSetVideoMode(w, h, bit, z, chGetVideoRefresh(), full, filter, texquality, driver, chGetVideoMipMap(), chGetVideoUseDD(), mdkMenuItemGetWidgitValue(t.tandl))
        omSceneSetShadows(mdkMenuItemGetWidgitValue(t.shadows))
        mdkSaveOptions()
        chSndSwitchMusic(0)

        mdkSetMenuItemTextW(menu.optvideo.cancelitem, strings.opt_ok)
    end
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemEnable(item, 0)
    menu.optvideo.applyitem = item

    item = mdkCreateMenuItemW(menu.optvideo, strings.opt_cancel)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.03)
    item.Select = function()
        menu.optvideo.Cancel()
    end
    menu.optvideo.cancelitem = item
end

menu.optvideo.Cancel = function()
    menu.SwitchDlg("options", 0)
    mdkShowMouse(1,0)
end


---------------------------------------------------------------------
-- Audio
---------------------------------------------------------------------

menu.optaudio = mdkCreateMenu(0.55, 0.3, 0.06, 0.06, 0.01, JUSTIFY_RIGHT, 1, strings.opt_audio)

function BuildAudioMenu()
    mdkClearMenu(menu.optaudio)

    item = mdkCreateMenuItemW(menu.optaudio, strings.opt_effectsvol)
    mdkMenuItemAddSlider(item, 0.1, chSndGetEffectsVolume())
    item.Inc = function(item, n)
        chSndSetEffectsVolume(n)
    end
    item.Dec = item.Inc

    item = mdkCreateMenuItemW(menu.optaudio, strings.opt_musicvol)
    mdkMenuItemAddSlider(item, 0.1, chSndGetCdVolume())
    item.Inc = function(item, n)
        chSndMusicVolume(n)
    end
    item.Dec = item.Inc

    item = mdkCreateMenuItemW(menu.optaudio, strings.opt_voicevol)
    mdkMenuItemAddSlider(item, 0.1, chSndGetVoiceVolume())
    item.Inc = function(item, n)
        chSndSetVoiceVolume(n)
    end
    item.Dec = item.Inc

    prev = chSndGetEnvEnabled()
    item = mdkCreateMenuItemW(menu.optaudio, strings.opt_eax)
    mdkMenuItemAddCheckBox(item, prev)
    item.Select = function(self, n)
        if (prev == 1) then
            chSndSetEnvEnabled(0)
        else
            chSndSetEnvEnabled(1)
        end
    end
    mdkMenuItemEnable(item, chSndGetEAXIsSupported())

    item = mdkCreateMenuItemW(menu.optaudio, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.03)
    item.Select = function()
		menu.optaudio.Cancel()
    end
end

menu.optaudio.Cancel = function()
    mdkSaveOptions()
    menu.SwitchDlg("options", 0)
    mdkShowMouse(1,0)
end


---------------------------------------------------------------------
-- Controls
---------------------------------------------------------------------

menu.optcontrols = mdkCreateMenu(0.66, 0.25, 0.06, 0.06, 0.01, JUSTIFY_RIGHT, 1, strings.opt_controls)

function BuildControlsMenu()
    mdkClearMenu(menu.optcontrols)

    item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_revupdown)
    mdkMenuItemAddCheckBox(item, mdkGetReverseUpDown())
    item.Select = function(self, n)
        mdkReverseUpDown(n)
    end

    item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_revsniper)
    mdkMenuItemAddCheckBox(item, mdkGetSniperReverseUpDown())
    item.Select = function(self, n)
        mdkReverseSniperUpDown(n)
    end

    item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_turnsens)
    mdkMenuItemAddSlider(item, 0.05, mdkGetTurnSensitivity())
    item.Inc = function(item, n)
        mdkSetTurnSensitivity(n)
    end
    item.Dec = item.Inc

    item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_looksens)
    mdkMenuItemAddSlider(item, 0.05, mdkGetLookSensitivity())
    item.Inc = function(item, n)
        mdkSetLookSensitivity(n)
    end
    item.Dec = item.Inc

    if (chGetNumJoysticks() > 0) then
        item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_joyconfig)
        item.Select = function()
            BuildJoyConfigMenu()
            menu.SwitchDlg("joyconfig")
            mdkMenuSetHighlighted(menu.joyconfig, chGetCurrentJoystick()+1)
        end
    end
           
    item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_keys)
    mdkMenuItemStayInCenterPlease(item)
    item.Select = function(self)
        BuildKeysMenu()
        menu.SwitchDlg("keys")
        mdkShowMouse(1,0)
    end

    item = mdkCreateMenuItemW(menu.optcontrols, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.03)
    item.Select = function()
        menu.optcontrols.Cancel()
    end

end

menu.optcontrols.Cancel = function()
    mdkSaveOptions()
    menu.SwitchDlg("options", 0)
    mdkShowMouse(1,0)
end


---------------------------------------------------------------------
-- Keys
---------------------------------------------------------------------

menu.keys = mdkCreateMenu(0.5, 0.118, 0.040, 0.040, 0.0, JUSTIFY_RIGHT, 1, strings.opt_keys)
menu.kkeys = mdkCreateMenu(0.5, 0.25, 0.040, 0.040, 0.0, JUSTIFY_RIGHT, 1, strings.opt_kurtkeys)
menu.mkeys = mdkCreateMenu(0.5, 0.25, 0.040, 0.040, 0.0, JUSTIFY_RIGHT, 1, strings.opt_maxkeys)
menu.dkeys = mdkCreateMenu(0.5, 0.25, 0.040, 0.040, 0.0, JUSTIFY_RIGHT, 1, strings.opt_dockeys)

keynames = {
    { strings.key_forward,      { COM_FORWARD, COM_MINIUP } },
    { strings.key_backward,     { COM_BACKWARD, COM_MINIDOWN } },
    { strings.key_left,         { COM_TURNLEFT, COM_MINILEFT } },
    { strings.key_right,        { COM_TURNRIGHT, COM_MINIRIGHT } },
    { strings.key_strafeleft,   { COM_STRAFELEFT,COM_SMSTRAFELEFT, COM_MINILEFT2 } },
    { strings.key_straferight,  { COM_STRAFERIGHT,COM_SMSTRAFERIGHT, COM_MINIRIGHT2 } },
    { strings.key_lookup,       { COM_LOOKUP, COM_MINIUP2 } },
    { strings.key_lookdown,     { COM_LOOKDOWN, COM_MINIDOWN2 } },
    { strings.key_jump,         { COM_JUMP,COM_CHUTE } },
    { strings.key_shoot,        { COM_SHOOT,COM_MELEE,COM_SMSHOOT,COM_INVCOMBINE,COM_INVACTIVATE } },
    { strings.key_quicksave,    { COM_SAVEINSTANT } },
    { strings.key_quickload,    { COM_LOADINSTANT } },
}

kkeynames = {
    { strings.key_sniper,       { COM_SMTOGGLE } },
    { strings.key_zoomin,       { COM_SMZOOMIN } },
    { strings.key_zoomout,      { COM_SMZOOMOUT } },
    { strings.key_invleft,      { COM_INVNEXT } },
    { strings.key_invright,     { COM_INVPREV } },
    { strings.key_invuse,       { COM_INVENABLE } },
}

mkeynames = {
    { strings.key_invleft,      { COM_MAXINVNEXT } },
    { strings.key_invright,     { COM_MAXINVPREV } },
    { strings.key_invequip,     { COM_MAXEQUIP } },
    { strings.key_invunequip,   { COM_MAXUNEQUIP } },
    { strings.key_jet,          { COM_JET } },
}

dkeynames = {
    { strings.key_docinvup,     { COM_DOCINVUP } },
    { strings.key_docinvdown,   { COM_DOCINVDOWN } },
    { strings.key_docinvleft,   { COM_DOCINVENLEFT } },
    { strings.key_docinvright,  { COM_DOCINVENRIGHT } },
}

function AddKeyItems(kmenu, keyt)
    local i, item
    kmenu.items = {}
    i = 1
    while (keyt[i]) do
        item = mdkCreateMenuItemW(kmenu, keyt[i][1])
        item.com = keyt[i][2]
        item.Clear = KeyMenuItemCleared
        item.Select = KeyMenuItemSelected
        AddKeyItemTextBox(item)
        item.i = i
        kmenu.items[i] = item
        i = i + 1
    end
end

function BuildKeysMenu()
    local i, item

    mdkClearMenu(menu.keys)
    mdkClearMenu(menu.kkeys)
    mdkClearMenu(menu.mkeys)
    mdkClearMenu(menu.dkeys)

    AddKeyItems(menu.keys, keynames)
    AddKeyItems(menu.kkeys, kkeynames)
    AddKeyItems(menu.mkeys, mkeynames)
    AddKeyItems(menu.dkeys, dkeynames)

    item = mdkCreateMenuItemW(menu.keys, strings.opt_kurtkeys)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        menu.SwitchDlg("kkeys", 0)
    end

    item = mdkCreateMenuItemW(menu.keys, strings.opt_maxkeys)
    mdkMenuItemStayInCenterPlease(item)
    item.Select = function()
        menu.SwitchDlg("mkeys", 0)
    end

    item = mdkCreateMenuItemW(menu.keys, strings.opt_dockeys)
    mdkMenuItemStayInCenterPlease(item)
    item.Select = function()
        menu.SwitchDlg("dkeys", 0)
    end

    item = mdkCreateMenuItemW(menu.keys, strings.opt_defkeys1)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        dofile('defaultkeys')
        BuildKeysMenu()
    end

    item = mdkCreateMenuItemW(menu.keys, strings.opt_defkeys2)
    mdkMenuItemStayInCenterPlease(item)
    item.Select = function()
        dofile('defaultkeys2')
        BuildKeysMenu()
    end

    item = mdkCreateMenuItemW(menu.keys, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        menu.keys.Cancel()
    end

    item = mdkCreateMenuItemW(menu.kkeys, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        menu.kkeys.Cancel()
    end

    item = mdkCreateMenuItemW(menu.mkeys, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        menu.mkeys.Cancel()
    end

    item = mdkCreateMenuItemW(menu.dkeys, strings.opt_ok)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        menu.dkeys.Cancel()
    end

end

function KeyMenuItemSelected(self)
    menu.keysbox = mdkMessageBoxCreate( 0.2, 0.5, 0.6, 0.05, strings.opt_presskey, 1 )
    conscheme = "poo"
    menu.keyspoll = 1
    menu.keyspollitem = self
    menu.keytimeout = 5
    mdkShowMouse(0,0)
end

function KeyMenuItemCleared(self)

    BindCommands(self.com, COM_KEY_FORCEUNBOUND, COM_KEY_FORCEUNBOUND)
    AddKeyItemTextBox(self)

end

function AddKeyItemTextBox(item)

    local key1 = omGetCommandBinding(item.com[1])
    local key2 = omGetCommandBinding2(item.com[1])

    if (key1 >= 0) and (key2 >= 0) then
        mdkMenuItemAddTextBox(item, "- "..omGetCommandKeyName(item.com[1])..", "..omGetCommandKeyName2(item.com[1]), 0.2)
    elseif (key1 >= 0) then
        mdkMenuItemAddTextBox(item, "- "..omGetCommandKeyName(item.com[1]), 0.2)
    elseif (key2 >= 0) then
        mdkMenuItemAddTextBox(item, "- "..omGetCommandKeyName2(item.com[1]), 0.2)
    else
        mdkMenuItemAddTextBox(item, "", 0.2)
    end

end

function BindCommands(comlist, key1, key2)

    local n,v = next(comlist, nil)
    while (n) do
        omBindCommandI(v, key1)
        omBindCommandI2(v, key2)
        n,v = next(comlist, n)
    end
   
end

function BindKeyItem(item, newkey)

    local key1 = omGetCommandBinding(item.com[1])
    local key2 = omGetCommandBinding2(item.com[1])

    if (newkey ~= key1) and (newkey ~= key2) then

        if (key1 >= 0) and (key2 >= 0) then
            key1 = key2
            key2 = newkey
        elseif (key1 >= 0) then
            key2 = newkey
        elseif (key2 >= 0) then
            key1 = key2
            key2 = newkey
        else
            key1 = newkey
        end

        BindCommands(item.com, key1, key2)
        
    end

end

function KeysUpdate()
    local n,v,k,i,item
    if (menu.keyspoll) then
        menu.keytimeout = menu.keytimeout - chGetDeltaT()
        if (menu.keytimeout <= 0) then
            k = 1
            omEnableControl()
        else
            k = omGetKeyPress()
        end
        if (k ~= -1) then
            if (k ~= 1) then
                item = menu.keyspollitem
                menu.keyspollitem = nil
                BindKeyItem(item, k)
                AddKeyItemTextBox(item)
            end
            mdkMessageBoxDestroy(menu.keysbox)
            mdkShowMouse(1,0)
            conscheme = "menu"
            menu.keyspoll = nil
        end
    end
end

menu.keys.Update = KeysUpdate
menu.kkeys.Update = KeysUpdate
menu.mkeys.Update = KeysUpdate
menu.dkeys.Update = KeysUpdate

menu.keys.Cancel = function()
    mdkSaveControlSettings()
    menu.SwitchDlg("optcontrols", 0)
    mdkShowMouse(1,0)
end

menu.kkeys.Cancel = function()
    menu.SwitchDlg("keys", 0)
end

menu.mkeys.Cancel = function()
    menu.SwitchDlg("keys", 0)
end

menu.dkeys.Cancel = function()
    menu.SwitchDlg("keys", 0)
end


---------------------------------------------------------------------
-- Joystick Config
---------------------------------------------------------------------

menu.joyconfig = mdkCreateMenu(0.5, 0.35, 0.04, 0.04, 0.01, JUSTIFY_CENTER, 1, strings.opt_joyconfig)
                               
menu.joyconfig.Cancel = function()
    menu.SwitchDlg("optcontrols", 0)
end

menu.joyconfig.Select = function(self)
    chSelectJoystick(self.i)
    menu.joyconfig.Cancel()
end

function BuildJoyConfigMenu()

    local item
    mdkClearMenu(menu.joyconfig)

    item = mdkCreateMenuItemW(menu.joyconfig, strings.opt_disablejoy)
    item.Select = function(self)
        chSelectJoystick(-1)
        menu.joyconfig.Cancel()
    end

    local i = 0
    local njoy = chGetNumJoysticks()
    while (i < njoy) do
        local name = AbbreviateString(chGetJoystickName(i), 50)
        item = mdkCreateMenuItem(menu.joyconfig, name)
        item.Select = menu.joyconfig.Select
        item.i = i
        i = i + 1
    end

    item = mdkCreateMenuItemW(menu.joyconfig, strings.opt_cancel)
    mdkMenuItemStayInCenterPlease(item)
    mdkMenuItemSetSponge(item, 0.02)
    item.Select = function()
        menu.joyconfig.Cancel()
    end

end
