﻿using DressingSimulation.Domain.Logic.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DressingSimulation.Domain.Clothes.Interfaces;
using DressingSimulation.Domain.Enums;
using DressingSimulation.Domain.Furniture.Interfaces;
using DressingSimulation.Domain.Logic.Actions.Interfaces;
using DressingSimulation.Logic.Actions;

namespace DressingSimulation.Logic
{
    public class Brain : IBrain
    {
        private readonly IActionFactory _actionFactory;
        public Brain(IActionFactory actionFactory)
        {
            _actionFactory = actionFactory;
        }
        public List<string> ProcessInstructions(List<DressingActions> commands,WeatherTypes currentWeather, List<IClothes> wearingclothes, IFurniture furniture)
        {
            var memorys = new List<string>();
            foreach (var command in commands)
            {
                string memory = "fail";
                bool processCommand = true;
                var actionItem = _actionFactory.GetAction(currentWeather, command);
                if (actionItem != null && actionItem.IsPutOnCommand)
                {
                    processCommand = false;
                    var isClothingFound = furniture.SearchForClothing(actionItem.ClothingItem);
                    if(isClothingFound)
                    {
                        furniture.RemoveClothing(actionItem.ClothingItem);
                        processCommand = true;
                    }                    
                }
                else if(actionItem != null && typeof(LeaveHouse_Action) == actionItem.GetType())
                {
                    ((LeaveHouse_Action)actionItem).Weather = currentWeather;
                }

                if(actionItem != null && processCommand)
                    memory = actionItem.PerformAction(wearingclothes);
                

                memorys.Add(memory);

                if (memory == "fail") break;
            }

            return memorys;
        }
    }
}
