--imports
local WIM = WIM;
local _G = _G;
local string = string;
local tostring = tostring;

--set namespace
setfenv(1, WIM);

db_defaults.shownTutorials = {};

local Tutorials = CreateModule("Tutorials", true);

local function varFriendly(var)
    var = string.gsub(var, "[^%w_]", "_");
    return var;
end

local theButton;

local _FlagTutorial = _G.FlagTutorial;
function _G.FlagTutorial(id)
    local tut = string.match(tostring(id), "^WIM(.+)");
    if(tut) then
        -- flag internally
        _G.TutorialFrame.wimTip = true;
        -- update text.
        if(tut ~= varFriendly(L["WIM Update Available!"])) then
            db.shownTutorials[tut] = true;
            _G.TutorialFrameCheckButton:Show();
            theButton:Hide();
        else
            _G.TutorialFrameCheckButton:Hide();
            theButton:Show();
        end
        _G.TutorialFrameCheckboxText:SetText(L["Display WIM tips"]);
    else
        -- this tutorial isn't WIM's call origional function
        _G.TutorialFrameCheckButton:Enable();
        _G.TutorialFrameCheckboxText:SetText(_G.ENABLE_TUTORIAL_TEXT);
        _G.TutorialFrame.wimTip = false;
        _FlagTutorial(id);
    end
end

local _TutorialFrame_OnHide = _G.TutorialFrame_OnHide;
function _G.TutorialFrame_OnHide(self)
    self = self or _G.TutorialFrame;
    if(self.wimTip) then
        -- handle own code here.
        _G.PlaySound("igMainMenuClose");
	if(not _G.TutorialFrameCheckButton:GetChecked()) then
            Tutorials:Disable();
        end
    else
        -- not wim's tip, operate as normal
        _TutorialFrame_OnHide(self);
    end
end


local function createChangelogButton()
    local button = _G.CreateFrame("Button", "WIM_TutorialButtonChangeLog", _G.TutorialFrame, "UIPanelButtonTemplate");
    local ok = _G.TutorialFrameOkayButton;
    button:SetPoint("BOTTOMLEFT", 7, 7);
    button:SetHeight(ok:GetHeight());
    button:SetNormalTexture("DialogButtonNormalTexture");
    button:SetPushedTexture("DialogButtonPushedTexture");
    button:SetHighlightTexture("DialogButtonHighlightTexture");
    button:SetDisabledTexture("DialogButtonDisabledTexture");
    _G.WIM_TutorialButtonChangeLogText:SetText(L["View Updates"]);
    button:SetNormalFontObject("GameFontNormalSmall");
    button:SetHighlightFontObject("GameFontNormalSmall");
    button:SetDisabledFontObject("GameFontNormalSmall");
    button:SetWidth(_G.WIM_TutorialButtonChangeLogText:GetStringWidth()+30)
    --button:Disable();
    button:SetScript("OnClick", ShowChangeLog);
    
    return button;
end


local function displayTutorial(title, tutorial)
    local var = varFriendly(title);
    if(not db.shownTutorials[var] or L["WIM Update Available!"] == title) then
        db.shownTutorials[var] = true;
        theButton = theButton or createChangelogButton();
        _G["TUTORIAL_TITLEWIM"..var] = title;
        _G["TUTORIALWIM"..var] = tutorial;
        _G.TutorialFrame_NewTutorial("WIM"..var);
    end
end

function Tutorials:OnEnable()
    DisplayTutorial = displayTutorial;
end

function Tutorials:OnDisable()
    -- to decrease table lookups, we can just define the function as such.
    DisplayTutorial = function() end;
end


local _TutorialFrame_NewTutorial = _G.TutorialFrame_NewTutorial;
_G.TutorialFrame_NewTutorial = function(id)
    _TutorialFrame_NewTutorial(id);
    local button = _G.LAST_TUTORIAL_BUTTON_SHOWN;
    if(button) then
        -- set OnHide to restore button's image.
        button:SetScript("OnHide", function(self)
                self:SetNormalTexture("Interface\\TutorialFrame\\TutorialFrameAlert");
                self:SetHighlightTexture("Interface\\TutorialFrame\\TutorialFrameAlert", "ADD");
            end);
        -- set buttons image to WIM tutorial.
        if(button.id and string.match(button.id, "^WIM")) then
            button:SetNormalTexture("Interface\\AddOns\\"..addonTocName.."\\Modules\\Textures\\tutorial");
            button:SetHighlightTexture("Interface\\AddOns\\"..addonTocName.."\\Modules\\Textures\\tutorial", "ADD");
        end
    end
end


--Global to add tutorial
function DisplayTutorial(title, tutorial)
    -- this function is defined when the module is enabled.
end

-- this is used to show tutorial even if Tutorials are disabled.
DisplayTutorialForce = displayTutorial; 