RunestoneView = class("RunestoneView", ContainerView)

autoImport("RuneMainPage")
autoImport("RuneEquipPage")
autoImport("PackageSetQuickPage")
autoImport("PackageBarrowBagPage");
autoImport("BaseAttributeView")
autoImport("EquipStrengthen")
autoImport('RunePackagePart');

RunestoneView.ViewType = UIViewType.NormalLayer

RunestoneView.LeftViewState = {
	Default = "RunestoneView_LeftViewState_Default",
	Fashion = "RunestoneView_LeftViewState_Fashion",
	RoleInfo = "RunestoneView_LeftViewState_RoleInfo",
	BarrowBag = "RunestoneView_LeftViewState_BarrowBag",
}

function RunestoneView:GetShowHideMode()
	return PanelShowHideMode.MoveOutAndMoveIn
end

function RunestoneView:Init()
	self.normalStick = self:FindComponent("NormalStick", UISprite);

	self.mainPage = self:AddSubView("RuneMainPage",RuneMainPage);
	self.equipPage = self:AddSubView("RuneEquipPage",RuneEquipPage);
	self.equipStrengthenViewController = self:AddSubView("EquipStrengthen", EquipStrengthen);
	self.BarrowBagPage = self:AddSubView("PackageBarrowBagPage", PackageBarrowBagPage);

	self.shortCutIsSetting = false;

	self:InitUI();
	self:MapEvent();
end

function RunestoneView:OnEnter()
	RunestoneView.super.OnEnter(self);
	self:CameraRotateToMe();

	self:SetLeftViewState(RunestoneView.LeftViewState.Default);

	self:ActiveSetShortCut(false);

	local ctrl = self:GetRunePackageBord();
	ctrl:UpdateInfo();

	-- if(self.viewdata.view and self.viewdata.view.tab)then
	-- 	self:TabChangeHandler(self.viewdata.view.tab)
	-- end
end

function RunestoneView:InitUI()
	self.onFashionBtn = self:FindGO("OnFashionBtn");
	self.onInfoBtn = self:FindGO("OnInfoBtn");
	self.equipBord = self:FindGO("EquipBord");
	self.fashionBord = self:FindGO("FashionEquipBord");
	self.infoBord = self:FindGO("attrViewHolder");
	self.barrowBagBord = self:FindGO("BarrowBagHolder");
	self.topCoins = self:FindGO("TopCoins");

	--self.itemBord = self:FindGO("ItemNormalList");
	--揹包按鈕
	--self.BagBtn = self:FindGO("ButtonBag");

	self.viewState = RunestoneView.LeftViewState.Default;
	self:AddClickEvent(self.onFashionBtn, function (go)
		if(self.viewState == RunestoneView.LeftViewState.Fashion)then
			self:SetLeftViewState(RunestoneView.LeftViewState.Default);
		else
			self:SetLeftViewState(RunestoneView.LeftViewState.Fashion);
		end
	end);
	self:AddClickEvent(self.onInfoBtn, function (go)
		if(self.viewState == RunestoneView.LeftViewState.RoleInfo)then
			self:SetLeftViewState(RunestoneView.LeftViewState.Default);
		else
			self:SetLeftViewState(RunestoneView.LeftViewState.RoleInfo);
		end
	end);

	-- 符文揹包監聽
	-- self:AddClickEvent(self.BagBtn,function(go)
	-- 	local ctrl = self:GetRunePackageBord();
	-- 	ctrl:UpdateInfo();
	-- 	ctrl:Show();

	-- 	--local x,y,z = LuaGameObject.GetPosition(self.BagBtn.gameObject.transform);
	-- 	--ctrl:SetPos(x,y,z);
	-- 	--
	-- 	--ctrl:SetLocalOffset(-257.1, 214, 0);
	-- end)


	local ctrl = self:GetRunePackageBord();
	ctrl:UpdateInfo();
	ctrl:Show();

	--分解符文按鈕監聽
	self:InitDecompose()


	-- 監聽快捷欄事件
	self.quickUseTween = self:FindComponent("QuickUseTweenButton", UIPlayTween);
	self:AddButtonEvent("QuickUseTweenButton", function (go)
		self:ActiveSetShortCut(not self.shortCutIsSetting);
	end);

	self.goQuickUseTweenButton = self:FindGO("QuickUseTweenButton")
	local bagTab = self:FindGO("BagTab");
	--self:AddTabChangeEvent(bagTab,self.itemBord,PanelConfig.Bag);
	RedTipProxy.Instance:RegisterUI(SceneTip_pb.EREDSYS_PET_ADVENTURE, bagTab)

	local strengthTab = self:FindGO("StrengthTab");
	self:AddOrRemoveGuideId(strengthTab, 35)
	self:AddTabChangeEvent(strengthTab, self.goEquipStrengthen, PanelConfig.EquipStrengthen)
end

function RunestoneView:GetRunePackageBord()
	if(self.RunePackageBord == nil)then
		local frontPanel = self:FindGO("ThefrontPanel");
		self.RunePackageBord = RunePackagePart.new();
		self.RunePackageBord:CreateSelf(frontPanel.gameObject);
		-- self.RunePackageBord:Hide();
	end
	return self.RunePackageBord;
end
--分解符文
function RunestoneView:InitDecompose()
	local DecomposeButton = self:FindGO("DecomposeRune");
	--FunctionUnLockFunc.Me():RegisteEnterBtnByPanelID(PanelConfig.DeComposeView.id, DecomposeButton)
	self:AddClickEvent(DecomposeButton, function ()
		GameFacade.Instance:sendNotification(UIEvent.JumpPanel,{view = PanelConfig.RuneDeComposeView})
	end);
end

-- function RunestoneView:TabChangeHandler(key)
-- 	if(RunestoneView.super.TabChangeHandler(self, key))then
-- 		if(key==1)then
-- 			self.itemBord:SetActive(true);
-- 			self.goQuickUseTweenButton:SetActive(true)

-- 			self.equipPage:RemoveMaskOnItems()
-- 			self.equipStrengthenViewController:Hide()
-- 			self.equipStrengthenIsShow = false
-- 		elseif(key == 2)then
-- 			self.itemBord:SetActive(false);
-- 			self.equipPage:AddMaskOnItems()
-- 			self.goQuickUseTweenButton:SetActive(false)
-- 			self:ActiveSetShortCut(false);
-- 			self.equipStrengthenViewController:Show()
-- 			local equipCtrl = self.equipPage.chooseEquip
-- 			if equipCtrl then
-- 				local equipData = equipCtrl.data
-- 				if equipData then
-- 					local equipInfo = equipData.equipInfo
-- 					local isCouldStrengthen = self.equipStrengthenViewController:IsCouldStrengthen(equipData.index) and equipInfo:CanStrength()
-- 					if isCouldStrengthen then
-- 						if equipInfo.damage then
-- 							self.equipPage:SetChoose()
-- 							self.equipStrengthenViewController:SetEmpty()
-- 						else
-- 							self.equipStrengthenViewController:Refresh(equipCtrl.data.index)
-- 						end
-- 					else
-- 						self.equipPage:SetChoose()
-- 						self.equipStrengthenViewController:SetEmpty()
-- 					end
-- 				else
-- 					self.equipPage:SetChoose()
-- 					self.equipStrengthenViewController:SetEmpty()
-- 				end
-- 			else
-- 				self.equipPage:SetChoose()
-- 				self.equipStrengthenViewController:SetEmpty()
-- 			end
-- 			self.equipStrengthenIsShow = true
-- 		end
-- 	end
-- end

function RunestoneView:GetBaseAttriView()
	if(not self.baseAttributeView)then
		self.baseAttributeView = self:AddSubView("BaseAttributeView",BaseAttributeView)
		self.baseAttributeView:OnEnter();
		self.baseAttributeView:HideHelpBtn()
	end
	return self.baseAttributeView;
end

function RunestoneView:ActiveSetShortCut(active)
	if(self.shortCutIsSetting ~= active)then
		if(active and not self.packageSetQuickPage)then
			self.packageSetQuickPage = self:AddSubView("PackageSetQuickPage",PackageSetQuickPage)
			self.packageSetQuickPage:OnEnter();
		end

		self.quickUseTween:Play(active);
		self.mainPage:SetItemDragEnabled(active);
		self.equipPage:SetItemDragEnabled(active);

		self.shortCutIsSetting = active;
	end
end

function RunestoneView:SetLeftViewState(viewState)
	local onRotation, offRotation = Quaternion.Euler(0,180,0), Quaternion.identity;
	if(self.viewState~=viewState)then
		if(self.viewState == RunestoneView.LeftViewState.BarrowBag)then
			self.BarrowBagPage:Close();
		end

		self.barrowBagBord:SetActive(viewState == RunestoneView.LeftViewState.BarrowBag);

		local rotation1, rotation2 = offRotation, offRotation;
		if(viewState == RunestoneView.LeftViewState.Default)then

		elseif(viewState == RunestoneView.LeftViewState.Fashion)then
			rotation1 = onRotation;
		elseif(viewState == RunestoneView.LeftViewState.RoleInfo)then
			rotation2 = onRotation;
			self:GetBaseAttriView():showMySelf();
		elseif(viewState == RunestoneView.LeftViewState.Strength)then

		elseif(viewState == RunestoneView.LeftViewState.BarrowBag)then
			self.BarrowBagPage:Open();
		end

		self.onFashionBtn.transform.localRotation = rotation1;
		self.onInfoBtn.transform.localRotation = rotation2;

		self.equipBord:SetActive(viewState == RunestoneView.LeftViewState.Default);
		self.fashionBord:SetActive(viewState == RunestoneView.LeftViewState.Fashion);
		self.infoBord:SetActive(viewState == RunestoneView.LeftViewState.RoleInfo);

		self.topCoins:SetActive(viewState ~= RunestoneView.LeftViewState.RoleInfo
			and viewState ~= RunestoneView.LeftViewState.BarrowBag);

		self.viewState = viewState;
	end
end

local FashionEquipMap = {
	[6] = 5,
	[8] = 7,
}
local EquipFashionMap = {
	[5] = 6,
	[7] = 8,
}
function RunestoneView:GetDataFuncs(data, source)
	local result = {};
	if(data)then
		local type = isDress and 2 or 1;
		if(self.viewState == RunestoneView.LeftViewState.BarrowBag)then
			result = {37};
		else
			result = FunctionItemFunc.GetItemFuncIds(data.staticData.id, source, self.viewState == RunestoneView.LeftViewState.Fashion);
			-- 臨時處理
			-- if(data.equipInfo)then
			-- 	local equipType = data.equipInfo.equipData.EquipType;
			-- 	local site = GameConfig.EquipType[equipType].site[1];
			-- 	local canPutFashion = TableUtility.ArrayFindIndex(PackageEquip_FashionParts, site) ~= 0;

			-- 	-- if(canPutFashion)then
			-- 	-- 	if(self.viewState == RunestoneView.LeftViewState.Fashion)then
			-- 	-- 		for k,id in pairs(result) do
			-- 	-- 			if(FashionEquipMap[id])then
			-- 	-- 				result[k] = FashionEquipMap[id];
			-- 	-- 			end
			-- 	-- 		end
			-- 	-- 	else
			-- 	-- 		for k,id in pairs(result) do
			-- 	-- 			if(EquipFashionMap[id])then
			-- 	-- 				result[k] = EquipFashionMap[id];
			-- 	-- 			end
			-- 	-- 		end
			-- 	-- 	end
			-- 	-- end
			-- 	-- FunctionItemFunc:GetItemDefaultFunc(data, source, dest_isfashion)
			-- end
		end
	end
	return result;
end

function RunestoneView:GetItemDefaultFunc(data, source)
	source = source or FunctionItemFunc_Source.MainBag;
	return FunctionItemFunc.Me():GetItemDefaultFunc(data, source, self.viewState == RunestoneView.LeftViewState.Fashion);
end

function RunestoneView:MapEvent()
	self:AddListenEvt(ServiceEvent.PlayerMapChange, self.HandleMapChange);
	self:AddListenEvt(LoadSceneEvent.SceneAnimEnd, self.HandleSceneAnimEnd);
	self:AddListenEvt(PackageEvent.OpenBarrowBag, self.HandleOpenBarrowBag);
end

function RunestoneView:SetRefineEquip(equip)
	if(self.tabType ~= RunestoneView.TabType.Refine)then
		return;
	end

	-- self.refinePage:SetRefineEquip(equip);
end

function RunestoneView:IsRefinePage()
	return self.tabType == RunestoneView.TabType.Refine;
end

function RunestoneView:SetChooseEquip(cell)
	self.equipPage:SetChoose(cell)
end

function RunestoneView:GetChooseEquip()
	return self.equipPage.chooseEquip
end

function RunestoneView:HandleMapChange(note)
	if( Game.MapManager:Previewing() )then
		return;
	end
	if(note.type == LoadSceneEvent.FinishLoad and note.body)then
		self:CameraRotateToMe();
	end
end

function RunestoneView:HandleSceneAnimEnd(note)
	self:CameraRotateToMe();
end

function RunestoneView:HandleOpenBarrowBag(note)
	self:SetLeftViewState(RunestoneView.LeftViewState.BarrowBag);
end

function RunestoneView:OnExit()
	UIUtil.StopEightTypeMsg()
	ServiceItemProxy.Instance:CallBrowsePackage(SceneItem_pb.EPACKTYPE_RUNE);

	-- local ctrl = self:GetRunePackageBord();
	--.Me():RemoveEventListener(ItemEvent.RuneUpdate, ctrl.UpdateInfo, self)

	RunestoneView.super.OnExit(self);

	self:CameraReset();
end