using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LolDataRequestLib.ResponseData
{
    internal class 룬데이터 : AResponseData
    {

        protected override DataTable buildDataForResponse(BsonDocument recvDocument)
        {
            BsonArray bufPlayerDataList = null;


            DataTable 룬데이터테이블 = new DataTable();

            룬데이터테이블.TableName = DBDefine.요청데이터분류.룬데이터.ToString();

            룬데이터테이블.Columns.Add("팀");
            룬데이터테이블.Columns.Add("선수이름");
            룬데이터테이블.Columns.Add("챔피언이름");
            룬데이터테이블.Columns.Add("메인룬스타일");
            룬데이터테이블.Columns.Add("메인룬이름");
            룬데이터테이블.Columns.Add("서브룬스타일");
            룬데이터테이블.Columns.Add("서브룬이름");
            룬데이터테이블.Columns.Add("룬1");
            룬데이터테이블.Columns.Add("룬2");
            룬데이터테이블.Columns.Add("룬3");
            룬데이터테이블.Columns.Add("룬4");
            룬데이터테이블.Columns.Add("룬5");
            룬데이터테이블.Columns.Add("룬6");
            룬데이터테이블.Columns.Add("룬7");
            룬데이터테이블.Columns.Add("룬8");
            룬데이터테이블.Columns.Add("룬9");
            룬데이터테이블.Columns.Add("룬10");
            룬데이터테이블.Columns.Add("룬배열");


            if (recvDocument == null)
            {
                return 룬데이터테이블;
            }

            bufPlayerDataList = recvDocument["eventDocument"]["participants"].AsBsonArray;


            foreach (BsonValue item in bufPlayerDataList)
            {
                DataRow bufRow = 룬데이터테이블.NewRow();

                bufRow["팀"] = (DBDefine.팀구분)item["teamID"].ToInt32();
                bufRow["선수이름"] = item["summonerName"].ToString();
                bufRow["챔피언이름"] = item["championName"].ToString();

                if (!item.AsBsonDocument.Contains("perks"))
                {
                    continue;
                }

                int 메인룬 = item["perks"][0]["perkStyle"].ToInt32();

                bufRow["메인룬스타일"] = 메인룬;

                if (DataManager.getInstance().mRuneTable.ContainsKey(메인룬))
                {
                    bufRow["메인룬이름"] = DataManager.getInstance().mRuneTable[메인룬];
                }

                int 서브룬 = item["perks"][0]["perkSubStyle"].ToInt32();

                bufRow["서브룬스타일"] = 서브룬;

                if (DataManager.getInstance().mRuneTable.ContainsKey(서브룬))
                {
                    bufRow["서브룬이름"] = DataManager.getInstance().mRuneTable[서브룬];
                }

                bufRow["룬배열"] = item["perks"][0]["perkIds"].ToString();

                BsonArray runeArray = item["perks"][0]["perkIds"].AsBsonArray;
                if (runeArray.Count != 0)
                {
                    for (int i = 1; i < runeArray.Count + 1; i++)
                    {
                        int runeID = runeArray[i - 1].ToInt32();

                        if (DataManager.getInstance().mRuneTable.ContainsKey(runeID))
                        {
                            bufRow["룬" + i] = DataManager.getInstance().mRuneTable[runeID];
                        }

                        
                    }
                }

                룬데이터테이블.Rows.Add(bufRow);

            }

            return 룬데이터테이블;

        }

        protected override BsonDocument getDataFromMongo()
        {

            //var filter = Builders<BsonDocument>.Filter.Eq("RequestGameID", DataManager.getInstance().mPlatformGameID);

            var projection = Builders<BsonDocument>.Projection
                .Exclude("_id")
                .Include("eventDocument");

            List<BsonDocument> documents = mEventDataBase.GetCollection<BsonDocument>("game_info")
                .Find(new BsonDocument()) //.Find(filter)
                .SortByDescending(x => x["sequenceIndex"])
                .Project(projection)
                .Limit(1)
                .ToList();

            if (documents.Count == 0)
            {
                return null;
            }

            return documents.Last();
        }
    }
}
