using System;  
 | 
using System.Collections;  
 | 
using System.Collections.Generic;  
 | 
using UnityEngine;  
 | 
namespace vnxbqy.UI  
 | 
{  
 | 
      
 | 
    public class HazyGrassModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk  
 | 
    {  
 | 
        Dictionary<int, int> m_MapNpcCount = new Dictionary<int, int>();  
 | 
        Dictionary<int, int> m_NpcMapItems;  
 | 
  
 | 
        public const int REIKI_DATAMAP = 32040;  
 | 
        public const int FAIRY_DATAMAP = 32050;  
 | 
        public const int REIKI_CLIENTDATAMAP = 3240;  
 | 
        public const int FAIRY_CLIENTDATAMAP = 3250;  
 | 
  
 | 
        public bool IsInDungeon { get; private set; }  
 | 
        public int autoCollectLevel { get; private set; }  
 | 
  
 | 
        public event Action onMapNpcCountRefresh;  
 | 
  
 | 
        HazyRegionModel hazyRegionModel { get { return ModelCenter.Instance.GetModel<HazyRegionModel>(); } }  
 | 
        DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel<DungeonModel>(); } }  
 | 
  
 | 
        public override void Init()  
 | 
        {  
 | 
            ParseConfig();  
 | 
  
 | 
            StageLoad.Instance.onStageLoadFinish += OnStageLoadFinish;  
 | 
        }  
 | 
  
 | 
        public void OnBeforePlayerDataInitialize()  
 | 
        {  
 | 
        }  
 | 
  
 | 
        public void OnPlayerLoginOk()  
 | 
        {  
 | 
        }  
 | 
  
 | 
        public override void UnInit()  
 | 
        {  
 | 
            StageLoad.Instance.onStageLoadFinish -= OnStageLoadFinish;  
 | 
        }  
 | 
  
 | 
        void ParseConfig()  
 | 
        {  
 | 
            var funcConfig = FuncConfigConfig.Get("HazyGrassNpcMapItem");  
 | 
            m_NpcMapItems = ConfigParse.GetDic<int, int>(funcConfig.Numerical1);  
 | 
            var level = 0;  
 | 
            int.TryParse(funcConfig.Numerical2, out level);  
 | 
            autoCollectLevel = level;  
 | 
        }  
 | 
  
 | 
        private void OnStageLoadFinish()  
 | 
        {  
 | 
            var mapId = PlayerDatas.Instance.baseData.MapID;  
 | 
  
 | 
            IsInDungeon = false;  
 | 
            if (IsInGrassDungeon(mapId))  
 | 
            {  
 | 
                IsInDungeon = true;  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public bool IsInGrassDungeon(int mapId)  
 | 
        {  
 | 
            if (!(StageLoad.Instance.currentStage is DungeonStage))  
 | 
            {  
 | 
                return false;  
 | 
            }  
 | 
  
 | 
            if (mapId == REIKI_CLIENTDATAMAP  
 | 
                || mapId == FAIRY_CLIENTDATAMAP)  
 | 
            {  
 | 
                return true;  
 | 
            }  
 | 
  
 | 
            var configs = HazyRegionConfig.GetValues();  
 | 
            foreach (var config in configs)  
 | 
            {  
 | 
                if ((config.incidentType == (int)HazyRegionIncidentType.FairyGrass  
 | 
                    || config.incidentType == (int)HazyRegionIncidentType.ReikiGrass)  
 | 
                    && config.dungeonId == mapId)  
 | 
                {  
 | 
                    return true;  
 | 
                }  
 | 
            }  
 | 
            return false;  
 | 
        }  
 | 
  
 | 
        bool GetDungeonTargetDone()  
 | 
        {  
 | 
            var dungeonHintId = 0;  
 | 
            if (ClientDungeonStageUtility.isClientDungeon)  
 | 
            {  
 | 
                var incidentId = hazyRegionModel.GetIncidentId(ClientDungeonStageUtility.dungeonInfo.mapId, ClientDungeonStageUtility.dungeonInfo.lineId);  
 | 
                var config = HazyRegionConfig.Get(incidentId);  
 | 
                if (config != null)  
 | 
                {  
 | 
                    dungeonHintId = dungeonModel.GetDungeonHintId(config.dungeonId, 0);  
 | 
                }  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                dungeonHintId = dungeonModel.GetDungeonHintId(PlayerDatas.Instance.baseData.MapID, 0);  
 | 
            }  
 | 
            if (dungeonHintId != 0)  
 | 
            {  
 | 
                var config = DungeonHintConfig.Get(dungeonHintId);  
 | 
                if (config != null)  
 | 
                {  
 | 
                    if (config.targetType1 != 0)  
 | 
                    {  
 | 
                        var times = GetCompleteTimes(config.targetType1, config.NPC1ID);  
 | 
                        if (times < config.targetValue1[0])  
 | 
                        {  
 | 
                            return false;  
 | 
                        }  
 | 
                    }  
 | 
                    if (config.targetType2 != 0)  
 | 
                    {  
 | 
                        var times = GetCompleteTimes(config.targetType2, config.NPC2ID);  
 | 
                        if (times < config.targetValue2[0])  
 | 
                        {  
 | 
                            return false;  
 | 
                        }  
 | 
                    }  
 | 
                    if (config.targetType3 != 0)  
 | 
                    {  
 | 
                        var times = GetCompleteTimes(config.targetType3, config.NPC3ID);  
 | 
                        if (times < config.targetValue3[0])  
 | 
                        {  
 | 
                            return false;  
 | 
                        }  
 | 
                    }  
 | 
                }  
 | 
            }  
 | 
            return true;  
 | 
        }  
 | 
  
 | 
        int GetCompleteTimes(int type, int[] npcIds)  
 | 
        {  
 | 
            var times = 0;  
 | 
            for (int i = 0; i < npcIds.Length; i++)  
 | 
            {  
 | 
                switch (type)  
 | 
                {  
 | 
                    case 11:  
 | 
                        times += dungeonModel.GetDugneonNpcCollectCount(npcIds[i]);  
 | 
                        break;  
 | 
                    case 12:  
 | 
                        times += dungeonModel.GetDungeonNpcAttackCount(npcIds[i]);  
 | 
                        break;  
 | 
                }  
 | 
            }  
 | 
            return times;  
 | 
        }  
 | 
  
 | 
        public int GetClientMapId(int incidentId)  
 | 
        {  
 | 
            var config = HazyRegionConfig.Get(incidentId);  
 | 
            switch ((HazyRegionIncidentType)config.incidentType)  
 | 
            {  
 | 
                case HazyRegionIncidentType.FairyGrass:  
 | 
                    return FAIRY_CLIENTDATAMAP;  
 | 
                case HazyRegionIncidentType.ReikiGrass:  
 | 
                    return REIKI_CLIENTDATAMAP;  
 | 
            }  
 | 
            return 0;  
 | 
        }  
 | 
  
 | 
        public int GetGrassMapId(int incidentId)  
 | 
        {  
 | 
            var config = HazyRegionConfig.Get(incidentId);  
 | 
            if (config != null)  
 | 
            {  
 | 
                return config.dungeonId;  
 | 
            }  
 | 
            return 0;  
 | 
        }  
 | 
  
 | 
        public List<int> GetGrassNpcInfos(int mapId)  
 | 
        {  
 | 
            return new List<int>(m_MapNpcCount.Keys);  
 | 
        }  
 | 
  
 | 
        public int GetMapNpcCount(int npcId)  
 | 
        {  
 | 
            if (m_MapNpcCount.ContainsKey(npcId))  
 | 
            {  
 | 
                return m_MapNpcCount[npcId];  
 | 
            }  
 | 
            return 0;  
 | 
        }  
 | 
  
 | 
        public int GetGrassNpcItemId(int npcId)  
 | 
        {  
 | 
            if (m_NpcMapItems.ContainsKey(npcId))  
 | 
            {  
 | 
                return m_NpcMapItems[npcId];  
 | 
            }  
 | 
            return 0;  
 | 
        }  
 | 
  
 | 
        public string GetLocalSaveKey(int mapId)  
 | 
        {  
 | 
            return StringUtility.Contact("HazyGrassMap", mapId, "_", PlayerDatas.Instance.PlayerId);  
 | 
        }  
 | 
  
 | 
        public void DeleteLocalSaveKey()  
 | 
        {  
 | 
            LocalSave.DeleteKey(GetLocalSaveKey(FAIRY_CLIENTDATAMAP));  
 | 
            LocalSave.DeleteKey(GetLocalSaveKey(REIKI_CLIENTDATAMAP));  
 | 
        }  
 | 
  
 | 
        public bool CanCollectClientNpc(int npcId)  
 | 
        {  
 | 
            if (ClientDungeonStageUtility.isClientDungeon  
 | 
                && (ClientDungeonStageUtility.clientMapId == REIKI_CLIENTDATAMAP  
 | 
                || ClientDungeonStageUtility.clientMapId == FAIRY_CLIENTDATAMAP))  
 | 
            {  
 | 
                var incidentId = hazyRegionModel.GetIncidentId(ClientDungeonStageUtility.dungeonInfo.mapId, ClientDungeonStageUtility.dungeonInfo.lineId);  
 | 
                var config = HazyRegionConfig.Get(incidentId);  
 | 
                if (config != null)  
 | 
                {  
 | 
                    var collectCount = dungeonModel.GetDugneonNpcCollectCount(npcId);  
 | 
                    var hintId = dungeonModel.GetDungeonHintId(config.dungeonId, config.lineId);  
 | 
                    var hintConfig = DungeonHintConfig.Get(hintId);  
 | 
  
 | 
                    if (hintConfig != null)  
 | 
                    {  
 | 
                        if (hintConfig.NPC1ID.Length > 1  
 | 
                            && Array.IndexOf(hintConfig.NPC1ID, npcId) != -1)  
 | 
                        {  
 | 
                            collectCount = 0;  
 | 
                            for (int i = 0; i < hintConfig.NPC1ID.Length; i++)  
 | 
                            {  
 | 
                                collectCount += dungeonModel.GetDugneonNpcCollectCount(hintConfig.NPC1ID[i]);  
 | 
                            }  
 | 
                            return collectCount < hintConfig.targetValue1[0];  
 | 
                        }  
 | 
  
 | 
                        if (hintConfig.NPC1ID.Length > 0 && hintConfig.NPC1ID[0] == npcId)  
 | 
                        {  
 | 
                            return collectCount < hintConfig.targetValue1[0];  
 | 
                        }  
 | 
                        else if (hintConfig.NPC2ID.Length > 0 && hintConfig.NPC2ID[0] == npcId)  
 | 
                        {  
 | 
                            return collectCount < hintConfig.targetValue2[0];  
 | 
                        }  
 | 
                    }  
 | 
                }  
 | 
            }  
 | 
            return true;  
 | 
        }  
 | 
  
 | 
        public void DisplayCollectErrorTip()  
 | 
        {  
 | 
            SysNotifyMgr.Instance.ShowTip("HazyGrassCollectError");  
 | 
        }  
 | 
  
 | 
        public void ReceivePackage(HA714_tagMCNPCCntList package)  
 | 
        {  
 | 
            if (package.MapID != REIKI_DATAMAP  
 | 
                && package.MapID != FAIRY_DATAMAP)  
 | 
            {  
 | 
                return;  
 | 
            }  
 | 
            m_MapNpcCount.Clear();  
 | 
            for (int i = 0; i < package.NPCInfoCnt; i++)  
 | 
            {  
 | 
                var data = package.NPCInfoList[i];  
 | 
                m_MapNpcCount[(int)data.NPCID] = (int)data.Cnt;  
 | 
            }  
 | 
            if (onMapNpcCountRefresh != null)  
 | 
            {  
 | 
                onMapNpcCountRefresh();  
 | 
            }  
 | 
  
 | 
            if (IsInGrassDungeon(PlayerDatas.Instance.baseData.MapID))  
 | 
            {  
 | 
                if (!WindowCenter.Instance.IsOpen<HazyGrassDungeonWin>())  
 | 
                {  
 | 
                    WindowCenter.Instance.Open<HazyGrassDungeonWin>();  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void RefreshMapNpcCount(int npcId, int count)  
 | 
        {  
 | 
            m_MapNpcCount[npcId] = count;  
 | 
            if (onMapNpcCountRefresh != null)  
 | 
            {  
 | 
                onMapNpcCountRefresh();  
 | 
            }  
 | 
        }  
 | 
  
 | 
        public void RequestEnterClientDungeon()  
 | 
        {  
 | 
            var config = HazyRegionConfig.Get(hazyRegionModel.processingIncidentId);  
 | 
            var mapId = GetClientMapId(hazyRegionModel.processingIncidentId);  
 | 
            ClientDungeonStageUtility.GotoNormalClientDungeon(mapId, config.dungeonId, config.lineId);  
 | 
        }  
 | 
    }  
 | 
}  
 | 
  
 |