// Copyright (c) Strange Loop Games. All rights reserved.
// See LICENSE file in the project root for full license information.

#nullable enable
namespace Eco.Mods.TechTree
{
    using Eco.Core.Items;
    using Eco.Gameplay.Blocks;
    using Eco.Gameplay.GameActions;
    using Eco.Gameplay.Interactions;
    using Eco.Gameplay.Items;
    using Eco.Gameplay.Objects;
    using Eco.Shared.Items;
    using Eco.Shared.Localization;
    using Eco.Shared.Math;
    using Eco.World;
    using Eco.World.Blocks;
    using System;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Linq;

    [Tag("Harvester")]
    [Category("Hidden")]
    [Mower]
    public abstract partial class MacheteItem : ToolItem
    {
        static readonly LocString ClearString                 = Localizer.DoStr("Clear");

        public override ClientPredictedBlockAction LeftAction => ClientPredictedBlockAction.DestroyBlock;
        public override LocString LeftActionDescription       => ClearString;

        public override bool CustomHighlight                  => true;

        public override InteractResult OnActLeft(InteractionContext context)
        {
            var interactionResult = InteractResult.NoOp;
            // Destroy highlighted plants
            if (context.HasBlock && context.Block!.Is<Clearable>())
                interactionResult = (InteractResult)AtomicActions.DestroyPlantNow(this.CreateMultiblockContext(context), notify: false);

            if (interactionResult != InteractResult.NoOp) return interactionResult;

            if (context.Target is WorldObject) return this.BasicToolOnWorldObjectCheck(context);

            return base.OnActLeft(context);
        }

        //Check if a block can be cleared by the machete
        public override bool ShouldHighlight(Type block)
        {
            return Block.Is<Clearable>(block);
        }

        //Get plants in front of the player by using a cone check. Used to check which plants will be destroyed by the machete
        protected override IEnumerable<Vector3i> GetAreaOfEffectBlocks(InteractionContext context, bool includeCenter = true, bool highlightCenter = true)
        {
            if (context.Player == null) return Enumerable.Empty<Vector3i>();

            var centerPos = context.HasBlock ? context.BlockPosition!.Value : context.TargetPosition;          //The position with which the player is interacting
            var targets   = new HashSet<Vector3i>();                                                           //Use HashSet instead of list to avoid duplicates

            if (this.ShouldHighlight(centerPos)) targets.Add(centerPos);                                       //Add center position to targets if it has a plant

            var blocks    = InteractionConeChecker.GetBlocksInConeCheck2D(centerPos, context.Player.FacingDir); //Get blocks in front of player

            foreach (var blockPosition in blocks)
            {
                if (this.ShouldHighlight(blockPosition)) targets.Add(blockPosition);                           //Add position to targets if it has a plant
            }

            return targets;
        }
    }
}
