using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


class Inventory
{
    private int Weight = 0;
    private int maxWeight = 280;
    private int[] totalHarvested = new int[11];
    private int[] totalFertilizerUsed = new int[8];
    private Cargo cargo = new Cargo();
    private Cargo itemStorage = new Cargo();

    // Item list by index:
    // ---------------------------------------
    //              (0) Fertilizers
    //  1:  10-26-26
    //  2:  14-35-14
    //  3:  17-17-17
    //  4:  20-20
    //  5:  28-28
    //  6:  DAP
    //  7:  Urea
    //              (1) Crops:
    //  1:  Barley
    //  2:  Cotton
    //  3:  Ground Nuts
    //  4:  Maize
    //  5:  Millets
    //  6:  Oil Seeds
    //  7:  Paddy
    //  8:  Pulses
    //  9:  Sugarcane
    // 10:  Tobacco
    // 11:  Wheat
    //
    // Number - 1 to get the index in the array


    //  Adds all the items to the houses inventory for pickup at the houses position
    public void initInventorySystem()
    {
        itemStorage.initStorageItems();
    }

    public int getWeight()
    {
        return Weight;
    }


    // Adds item to the tractor.
    // 0 = Fertilizer
    // 1 = Crops
    public bool addItem(int newItemIndex, int Type)
    {
        Items newItem;
        if (Type == 1)
        {
            totalHarvested[newItemIndex]++;
        }
        newItem = itemStorage.getItemByIndex(newItemIndex, Type);
        if (maxWeight >= Weight + newItem.getWeight())
        {
            cargo.addItem(newItem, Type);
            Weight = Weight + newItem.getWeight();
            return true;
        }
        else
        {
            return false;
        }
    }


    // Uses an item from the tractor.
    // 0 = Fertilizer
    // 1 = Crops
    public bool useItem(int Index, int Type, bool isClearing)
    {
        if (cargo.itemExists(Index, Type))
        {
            if (Type == 0 && !isClearing)
            {
                totalFertilizerUsed[Index]++;
            }
            removeItem(Index, Type);
            return true;
        }
        return false;
    }

    public int getMaxWeight()
    {
        return maxWeight;
    }

    // Checks whether or not the tractor has the item in its inventory.
    // 0 = Fertilizer
    // 1 = Crops
    public bool itemExists(int Index, int Type)
    {
        return cargo.itemExists(Index, Type);
    }

    public void removeItem(int Index, int Type)
    {

        Weight = Weight - itemStorage.getItemByIndex(Index, Type).getWeight();
        cargo.removeItem(Index, Type);
    }

    public Cargo getPredefinedItems()
    {
        return itemStorage;
    }

    public void clearInventory()
    {
        for (int i = 0; i <= 6; i++)
            while (useItem(i, 0, true)) ;

        for (int i = 0; i <= 10; i++)
            while (useItem(i, 1, true)) ;
    }

    public void fillWithFertilizer()
    {
        int i = 0;
        while (getWeight() < getMaxWeight())
        {
            if (i > 6)
                i = 0;
            addItem(i, 0);
            i++;
        }
    }

    public bool isMissingFertilizer()
    {
        for (int i = 0; i <= 6; i++)
        {
            if (!itemExists(i, 0))
                return true;
        }
        return false;
    }

    public void printItems(Input input, SpriteBatch spriteBatch, SpriteFont Bold)
    {

        spriteBatch.DrawString(Bold, "Crops: ", new Vector2(470, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
        spriteBatch.DrawString(Bold, "Harvested:", new Vector2(600, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
        for (int i = 0; i < 11; i++)    //Print Crops
        {
            spriteBatch.DrawString(Bold, cargo.getCount(i, 1) + "    " + itemStorage.getItemByIndex(i, 1).getItemType(), new Vector2(470, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal);
            spriteBatch.DrawString(Bold, totalHarvested[i].ToString(), new Vector2(620, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal);
        }

        spriteBatch.DrawString(Bold, "Fertilizers: ", new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
        spriteBatch.DrawString(Bold, "Used ", new Vector2(830, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 2), Color.DarkRed);
        for (int i = 0; i < 7; i++)    //Print Fertilizers
        {
            spriteBatch.DrawString(Bold, cargo.getCount(i, 0) + "    " + itemStorage.getItemByIndex(i, 0).getItemType(), new Vector2(700, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal);
            spriteBatch.DrawString(Bold, totalFertilizerUsed[i].ToString(), new Vector2(835, input.getSize().Y * (input.getTileSize() + input.getSpacing()) + 20 * i + 22), Color.Teal);
        }


    }
}