using Campofinale.Game.Inventory;
using Campofinale.Network;
using Campofinale.Protocol;
using Campofinale.Resource;

namespace Campofinale.Packets.Sc
{
    public class PacketScItemBagScopeSync : Packet
    {
        public PacketScItemBagScopeSync(Player client,ItemValuableDepotType type) {
            
            ScItemBagScopeSync proto = new ScItemBagScopeSync()
            {
                Bag = new()
                {
                    GridLimit = client.inventoryManager.items.maxBagSize,
                    Grids =
                    {
                        /*new ScdItemGrid()
                        {
                            GridIndex=0,
                            Count=1,
                            Id="item_port_power_pole_2",
                            Inst = new()
                            {
                                InstId=300000000000,

                            },

                        },
                        new ScdItemGrid()
                        {
                            GridIndex=1,
                            Count=1,
                            Id="item_port_travel_pole_1",
                            Inst = new()
                            {
                                InstId=300000000001,

                            },

                        },
                        new ScdItemGrid()
                        {
                            GridIndex=2,
                            Count=1,
                            Id="item_port_grinder_1",
                            Inst = new()
                            {
                                InstId=300000000002,

                            },

                        },
                        new ScdItemGrid()
                        {
                            GridIndex=3,
                            Count=1,
                            Id="item_port_sp_hub_1",
                            Inst = new()
                            {
                                InstId=300000000003,

                            },
                        },
                        new ScdItemGrid
                        {
                            GridIndex=4,
                            Count=1,
                            Id="item_port_power_diffuser_1",
                            Inst = new()
                            {
                                InstId=300000000004,

                            },
                        }*/
                    }
                },
                FactoryDepot =
                {
                    {"domain_1", 
                        new ScdItemDepot()
                        {
                            
                        } 
                    },
                    {"domain_2",
                        new ScdItemDepot()
                        {

                        }
                    }
                },
                ScopeName = 1,
                Depot = 
                { 

                },
                

            };
            
            //All depots type from 1 to 10
            int i = (int)type;
            if(i > 1)
            {
                proto.FactoryDepot.Clear();
                proto.Bag = null;
            }
            proto.Depot.Add(i, new ScdItemDepot());
            if(proto.Bag!=null)
            foreach (var item in client.inventoryManager.items.bag)
            {
                proto.Bag.Grids.Add(new ScdItemGrid()
                {
                    Count=item.Value.amount,
                    GridIndex=item.Key,
                    Id=item.Value.id,
                    Inst=item.Value.ToProto().Inst,
                });
            }
            List<Item> items = client.inventoryManager.items.items.FindAll(item => item.ItemType == (ItemValuableDepotType)i);
            items.ForEach(item =>
            {
                if (item.InstanceType())
                {
                    proto.Depot[i].InstList.Add(item.ToProto());
                }
                else 
                {

                    if (proto.Depot[(int)i].StackableItems.ContainsKey(item.id))
                    {
                        proto.Depot[(int)i].StackableItems[item.id]+= item.amount;
                    }
                    else
                    {
                        proto.Depot[(int)i].StackableItems.Add(item.id, item.amount);
                    }
                    
            
                }
            });
            
           // Logger.Print(proto.ToString());
            SetData(ScMsgId.ScItemBagScopeSync, proto);
        }

    }
}
