﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simulator.NPCActions;
using Simulator.LocationHandler;
using Simulator.NPC_Types;
using Simulator.ItemHandler;
using Simulator.NPC_Types.EquipmentHandler;
using System.Diagnostics;

namespace Simulator.Planners.NaivePlanners
{
    class ArtisanPlanner : naivePlanner
    {

        public override void marketPlan()
        {
            base.marketPlan();
            if (Character.currentLocation is Market)
            {
                Market M = (Market)Character.currentLocation;

                List<Item> forSale = Character.Inventory.OfType<Item>().ToList<Item>().FindAll(x => x.crafted);
                foreach (var saleItem in forSale)
                {
                    int keep = Character.getKeepItem(saleItem.itemType.Name, saleItem.Tier);
                    float Price = M.getItemPrice(saleItem);
                    int quantity = saleItem.quantity - keep;
                    if (quantity > 0)
                    {
                        Trade trade = new Trade(Character, M, saleItem, Price, false, quantity);
                        parallelActionList.Actions.Add(trade);
                        if (!trade.Verify())
                        {
                            parallelActionList.Actions.Remove(trade);
                        }
                    }
                }
            }
        }

        private Item findBestItemToCraft()
        {

            float Demand = 0;

            ItemType bestType = null;
            //Start by picking one at random.
            string produce = Character.Job.Produces[_G._RNG.Next(0, Character.Job.Produces.Count - 1)];
            bestType = _G.ItemTypes.Find(x => x.Name == produce);
            int bestTier = 0;
            //See if something else is in higher demand.
            foreach (var S in Character.Job.Produces)
            {
                ItemType Type = (ItemType)_G.ItemTypes.Find(x => x.Name == S).Clone();
                for (int tier = 0; tier < Type.numTiers; tier++)
                {
                    if (Type.getTotalDemand(tier) > Demand)
                    {
                        bestType = Type;
                        Demand = Type.getTotalDemand(tier);
                        bestTier = tier;
                    }
                }
            }
            //catch to see if something went wrong.
            if (bestType == null)
            {
                Debug.Assert(false);
                return null;
            }
            Item I = new Item();
            I.itemType = bestType;
            I.Name = bestType.Name;
            I.Tier = bestTier;
            I.materialsUsed = bestType.materialsNeeded[bestTier];
            return I;
        }

        private beingCrafted currentProject()
        {
            var crafts = Character.Inventory.OfType<beingCrafted>().ToList<beingCrafted>();
            if (crafts.Count > 0)
                return crafts[0];
            return null;
        }

        public override void workPlan()
        {
            beingCrafted BC = currentProject();
            if (BC != null)
            {
                continueCrafting CC = new continueCrafting(BC, Character);
                sequentialActionList.add(CC);
            }
            else
            {
                Item newProject = findBestItemToCraft();
                if (newProject != null)
                {
                    Dictionary<string, int> materialsUsed = newProject.itemType.Materials(Character, newProject.Tier);
                    if (materialsUsed != null)
                    {
                        newProject.materialsUsed = materialsUsed;
                        Craft C = new Craft(Character, newProject, newProject.Tier);
                        sequentialActionList.add(C);
                    }
                    else
                    {
                        addMaterialNeeds(newProject.materialsUsed);
                    }
                }
            }
        }
    }
}
