local addonName,ns = ...
------------------
local ADDUIGUI = CreateFrame("Frame")
local category = Settings.RegisterCanvasLayoutCategory(ADDUIGUI, "|cff00BFFFAddUI|r")
Settings.RegisterAddOnCategory(category)

SlashCmdList["AddUIC"] = function()
	Settings.OpenToCategory(category:GetID())
end
SLASH_AddUIC1 = "/ad"
SLASH_AddUIC2 = "/addui"

local function newFont(offx, offy, createframe, anchora, anchroframe, anchorb, text, fontsize)
	local font = createframe:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	font:SetPoint(anchora, anchroframe, anchorb, offx, offy)
	font:SetText(text)
	font:SetFont("fonts\\ARHei.ttf", fontsize, "OUTLINE")	
	return font
end

local clickXOffset1,clickXOffset2,clickXOffset3 = 0,0,0
local function newCheckbox(clickX,text, tip, db)
	local x,y
	if clickX == 1 then
		x = 16
		y = clickXOffset1*-30-120
		clickXOffset1 = clickXOffset1 + 1
	elseif clickX == 2 then
		x = 226
		y = clickXOffset2*-30-120
		clickXOffset2 = clickXOffset2 + 1
	elseif clickX == 3 then
		x = 446
		y = clickXOffset3*-30-120
		clickXOffset3 = clickXOffset3 + 1
	end
	if text == "" then return end
	local check = CreateFrame("CheckButton", "ADDUICheck" .. text, ADDUIGUI, "InterfaceOptionsCheckButtonTemplate")
	check:SetPoint("TOPLEFT", ADDUIGUI, "TOPLEFT", x, y)
	check:SetChecked(AddUIDB[db])
	check.text:SetText(text)
	check:SetScript("OnEnter",function(self) 
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT") 
		GameTooltip:AddLine("|cffFFFFFF"..tip.."|r") 
		GameTooltip:Show() 
	end)
	check:SetScript("OnLeave", function(self)    
		GameTooltip:Hide()
	end)

	check:SetScript("OnClick", function ( ... )
		AddUIDB[db] = check:GetChecked()
	end)
	
	return check
end


ns.ADDUIEVENT("PLAYER_LOGIN", function()

	local clear = CreateFrame("Button", "AddUISaveButton", ADDUIGUI, "UIPanelButtonTemplate")
	clear:SetText("恢复默认并重载界面")
	clear:SetWidth(177)
	clear:SetHeight(24)
	clear:SetPoint("TOPLEFT", 450, 77)
	clear:SetScript("OnClick", function()
		AddUIDB = AddUIDefaultDB
		ReloadUI()
	end)
	local pcrl = CreateFrame("Button", "AddUIrl", ADDUIGUI, "UIPanelButtonTemplate")
	pcrl:SetText("重载")
	pcrl:SetWidth(92)
	pcrl:SetHeight(22)
	pcrl:SetPoint("BOTTOMRIGHT", -132, -31)
	pcrl:SetScript("OnClick", function()
		 ReloadUI()
	end)
	local fdx = CreateFrame("Button", "更新日志", ADDUIGUI, "UIPanelButtonTemplate")
	fdx:SetText("更新日志")
	fdx:SetWidth(100)
	fdx:SetHeight(22)
	fdx:SetPoint("BOTTOMLEFT", 277, -31)
	fdx:SetScript("OnClick", function()
		StaticPopup_Show("ADDUI_EDITBOX","复制下面链接,在网页中打开", nil, "https://docs.qq.com/doc/DZVBtYXFxeExicERC")
	end)
	local qqun = ADDUIGUI:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	qqun:SetPoint("BOTTOMLEFT", -20, -30)
	qqun:SetText("简繁:斗鱼323075       版本:|cff00FFFF"..ns.ADDUIBB)
	qqun:SetJustifyH("RIGHT")
	local ppc = ADDUIGUI:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	ppc:SetPoint("TOPRIGHT", -5, -5)
	ppc:SetText("|cff00FF7F/ad|r快速打开此界面")
	ppc:SetJustifyH("RIGHT")
	ppc:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')

	local AddUI = newFont(16, -15 , ADDUIGUI, "TOPLEFT", ADDUIGUI, "TOPLEFT", "AddUI", 30)
	local AddUI2 = newFont(0, 5 , ADDUIGUI, "BOTTOMLEFT", AddUI, "BOTTOMRIGHT", "源生界面增强", 15)
	--local cda = newFont(-20, 28 , ADDUIGUI, "TOPLEFT", ADDUIGUI, "TOPLEFT", "|cff00BFFF技能冷却结束语音提醒|r已分离单体,搜索|cff00BFFFCDAlert|r", 15)
	local somep = ADDUIGUI:CreateFontString(nil, "OVERLAY");
	somep:SetFontObject("GameFontHighlight");
	somep:SetPoint("TOPLEFT", ADDUIGUI, "TOPLEFT", 16, -50 );   
	somep:SetJustifyH("LEFT")
	somep:SetText("此界面只是一些功能的开关,并不是全部功能都在这\n/aa 重载界面  /ab 打开网格线  /chat 配置聊天框\n/sc 删除所有宏并恢复默认按键  \n/sd 配置esc-界面-里面的大部分东西");

	local unitc = newCheckbox(1,"头像模块","改变头像样式", "unitf")
	local unbtc = newCheckbox(1,"头像百分比","头像旁创建百分比文字", "unitbt")
	local ticoc = newCheckbox(1,"目标标记按钮","目标头像右边的几个标记按钮", "icon")
	local mmbbc = newCheckbox(1,"动作条模块","改变动作条样式", "mmb")
	local mmbac = newCheckbox(1,"旧版动作条动画","在10.15版本加了些花里胡哨的动画,把他干掉恢复以前的样子!", "mmba")
	local mnmbc = newCheckbox(1,"微型菜单模块","禁用动作条模块此项也会失效", "minimb")
	--quesc = newCheckbox(16, -300, "任务模块","略微改变任务框架样式", "quest")
	local jsnjc = newCheckbox(1,"角色面板耐久","C键面板装备栏下面的耐久度百分比", "syd")
	local smapc = newCheckbox(1,"小地图模块","方形小地图", "smap")
	local smicc = newCheckbox(1,"小地图图标渐隐","鼠标指向小地图显示图标,离开时渐隐", "smapicon")
	local chatc = newCheckbox(1,"聊天窗模块","聊天窗口样式,tab可以切换聊天频道", "chatm")
	local chabc = newCheckbox(1,"聊天频道按钮","提供一行可以切换频道的按钮", "chatb")
	local castc = newCheckbox(1,"施法条模块","改变施法条样式", "cast")
	local rcast = newCheckbox(1,"|cff00BFFF自定义施法条材质|r","禁用施法条模块此项也会失效", "SCastTexture")
	local chabc = newCheckbox(1,"")
	local chabc = newCheckbox(1,"")
	local dimic = newCheckbox(1,"|cff00BFFF右下角信息栏|r","|r|cff00FF00右下角显示延迟耐久", "dimi")
	
	
	local lfctc = newCheckbox(2,"滚动战斗记录","中下偏右滚动受到伤害治疗提示", "ct")
	local jbtec = newCheckbox(2,"法术警报倒数","触发buff的括号上显示剩余时间,需要esc动作条-显示冷却时间", "jbtext")
	local combc = newCheckbox(2,"进入战斗提示","屏幕中间进入脱离战斗提示", "comb")
	local mizuc = newCheckbox(2,"密语自动邀请","别人密你123或者.组.会自动邀请,支持战网密语", "zu")
	local mizuc = newCheckbox(2,"自我打断通报","打断成功显示一行表情提示", "stop")
	local zdmhc = newCheckbox(2,"自动卖灰","自动卖垃圾", "mh")
	local shifc = newCheckbox(2,"Shift焦点","按shift+鼠标指向点击设置焦点", "sf")
	local lfgkc = newCheckbox(2,"|cff00BFFFLFG增强|r","|r|cff00FF00预创建双击申请,自动邀请,自动进组|r\n!!!!!!如果你用集合石,此项自动失效!!!!!!", "lfgkg")
	local tbufc = newCheckbox(2,"目标头像所有buff","不勾选只会显示自己的", "debuff")
	local kssqc = newCheckbox(2,"快速拾取","增加拾取速度", "sq")
	local raidc = newCheckbox(2,"团队框架BUFF时间","团队框架buff小于1分钟时显示时间,微调debuff大小", "raidframebuff")
	
	
	local cvac = newCheckbox(3,"CVAR自动设置","自动配置一些CVAR,大部分已移动至|cff00FF00/sd|r命令", "cvar")
	--local dbmc = newCheckbox(3,"DBM","DBM自动配置", "dbm")
	local mrtc = newCheckbox(3,"MRT","MRT自动配置,原exrt,聊天框右上角计时器和战复,", "exrt")
	--local skdc = newCheckbox(3,"Skada/Details","自动定位和一些配置", "skada")
	local tinyc= newCheckbox(3,"TinyInspect自动设置","勾选TinyInspect的一些设置", "tiny")
	--local chapc= newCheckbox(3,"聊天窗口锁定","每次重载重置聊天框大小和位置", "chatp")
	local cbufc= newCheckbox(3,"灌注/大餐语音提醒","还有仲夏祝福什么的", "buffcall")
	local cicoc= newCheckbox(3,"获得标记语音提醒","被标记语音提醒是什么标记", "iconcall")

	--材质下拉菜单
	local RadioDropdown = CreateFrame("DropdownButton", nil, ADDUIGUI, "WowStyle1DropdownTemplate")
    RadioDropdown:SetPoint("TOPLEFT", 20, -510)
	RadioDropdown:SetDefaultText(PlateColorDB[db])
    RadioDropdown:SetWidth(170)
    local function IsSelected(value)
        return value == AddUIDB.CastTexture
    end
    local function SetSelected(value)
        AddUIDB.CastTexture = value
    end
    local function GeneratorFunction(dropdown, rootDescription)
		rootDescription:SetScrollMode(400);
        for text, option in pairs(ns.CastBarTextrue) do
            local radio = rootDescription:CreateRadio(text, IsSelected, SetSelected, text)
            radio:AddInitializer(function(button, description, menu)
                local bgTexture = button:AttachTexture()
				bgTexture:SetSize(170,18)
                bgTexture:SetPoint("LEFT",15,0);
				if string.match(option,"Interface") then
					bgTexture:SetTexture(option)
				else
					bgTexture:SetAtlas(option)
				end
            end)
        end
    end
    RadioDropdown:SetupMenu(GeneratorFunction)
	
	--鼠标跟随方式
	local tipdroptable = {{"鼠标提示:跟随",1},{"鼠标提示:不跟随",0},{"鼠标提示:非战斗跟随",2},{"鼠标提示:禁用",3}}
	local RadioDropdown = CreateFrame("DropdownButton", nil, ADDUIGUI, "WowStyle1DropdownTemplate")
	RadioDropdown:SetPoint("TOPLEFT", 230, -510)
	RadioDropdown:SetWidth(170)
	local function IsSelected(value)
		return value == AddUIDB.ftip
	end
	local function SetSelected(value)
		AddUIDB.ftip = value
	end
	MenuUtil.CreateRadioMenu(RadioDropdown,IsSelected, SetSelected,	unpack(tipdroptable))
	
	local function newSlider(SliderName, x, y, minValue, maxValue, curValue, valueStep, lowText, highText, upText, tipText, varformat)
	local pSlider = CreateFrame("Slider", "Slider"..SliderName , ADDUIGUI, "OptionsSliderTemplate" );
	pSlider:SetPoint("TOPLEFT", ADDUIGUI, "TOPLEFT", x, y);
	pSlider:SetMinMaxValues(minValue, maxValue);
	pSlider:SetValue(curValue);
	pSlider:SetValueStep(valueStep);
	pSlider:SetObeyStepOnDrag(true);
	pSlider.textLow = _G["Slider"..SliderName.."Low"]
	pSlider.textHigh = _G["Slider"..SliderName.."High"]
	pSlider.text = _G["Slider"..SliderName.."Text"]
	pSlider.textLow:SetText(lowText)
	pSlider.textHigh:SetText(highText)
	pSlider.text:SetText("|cffFFD700"..upText.." :  "..string.format(varformat,pSlider:GetValue()).."|r")

	pSlider:SetScript("OnValueChanged", function(pSlider,event,arg1) 
			pSlider.text:SetText("|cffFFD700"..upText.." :  "..string.format(varformat,pSlider:GetValue()).."|r")
	end)
	pSlider.tooltipText = tipText
	-- body
	return pSlider
	end
	
	ADDUIGUI.Spell = newSlider("Spell", 230, -580, 0, 400, 400, 1, " ", " ", "施法序列延迟", "施法序列延迟","%d")
	ADDUIGUI.Spell:SetValue(AddUIDB.SpellQ)
	ADDUIGUI.Spell:HookScript("OnValueChanged", function(self, value)
		AddUIDB.SpellQ = tonumber(string.format("%d",self:GetValue()))
		SetCVar("SpellQueueWindow", AddUIDB.SpellQ) 
	end)
	
	--鼠标提示写一些没开关的功能
	local TIPFrame = CreateFrame("Frame", nil, ADDUIGUI)
    TIPFrame:SetSize(115, 20)
    TIPFrame:SetPoint("TOPRIGHT", -8, -25)
	local TIPtext = TIPFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
	TIPtext:SetPoint("CENTER")
	TIPtext:SetText("鼠标放这里")
	TIPtext:SetFont("fonts\\ARHei.ttf", 14, "OUTLINE")
	local TIPBackground = TIPFrame:CreateTexture(nil, "BACKGROUND")
	TIPBackground:SetTexture(130937)
	TIPBackground:SetAllPoints(TIPFrame)
	TIPBackground:SetColorTexture(0, 0, 0, 0.5) -- 设置背景颜色为黑色，透明度为0.5
	TIPBackground:SetScript("OnEnter", function(self)
		GameTooltip:SetOwner(TIPBackground, "ANCHOR_RIGHT")
		for i,text in ipairs(ns.TIPTEXTS) do
			GameTooltip:AddLine(text) 
		end
		GameTooltip:Show()
	end)
	TIPBackground:SetScript("OnLeave", function(self)
		GameTooltip:Hide()
	end)

end)