using System;
using ModernUO.Serialization;
using Server.Targeting;

namespace Server.Items;

[SerializationGenerator(0, false)]
public partial class Dough : Item
{
    [Constructible]
    public Dough() : base(0x103d)
    {
        Stackable = Core.ML;
    }

    public override double DefaultWeight => 1.0;
}

[SerializationGenerator(0, false)]
public partial class SweetDough : Item
{
    [Constructible]
    public SweetDough() : base(0x103d)
    {
        Stackable = Core.ML;
        Hue = 150;
    }

    public override double DefaultWeight => 1.0;
    public override int LabelNumber => 1041340; // sweet dough
}

[SerializationGenerator(0, false)]
public partial class JarHoney : Item
{
    [Constructible]
    public JarHoney() : base(0x9ec)
    {
        Stackable = true;
    }

    public override double DefaultWeight => 1.0;
}

[SerializationGenerator(0, false)]
public partial class BowlFlour : Item
{
    [Constructible]
    public BowlFlour() : base(0xa1e)
    {
    }

    public override double DefaultWeight => 1.0;
}

[SerializationGenerator(0, false)]
public partial class WoodenBowl : Item
{
    [Constructible]
    public WoodenBowl() : base(0x15f8)
    {
    }

    public override double DefaultWeight => 1.0;
}

[TypeAlias("Server.Items.SackFlourOpen")]
[SerializationGenerator(0, false)]
public partial class SackFlour : Item, IHasQuantity
{
    [Constructible]
    public SackFlour() : base(0x1039)
    {
        _quantity = 20;
    }

    public override double DefaultWeight => 5.0;

    [SerializableProperty(0)]
    [CommandProperty(AccessLevel.GameMaster)]
    public int Quantity
    {
        get => _quantity;
        set
        {
            _quantity = Math.Min(20, Math.Max(0, value));

            if (_quantity == 0)
            {
                Delete();
            }
            else if (_quantity < 20 && ItemID is 0x1039 or 0x1045)
            {
                ++ItemID;
            }

            this.MarkDirty();
        }
    }

    public override void OnDoubleClick(Mobile from)
    {
        if (Movable && ItemID is 0x1039 or 0x1045)
        {
            ++ItemID;
        }
    }
}

[SerializationGenerator(0, false)]
public partial class Eggshells : Item
{
    [Constructible]
    public Eggshells() : base(0x9b4)
    {
    }

    public override double DefaultWeight => 0.5;
}

[SerializationGenerator(0, false)]
public partial class WheatSheaf : Item
{
    [Constructible]
    public WheatSheaf(int amount = 1) : base(7869)
    {
        Stackable = true;
        Amount = amount;
    }

    public override double DefaultWeight => 1.0;

    public override void OnDoubleClick(Mobile from)
    {
        if (Movable)
        {
            from.BeginTarget(4, false, TargetFlags.None, OnTarget);
        }
    }

    public virtual void OnTarget(Mobile from, object obj)
    {
        if (obj is AddonComponent addon)
        {
            obj = addon.Addon;
        }

        if (obj is IFlourMill mill)
        {
            var needs = mill.MaxFlour - mill.CurFlour;

            if (needs > Amount)
            {
                needs = Amount;
            }

            mill.CurFlour += needs;
            Consume(needs);
        }
    }
}
