--[[

    MonkeySpeed:
    A simple speedometer.
    
    Author: Trentin

    Resurected by: Quel

--]]


-- define the dialog box for reseting config
StaticPopupDialogs["MONKEYSPEED_RESET"] = {
    text = TEXT(MONKEYSPEED_CONFIRM_RESET),
    button1 = TEXT(OKAY),
    button2 = TEXT(CANCEL),
    OnAccept = function()
        MonkeySpeed_ResetConfig();
    end,
    timeout = 0,
    exclusive = 1
};


-- Script array, not saved
MonkeySpeed = {};
MonkeySpeed.m_iDeltaTime = 0;

MonkeySpeed.m_fSpeed = 0.0;
MonkeySpeed.m_fSpeedDist = 0.0;
MonkeySpeed.m_bLoaded = false;
MonkeySpeed.m_bVariablesLoaded = false;
MonkeySpeed.m_strPlayer = "";
MonkeySpeed.m_vCurrPos = {};
MonkeySpeed.m_bCalibrate = false;


function MonkeySpeed_Init()
        
    -- double check that we didn't already load
    if ((MonkeySpeed.m_bLoaded == false) and (MonkeySpeed.m_bVariablesLoaded == true)) then
        
        -- add the realm to the "player's name" for the config settings
        MonkeySpeed.m_strPlayer = GetCVar("realmName").."|"..MonkeySpeed.m_strPlayer;
        
        if (not MonkeySpeedConfig) then
            MonkeySpeedConfig = { };
        end
        
        -- now we're ready to calculate speed
        MonkeySpeed.m_vLastPos = {};
        MonkeySpeed.m_vLastPos.x, MonkeySpeed.m_vLastPos.y = GetPlayerMapPosition("player");

        -- if there's not an entry for this
        if (not MonkeySpeedConfig[MonkeySpeed.m_strPlayer]) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer] = {};
        end
        
        -- set the defaults if the values weren't loaded by the SavedVariables.lua
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplay == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplay = true;
        end
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayPercent == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayPercent = true;
        end
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayBar == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayBar = true;
        end
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_fUpdateRate == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_fUpdateRate = 0.5;
        end
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDebugMode == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDebugMode = false;
        end
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bLocked == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bLocked = false;
        end
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_iFrameWidth == nil) then
            MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_iFrameWidth = 60; -- akpatch: reduced size 96 ==> 60
        end

        -- akpatch: поддержка скорости по глобальной карте мира
        if MonkeySpeedConfig.m_ZoneBaseline0 == nil then
            MonkeySpeedConfig.m_ZoneBaseline0 = {[0] = 0.00023577}
        end

        -- akpatch: поддержка скорости по карте континента Calimdor, заменяет установки старых версий
        if MonkeySpeedConfig.m_ZoneBaseline1 == nil or MonkeySpeedConfig.m_ZoneBaseline1[0] == nil then
            MonkeySpeedConfig.m_ZoneBaseline1 = {
                    [0] = 0.000285327663842813, -- по карте континента
                    [1] = 0.00182, -- Ashenvale
                    [2] = 0.00207, -- Azshara
                    [3] = 0.00160, -- Darkshore
                    [4] = 0.00992, -- Darnassus
                    [5] = 0.00234, -- Desolace
                    [6] = 0.00199, -- Durotar
                    [7] = 0.00200, -- Dustwallow Marsh
                    [8] = 0.00183, -- Felwood
                    [9] = 0.00151, -- Feralas
                    [10] = 0.00455, -- Moonglade
                    [11] = 0.00204, -- Mulgore
                    [12] = 0.00748, -- Orgrimmar
                    [13] = 0.003014,-- Silithus (до расширения зоны было 0.00151 - в два раза меньше)
                    [14] = 0.00215, -- Stonetalon Mtns
                    [15] = 0.00152, -- Tanaris
                    [16] = 0.00206, -- Teldrassil
                    [17] = 0.00104, -- The Barrens
                    [18] = 0.00239, -- Thousand Needles
                    [19] = 0.01006, -- Thunderbluff
                    [20] = 0.00284, -- Un'Goro Crater
                    [21] = 0.00148, -- Winterspring
            };
        end

        -- akpatch: поддержка скорости по карте континента Eastern Kingdoms, заменяет установки старых версий
        if MonkeySpeedConfig.m_ZoneBaseline2 == nil or MonkeySpeedConfig.m_ZoneBaseline2[0] == nil then
            MonkeySpeedConfig.m_ZoneBaseline2 = {
                    [0] = 0.0002983842120877793, -- по карте континента
                    [1] = 0.00375, -- Alterac Mtns
                    [2] = 0.00292, -- Arathi Highlands
                    [3] = 0.00420, -- Badlands
                    [4] = 0.00313, -- Blasted Lands
                    [5] = 0.00359, -- Burning Steppes
                    [6] = 0.00420, -- Deadwind Pass
                    [7] = 0.00213, -- Dun Morogh
                    [8] = 0.00389, -- Duskwood
                    [9] = 0.00271, -- Eastern Plaguelands
                    [10] = 0.00302, -- Elwynn Forest
                    [11] = 0.00328, -- Hillsbrad
                    [12] = 0.01327, -- Ironforge
                    [13] = 0.00381, -- Loch Modan
                    [14] = 0.00484, -- Redridge Mtns
                    [15] = 0.00471, -- Searing Gorge
                    [16] = 0.00250, -- Silverpine Forest
                    [17] = 0.00781, -- Stormwind
                    [18] = 0.00165, -- Stranglethorn Vale
                    [19] = 0.00458, -- Swamp of Sorrows
                    [20] = 0.00273, -- Hinterlands
                    [21] = 0.00232, -- Tirisfal Glades
                    [22] = 0.01094, -- Undercity
                    [23] = 0.00244, -- Western Plaguelands
                    [24] = 0.00300, -- Westfall
                    [25] = 0.00254 -- Wetlands
            };
        end
        
        if (MonkeySpeedConfig.m_SpecialZoneBaseline == nil) then
            MonkeySpeedConfig.m_SpecialZoneBaseline = {
                    [MONKEYSPEED_BLACKROCK] = 0.0002983199214410154,
                    [MONKEYSPEED_WARSONG] = 0.009159138767039199,
                    [MONKEYSPEED_ALTERAC] = 0.002477872662261515,
                    [MONKEYSPEED_ARATHI] = 0.005978692329518227
            };
        end
        
        -- show or hide the right options
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplay) then
            MonkeySpeedFrame:Show();
        else
            MonkeySpeedFrame:Hide();
        end
        
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayPercent) then
            MonkeySpeedText:Show();
        else
            MonkeySpeedText:Hide();
        end
        
        if (MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayBar) then
            MonkeySpeedBar:Show();
        else
            MonkeySpeedBar:Hide();
        end

        MonkeySpeedSlash_CmdSetWidth(MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_iFrameWidth);
        
        -- All variables are loaded now
        MonkeySpeed.m_bLoaded = true;
        
        -- print out a nice message letting the user know the addon loaded
        if (DEFAULT_CHAT_FRAME) then
            DEFAULT_CHAT_FRAME:AddMessage(MONKEYSPEED_LOADED);
        end
    end
end

function MonkeySpeed_ResetConfig()
    -- set the defaults if the values weren't loaded by the SavedVariables.lua
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplay = true;
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayPercent = true;
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDisplayBar = true;
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_fUpdateRate = 0.5;
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bDebugMode = false;
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_bLocked = false;
    MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_iFrameWidth = 60; -- akpatch: reduced size 96 ==> 60
    
    -- show or hide the right options
    -- update the frame
    MonkeySpeedFrame:ClearAllPoints();
    MonkeySpeedFrame:SetPoint("BOTTOMRIGHT", "UIParent", "BOTTOMRIGHT", -85, 0); -- akpatch: default coordinates
    MonkeySpeedFrame:Show();
    
    MonkeySpeedText:Show();
    MonkeySpeedBar:Show();

    MonkeySpeedSlash_CmdSetWidth(MonkeySpeedConfig[MonkeySpeed.m_strPlayer].m_iFrameWidth);
    
    -- check for MonkeyBuddy
    if (MonkeyBuddySpeedFrame_Refresh ~= nil) then
        MonkeyBuddySpeedFrame_Refresh();
    end
end
