#pragma once
#include "global.h"
#include "MyView.h"
#include "GameMsgbox.h"
#include "GameScript.h"
#include "MapSystem.h"
#include "Entity.h"
#include "EntityRes.h"
#include "GameUI.h"
class MiniScriptSystem;
//物品注册信息
struct ItemRegister
{
	std::wstring iconEntityName;//图标实体名
	std::wstring fName;//选中时调用的lua函数名
	std::wstring checkFName;//调查时调用的lua函数名
	bool stackable = false;//可叠数量物品
};
//物品数据
struct ItemData
{
	std::wstring regID;//物品注册信息
	std::weak_ptr<Entity> e;//实体指针
	int num = 1;//拥有数量
	bool locked = false;//该物品是否在锁定状态
};
//任务注册信息
struct MissionRegister
{
	std::wstring iconEntityName;//图标实体名
	std::wstring name;//任务标题
	std::wstring fName;//选中时调用的lua函数名
	std::wstring checkFName;//调查时调用的lua函数名
};
//任务数据
struct MissionData
{
	std::wstring regID;//任务注册信息
	std::weak_ptr<Entity> e;//实体指针
};
//物品栏系统
class ItemSystem
{
public:
	virtual ~ItemSystem();
	void Init(MyDX11Renderer* pRenderer, MyView* pView, MySDLInput* pSDLInput, GameMsgbox* pGameMsgBox,
		GameScript* pGameScript, MapSystem* pMapSystem, EntityRes* pEntityRes, GameUI* pGameUI, const void* game);
	void Reset();
	bool GetShow();
	void Step();
	void Draw(float deltaTime);
	void OnResetScale(int w, int h);
	void SetEnable(bool bEnable);
	bool IsPanelFree(GameScript* gameScript);
	bool IsItemSystemFree();//物品栏系统是否在空闲状态
	void RegisterItem(const wchar_t* regID, ItemRegister itemReg);
	int AddItem(const wchar_t* regID, int num);
	int FindItem(const wchar_t* regID);
	void LockItem(int itemIndex, bool bLock);//锁定一个物品，让其他玩家无法调查
	bool ItemLocked(int itemIndex);//查看一个物品是否被锁定
	std::vector<ItemData>* GetItemList();
	void RegisterMission(const wchar_t* regID, MissionRegister missionReg);
	void AddMission(const wchar_t* regID);
	void DeleteMission(const wchar_t* regID);
	void DeleteAllMission();
	void ReplaceMission(const wchar_t* regID_src, const wchar_t* regID_dst);
	std::list<MissionData>::iterator FindMission(const wchar_t* regID);
	std::list<MissionData>* GetMissionList();
	void Controll();
	void SetShowMiniItem(bool bShowMiniItem, const wchar_t* regID, int num);//显示迷你物品
	void LockAllPlayerJump();//防止误跳跃
	bool IsEnableMapSystem();//是否启用地图系统
	void ReloadLanguage();
private:
	MyDX11Renderer* pRenderer = nullptr;
	MyView* pView = nullptr;
	MySDLInput* pSDLInput = nullptr;
	GameMsgbox* pGameMsgBox = nullptr;
	GameScript* pGameScript = nullptr;
	MapSystem* pMapSystem = nullptr;
	EntityRes* pEntityRes = nullptr;
	GameUI* pGameUI = nullptr;
	float itemsys_x = 0, itemsys_y = 0;
	const int itemsys_page_count_w = 20;
	const int itemsys_page_count_h = 22;
	bool bShow = false;
	bool bShowMiniItem = false;//显示小物品窗
	int pageIndex = 0;
	bool bEnable = false;
	const int cursor_flash_interval = 16;
	int cursor_flash_interval_counter = 0;
	bool cursor_flash = false;
	bool enableMapSystem = false;
	void DrawFrame();//绘制物品栏框架
	void DrawAddItemPage();
	void DrawAddMissionPage();
	bool PageControl();
	void ForceEndScript(GameScript* gameScript, GameMsgbox* gameMsgBox);
	//物品栏列表
	int itemCursor_x_player1 = 0;//物品栏光标
	int itemCursor_y_player1 = 0;
	int itemCursor_x_selected_player1 = -1;//物品栏选中光标
	int itemCursor_y_selected_player1 = -1;
	int itemCursor_x_player2 = 0;//物品栏光标
	int itemCursor_y_player2 = 0;
	int itemCursor_x_selected_player2 = -1;//物品栏选中光标
	int itemCursor_y_selected_player2 = -1;
	int SkillCursorToItemCursorX(int playerIndex) const;
	int ItemCursorXToSkillCursor(int playerIndex) const;
	//技能栏光标
	int skillCursor_player1 = -1;//技能栏光标
	int skillCursor_selected_player1 = -1;//技能栏光标
	int skillCursor_player2 = -1;//技能栏光标
	int skillCursor_selected_player2 = -1;//技能栏光标
	int itemCursorMode_player1 = 0;//0控制物品栏 1控制技能栏
	int itemCursorMode_player2 = 0;//0控制物品栏 1控制技能栏
	std::map<std::wstring, ItemRegister> itemRegList;
	std::vector<ItemData> itemList;
	void DrawItem();
	void DrawAddCursor();//绘制光标
	void ItemMenuControl(GameScript* gameScript, GameMsgbox* gameMsgBox, std::weak_ptr<Entity> player,
		int* pCursorX, int* pCursorY, int* pCursorX_selected, int* pCursorY_selected, int* pSkillCursor, int* pSkillCursor_selected, int* pItemCursorMode);
	void ItemMenuUpdate(GameScript* gameScript, GameMsgbox* gameMsgBox, int cursorX, int cursorY, bool bCheck = false);
	void ItemMenuScript(GameScript* gameScript, GameMsgbox* gameMsgBox, int cursorX, int cursorY);
	void SkillMenuUpdate(GameScript* gameScript, GameMsgbox* gameMsgBox, PlayerUIData* ui, int skillCursor, bool bCheck = false);
	void SkillMenuScript(GameScript* gameScript, GameMsgbox* gameMsgBox, PlayerUIData* ui, int skillCursor);
	void ResetCursorPos();
	//更新翻页按键文本
	void DrawPanelFreeGraph();
	//任务列表
	int missionCursor_player1 = 0;//任务栏光标
	int missionCursor_player2 = 0;//任务栏光标
	TTF_Font* myfont12 = nullptr;
	std::unordered_map<wchar_t, MyFontData> fontMap_myfont12;
	std::unordered_map<std::wstring, MissionRegister> missionRegList;
	std::list<MissionData> missionList;
	void DrawMission();
	void MissionMenuControl(GameScript* gameScript, GameMsgbox* gameMsgBox, std::weak_ptr<Entity> player, int* pMissionCursor);
	void MissionMenuUpdate(GameScript* gameScript, GameMsgbox* gameMsgBox, int missionCursor, bool bCheck = false);
	void MissionMenuScript(GameScript* gameScript, GameMsgbox* gameMsgBox, int missionCursor);
	//绘制
	void DrawPanel_Player1(float offx, float offy);//绘制玩家1面板
	void DrawPanel_Player2(float offx, float offy);//绘制玩家2面板
	void DrawAddEntity(std::shared_ptr<Entity>& we, float offx, float offy);
	void DrawAddEntity2(std::shared_ptr<Entity>& we, float offx, float offy);
	void DrawAddNum(float srcX, float srcY, float dstX, float dstY, int num, bool order = false);
	MiniScriptSystem* childScriptSys_player1 = nullptr;
	MiniScriptSystem* childScriptSys_player2 = nullptr;
	//图标实体
	std::list<std::shared_ptr<Entity>> itemIconEntity;//物品实体
	void DrawAddEntity_Icon(std::shared_ptr<Entity>& we, float offx, float offy);//绘制图标实体
	void DrawAddEntity2_Icon(std::shared_ptr<Entity>& we, float offx, float offy);//绘制图标实体
	void DrawAddItemIconEntity();
	//迷你物品
	ItemData miniItem;
	std::list<std::shared_ptr<Entity>> miniItemEntity;//迷你物品实体
	void DrawMiniItem();
	//任务实体
	std::list<std::shared_ptr<Entity>> missionIconEntity;
	void DrawAddMissionIconEntity();
};

