
--Lua scripting

--[=[
for this example, let's consider the name of your addon is "Slice And Dance"
addon folder name on World of Warcraft/_retail_/Interface/AddOns/SliceAndDance/
toc file example /World of Warcraft/_retail_/Interface/AddOns/SliceAndDance/SliceAndDance.toc:
    ## Interface: 120100
    ## Title: "Slice And Dance"
    ## Notes: Show a slice and dice bar for rogues
    ## SavedVariables: SliceAndDanceDatabase
    SliceAndDance_Core.lua
--]=]

--create a new file on your addon folder called "SliceAndDance_Core.lua"
--this are the contents to add to the file

--each lua file of the addon receives a payload with the addon name and a private table
--this private table is shared between all files of your addon and cannot be accessed by other addons or scripts
--the addon name is the ToC name of your addon, the ToC is also the folder name of your addon in the addons folder
local addonName, privateTable = ...

--saved variables name
--this is the name of the global table where your addon will store it's saved variables
--you define this on the addon.toc file under the line "## SavedVariables: SliceAndDanceDatabase"
local savedVariablesName = "SliceAndDanceDatabase"

--default settings
--a simple table with default settings for your addon, any value modified by the user will be stored in the saved variables table
--if your addon doesn't have any settings, you can just pass an empty table
local defaultSettingsExample = {
    width = 500,
    height = 500,
    name = "John",
}

local detailsFramework = DetailsFramework
--create the core addon object, this is a table which will all public functions of your addon
local myNewAddonObject = detailsFramework:CreateNewAddOn(addonName, savedVariablesName, defaultSettingsExample)

--by default, the table generated by the CreateNewAddOn() isn't placed in the global environment
--if this is desired for some reason, you can do it manually, take in mind that this will make your addon object accessible by other addons and scripts
_G.MyNewAddon = myNewAddonObject

--you can also place the addon object in the private table, this way it can be accessed by all files of your addon but not by other addons or scripts
privateTable.MyNewAddon = myNewAddonObject

--called on event ADDON_LOADED, this function is called when the savedVariables of your addon is ready to be used
function myNewAddonObject.OnLoad(self, profile)
    --self is: myNewAddonObject
    --profile is a table with defaultSettingsExample
end

--called on event PLAYER_LOGIN, this function is called when the loading screen is gone and the player character is ready to play
function myNewAddonObject.OnInit(self, profile) --fired from detailsFramework at PLAYER_LOGIN
    --self is: myNewAddonObject
    --profile is a table with defaultSettingsExample
end

--accessing the profile
--profile is available after the event "ADDON_LOADED" and can be accessed at any part of the addon.
local addonProfile = myNewAddonObject.profile
--the table 'addonProfile' now contains a copy of 'defaultSettingsExample', any change to 'addonProfile' will be saved in the global saved variables table 'SliceAndDanceDatabase' and will persist between sessions.

--to access the addon object from another file of the addon, you have two choises:
--1 - add the addon object in the global environment _G, like the example above with _G.MyNewAddon = myNewAddonObject, and then access it from any file with local addon = _G.MyNewAddon
--2 - add to the private table, like the example above with privateTable.MyNewAddon = myNewAddonObject, and then access it from any file with local addon = privateTable.MyNewAddon

--real world example:
local detailsFramework = DetailsFramework
local addonName, privateTable = ...
local savedVariablesName = "SliceAndDanceDatabase"
local defaultSettingsExample = {
    width = 500,
    height = 500,
    name = "John",
}

local myNewAddonObject = detailsFramework:CreateNewAddOn(addonName, savedVariablesName, defaultSettingsExample)

--placing the addon object in the global environment, this is optional but can be useful for debugging and for other addons to access it
_G.SliceAndDanceAddOn = myNewAddonObject
--as the addon object is public now and for organization, place a 'public' key in the private table and store the addon object there, this way you can access it from any file of your addon with privateTable.public
privateTable.public = myNewAddonObject

function myNewAddonObject:OnLoad()
    local profile = self.profile
    print("Addon loaded, profile width is " .. profile.width)
end

function myNewAddonObject:OnInit()
    local profile = self.profile
    print("Addon initialized, player in ready to play")
end
