local mod = simpleMinimap:NewModule("skins")
local L = AceLibrary("AceLocale-2.2"):new("simpleMinimap_skins")
L:RegisterTranslations("enUS", function() return({
	enabled = true,
	enabled_desc = "enable / disable skin module",
	border = true,
	border_desc = "minimap border texture",
	skin = true,
	skin_desc = "minimap skin and shape",
	skins = true,
	skins_desc = "minimap skin and shape module",
	skinNames = {
		"round (default)",
		"square",
		"thin square (dark)",
		"thin square (light)",
		"top right corner",
		"top left corner",
		"bottom left corner",
		"bottom right corner"
	}
}) end)

L:RegisterTranslations("zhCN", function() return({ 
	enabled = "启用", 
	enabled_desc = "启用/禁用 皮肤单元", 
	border = "边框", 
	border_desc = "迷你地图的边框材质", 
	skin = "皮肤", 
	skin_desc = "迷你地图的皮肤与外形", 
	skins = "皮肤集合", 
	skins_desc = "迷你地图的皮肤与外形单元", 
	skinNames = { 
		"圆形 (默认)", 
		"方形", 
	    "窄邊方形 (暗色)",
		"厚邊方形 (亮色)", 
		"方形圆角-圆弧在左下方", 
		"方形圆角-圆弧在右下方", 
		"方形圆角-圆弧在右上方", 
		"方形圆角-圆弧在左上方", 
	} 
}) end)

L:RegisterTranslations("zhTW", function() return({ 
    enabled = "啟用", 
    enabled_desc = "啟用/關閉 外觀模組", 
    border = "邊框", 
    border_desc = "迷你地圖的邊框材質", 
	skin = "皮膚", 
	skin_desc = "迷你地圖的皮膚與外形", 
	skins = "皮膚集合", 
	skins_desc = "迷你地圖的皮膚與外形單元", 
    skinNames = { 
        "圓形 (默認)", 
        "方形", 
	    "窄邊方形 (暗色)",
		"厚邊方形 (亮色)",
        "方形圓角-圓弧在左下方", 
        "方形圓角-圓弧在右下方", 
        "方形圓角-圓弧在右上方", 
        "方形圓角-圓弧在左上方", 
    } 
}) end)


--
L:RegisterTranslations("koKR", function() return({
	enabled = "켬",
	enabled_desc = "스킨 모듈 켜기 / 끄기",
	border = "테두리",
	border_desc = "미니맵 테두리 텍스쳐",
	skin = "스킨",
	skin_desc = "미니맵 스킨과 외형",
	skins = "스킨들",
	skins_desc = "미니맵 스킨과 외형 모듈",
	skinNames = {
		"둥근형 (기본값)",
		"사각형",
		"가는 사각형 (어두운)",
		"가는 사각형 (밝은)",
		"우측 상단 모서리",
		"좌측 상단 모서리",
		"좌측 하단 모서리",
		"우측 하단 모서리"
	}
}) end)

L:RegisterTranslations("deDE", function() return({
	enabled = "aktiviert",
	enabled_desc = "aktiviert / deaktiviert das skin modul",
	border = "rand",
	border_desc = "minimap randtexture",
	skin = "skin",
	skin_desc = "minimap skin und form",
	skins = "skins",
	skins_desc = "minimap skin und form module",
	skinNames = {
		"rund (vorgabe)",
		"Quadrat",
		"dünnes quadrat (dunkel)",
		"dünnes quadrat (hell)",
		"ecke oben rechts",
		"ecke oben links",
		"ecke unten links",
		"ecke unten rechts"
	}
}) end)

L:RegisterTranslations("esES", function() return({
	enabled = "Activar",
		enabled_desc = "Activa / Desactiva el m\195\179dulo de skin",
	border = "Borde",
		border_desc = "Textura del borde del minimapa",
	skin = "Skin",
		skin_desc = "Skin y forma del minimapa",
	skins = "Skins",
		skins_desc = "M\195\179dulo de skin y forma del minimapa",
	skinNames = {
		"circular (por defecto)",
		"cuadrado",
		"cuadrado fino (oscuro)",
		"cuadrado fino (claro)",
		"esquina superior derecha",
		"esquina superior izquierda",
		"esquina inferior izquierda",
		"esquina inferior derecha"
	}
}) end)
--
function mod:OnInitialize()
	self.db = simpleMinimap:AcquireDBNamespace("skins")
	self.skins = {
		{
			shape="ROUND",
			texture="Interface\\Minimap\\UI-Minimap-Border",
			mask="Textures\\MinimapMask"
		},{
			shape="SQUARE",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\SquareMinimap",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\smmSquareMask"
		},{
			shape="SQUARE",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\smmSkin",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\smmSquareMask"
		},{
			shape="SQUARE",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\thinSquare2",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\smmSquareMask"
		},{
			shape="CORNER-BOTTOMLEFT",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\dLxTopRight",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\dLxTopRightMask"
		},{
			shape="CORNER-BOTTOMRIGHT",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\dLxTopLeft",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\dLxTopLeftMask"
		},{
			shape="CORNER-TOPRIGHT",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\dLxBottomLeft",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\dLxBottomLeftMask"
		},{
			shape="CORNER-TOPLEFT",
			texture="Interface\\AddOns\\simpleMinimap\\skins\\dLxBottomRight",
			mask="Interface\\AddOns\\simpleMinimap\\skins\\dLxBottomRightMask"
		}
	}
	self.defaults = { enabled=true, skin=1, border=true }
	self.options = {
		type="group", order=80, name=L.skins, desc=L.skins_desc,
		args={
			title={
				type="header", order=1, name="simpleMinimap |cFFFFFFCC"..L.skins
			},
			spacer1={
				type="header", order=2
			},
			enabled={
				type="toggle", order=3, name=L.enabled, desc=L.enabled_desc,
				get=function() return(self.db.profile.enabled) end,
				set=function(x) self.db.profile.enabled=x simpleMinimap:ToggleModuleActive(self, x) end
			},
			spacer2={
				type="header", order=4, name="---"
			},
			border={
				type="toggle", order=10, name=L.border, desc=L.border_desc,
				get=function() return(self.db.profile.border) end,
				set=function(x) self.db.profile.border=x self:OnEnable() end
			},
			skin={
				type="group", order=10, name=L.skin, desc=L.skin_desc,
				args={}
			}
		}
	}
	for n, _ in pairs(self.skins) do
		self.options.args.skin.args[tostring(n)] = {
			type="toggle", order=n, name=n.." :: "..L.skinNames[n], desc=L.skinNames[n],
			get=function() return(self.db.profile.skin==n) end,
			set=function() self.db.profile.skin=n simpleMinimap:UpdateScreen() end
		}
	end
	simpleMinimap.options.args.skins = self.options
	simpleMinimap:RegisterDefaults("skins", "profile", self.defaults)
end
--
function mod:OnEnable()
	if(self.db.profile.enabled) then
		simpleMinimap.GetButtonPos = function(simpleMinimap, vector)
			local r = true
			local v = math.rad(vector)
			local x, y = math.cos(v), math.sin(v)
			local o = 80 + simpleMinimap.db.profile.radius
			if(simpleMinimap:IsModuleActive('skins')) then
				local s = self:GetShape()
				if(s and ((s=="SQUARE") or
						(s=="CORNER-TOPRIGHT" and (x<0 or y<0)) or
						(s=="CORNER-TOPLEFT" and (x>0 or y<0)) or
						(s=="CORNER-BOTTOMRIGHT" and (x<0 or y>0)) or
						(s=="CORNER-BOTTOMLEFT" and (x>0 or y>0)))) then
					r = false
				end
			end
			if(r) then
				return x * o, y * o
			else
				x = math.max(o * -1, math.min(110 * x, o))
				y = math.max(o * -1, math.min(110 * y, o))
				return x, y
			end
		end
		simpleMinimap:UpdateScreen()
		self.loaded = true
	else
		simpleMinimap:ToggleModuleActive(self, false)
	end
end
--
function mod:OnDisable()
	if(self.loaded) then
		simpleMinimap:UpdateScreen()
		self.loaded = nil
	end
end
--
function mod:UpdateScreen()
	if(self.db.profile.enabled) then
		if(self.db.profile.border) then
			MinimapBorder:SetTexture(self.skins[self.db.profile.skin].texture)
		else
			MinimapBorder:SetTexture(nil)
		end
		Minimap:SetMaskTexture(self.skins[self.db.profile.skin].mask)
	else
		MinimapBorder:SetTexture("Interface\\Minimap\\UI-Minimap-Border")
		Minimap:SetMaskTexture("Textures\\MinimapMask")
		Minimap.smmSkinned = nil
	end	
end
--
function mod:GetShape()
	if(self and self.db.profile.enabled) then
		return self.skins[self.db.profile.skin].shape
	end
end
-- used for Fubar and Gatherer
function GetMinimapShape() return mod:GetShape() end
