using System.Windows.Forms;
using System.Threading;
using System.Collections.Generic;
using ArcheBuddy.Bot.Classes;
namespace ScarecrowAlternative {
    public class ScarecrowAlternative : Core {
        public void PluginRun() {
              
            
            // Activate an infinite loop (true: activate, false: deactivate)
            bool UseLoop = true;
            // How long to wait after each loop in MS (milliseconds)
            Random random = new Random();
                    var mseconds = random.Next(240, 300) * 1000;
                    var seconds  = mseconds / 1000;                    
            int  LoopTime = mseconds;
            
            InitItemsList();
            // SetPlantAlgoritm(PlantAlgoritm.Randomized;
            SetPlantAlgoritm(PlantAlgoritm.MaxPerfomance);
                        
            //  Create one or multiple group of farm
            //  Just replace the "11111" with your Farm ID
            //  Use the Scarecrow plugin to quickly find your Farm ID
            //  Example:
            List<uint> TreeFarmGroup = new List<uint>();
            TreeFarmGroup.Add(11111); 
            TreeFarmGroup.Add(11111); 
            List<uint> NoTreeFarmGroup = new List<uint>();
            NoTreeFarmGroup.Add(14220);
            NoTreeFarmGroup.Add(11993); 
            NoTreeFarmGroup.Add(10078); 
            NoTreeFarmGroup.Add(10079); 
            //NoTreeFarmGroup.Add(11111);
            
            List<uint> AnimalFarmGroup = new List<uint>();
            AnimalFarmGroup.Add(11111); 
            //AnimalFarmGroup.Add(1111); 
            //AnimalFarmGroup.Add(11111);
            
            // Create one or multiple group of item to collect/plant
            // Just enter the name of the item that are added at the bottom of this file
            // Take notice that you can't mix Plant/Tree with Animals so use seperate group.
            // Also, the first item you add will be the first item that will get processed.
            // Example:
            
            
            List<object> TreeItemGroup = new List<object>();
            TreeItemGroup.Add(OliveTree);                      
            List<object> NoTreeItemGroup = new List<object>();
            NoTreeItemGroup.Add(Barley);
            NoTreeItemGroup.Add(Azalea);
            NoTreeItemGroup.Add(Garlic);
            
            List<object> AnimalItemGroup = new List<object>();
            AnimalItemGroup.Add(DuckCage);
            AnimalItemGroup.Add(ChickenCoop);
            
            bool StopLoop = false;
            
            
            while (!StopLoop) {
                ClearLogs();
                
                //  This is where you set which item to collect using which Farm Group and Item Group
                //  Use CollectItems for Plants/Tree and CollectAnimals for Animals and Fruit Trees
                //  Example: 
                
                //Animals
                CollectAnimals(AnimalFarmGroup, AnimalItemGroup);
                PlantAnimals(AnimalFarmGroup, AnimalItemGroup);
                //Fruit Trees
                CollectAnimals(TreeFarmGroup, TreeItemGroup);
                PlantAnimals(TreeFarmGroup, TreeItemGroup);
                
                //All other plants
                CollectItems(NoTreeFarmGroup, NoTreeItemGroup);
                PlantItems(NoTreeFarmGroup, NoTreeItemGroup);
                
                Log("End of loop.");                
                Log("Running Anti-AFK.");
                UpdateNoAfkState();
                Turn(0.5);
                Log("Waiting for " + seconds.ToString() + " seconds.");
                          
                             
                if (UseLoop) {
                    Thread.Sleep(LoopTime);
                } else {
                    StopLoop = true;
                }
            }
        }
    
        //  If you want to add new item to gather/plant first create a new List<string> here
        //  Example if you want to add Aspen you would add this line:
        //  public List<string> Aspen = new List<string>();
        //  Then you would add to Aspen the corresponding string in the next block.
         
        // Trees
        public List<string> Pine = new List<string>();
        public List<string> Cedar = new List<string>();
        public List<string> MapleWoodlot = new List<string>();
        
        // Gathering
        public List<string> Mushroom = new List<string>();
        public List<string> Lavender = new List<string>();
        public List<string> Thistle = new List<string>();
        
        // Farming
        public List<string> Quinoa = new List<string>();
        public List<string> Barley = new List<string>();
        public List<string> Garlic = new List<string>();
        public List<string> Azalea = new List<string>();
        public List<string> BarleyBundle = new List<string>();
        
        // Animals&FruitTrees
        public List<object> Sheep = new List<object>();
        public List<object> Hen = new List<object>();
        public List<object> ChickenCoop = new List<object>();
        public List<object> DuckCage = new List<object>();
        public List<object> OliveTree = new List<object>();
        // This is where you add the string to a specific item
        // Example for Aspen:
        //  Aspen.Add("Aspen Tree"); (that's the name of the fully mature item)
        //  Aspen.Add("Logging: Spend up to 20 Labor to chop down a tree."); (that's the action required to use the fully mature item)
        //  Aspen.Add("Aspen Sapling") (that's the name of item that is used to plant it)
        //  Aspen.Add("Standart") (that's the motion type you want to use for this item)
        //  
        //  For animal it's a bit different but you should be able to figure it out by following how I did it for Sheep and Hen
        public void InitItemsList() {
            
            // Saplings
            Pine.Add("Pine Tree");
            Pine.Add("Logging: Spend up to 20 Labor to chop down a tree.");
            Pine.Add("Pine Sapling");
            Pine.Add("Standart");
            Cedar.Add("Cedar Tree");
            Cedar.Add("Logging: Spend up to 10 Labor to chop down a tree.");
            Cedar.Add("Cedar Sapling");
            Cedar.Add("Standart");
            
            // Woodlot
            MapleWoodlot.Add("Maple Woodlot");
            MapleWoodlot.Add("Logging: Spend up to 200 Labor to chop down a tree.");
            MapleWoodlot.Add("Maple Woodlot");
            MapleWoodlot.Add("SnakeFromBorder");
            // Gathering
            Mushroom.Add("Mushroom");
            Mushroom.Add("Gathering: Spend 1 Labor to gather materials.");
            Mushroom.Add("Mushroom Spore");
            Mushroom.Add("Standart");
            Lavender.Add("Lavender");
            Lavender.Add("Gathering: Spend 1 Labor to gather materials.");
            Lavender.Add("Lavender Seed");
            Lavender.Add("Standart");
            Thistle.Add("Thistle");
            Thistle.Add("Gathering: Spend 1 Labor to gather materials.");
            Thistle.Add("Thistle Seed");
            Thistle.Add("Standart");
            // Farming
            Quinoa.Add("Quinoa");
            Quinoa.Add("Farming: Spend up to 3 Labor to harvest crops.");
            Quinoa.Add("Quinoa Seed");
            Quinoa.Add("Standart");
          
            Garlic.Add("Garlic");
            Garlic.Add("Farming: Spend up to 2 Labor to harvest crops.");
            Garlic.Add("Garlic Seed");
            Garlic.Add("Standart");
          
            Azalea.Add("Azalea Plant");
            Azalea.Add("Farming: Spend 1 Labor to harvest crops.");
            Azalea.Add("Azalea Seed");
            Azalea.Add("Standart");  
            
            Barley.Add("Barley");
            Barley.Add("Farming: Spend 1 Labor to harvest crops.");
            Barley.Add("Barley Seed");
            Barley.Add("Standart");
            
            BarleyBundle.Add("Barley Bundle");
            BarleyBundle.Add("Farming: Spend 10 Labor to harvest crops.");
            BarleyBundle.Add("Barley Seed Bundle");
            BarleyBundle.Add("Standart");
            
            //FruitTrees
            
            // Make sure all your FruitTrees have a "Plant" List<string>, even if you don't need to plant that Tree. Otherwise it will break the collect and plant function.
            // Create new List<String> to hold the planting information.
            List<string> PlantOliveSapling = new List<string>();
            List<string> DyingOliveTree = new List<string>();
            List<string> FruitedOliveTree = new List<string>();
            // add the item name your planting FIRST (Yes, the order is important.)
            PlantOliveSapling.Add("Olive Sapling");
            // add planting algorithm SECOND (Yes, the order is important.)
            PlantOliveSapling.Add("SnakeFromBorder");
            DyingOliveTree.Add("Dying Olive Tree");
            DyingOliveTree.Add("Gathering: Spend up to 15 Labor to gather fruit.");
            FruitedOliveTree.Add("Fruited Olive Tree");
            FruitedOliveTree.Add("Gathering: Spend up to 15 Labor to gather fruit.");
            // always add the "Plant" List<string> first or the PlantAnimals/CollectAnimals function won't work
            OliveTree.Add(PlantOliveSapling);
            OliveTree.Add(DyingOliveTree);
            OliveTree.Add(FruitedOliveTree);
            
            // Livestock
            List<string> DiseasedSheep = new List<string>();
            List<string> HungrySheep = new List<string>();
            List<string> DyingSheep = new List<string>();
            List<string> NormalSheep = new List<string>();
            List<string> ThrivingSheep = new List<string>();
            DiseasedSheep.Add("Diseased Sheep");
            DiseasedSheep.Add("Husbandry: Spend 3 Labor and 1 Livestock Supplement to treat sick livestock.");
            HungrySheep.Add("Hungry Sheep");
            HungrySheep.Add("Husbandry: Spend 2 Labor and 1 Combined Feed to feed livestock.");
            NormalSheep.Add("Sheep");
            NormalSheep.Add("Husbandry: Spend 2 Labor and 1 Combined Feed to feed livestock.");
            ThrivingSheep.Add("Thriving Sheep");
            ThrivingSheep.Add("Husbandry: Spend up to 10 Labor to shear livestock.");
            DyingSheep.Add("Dying Sheep");
            DyingSheep.Add("Husbandry: Spend up to 10 Labor to shear livestock.");
            Sheep.Add(DiseasedSheep);
            Sheep.Add(HungrySheep);
            Sheep.Add(DyingSheep);
            Sheep.Add(NormalSheep);
            Sheep.Add(ThrivingSheep);
            // Make sure all your Animal Object have a "Plant" List<string>, even if you don't need to plant that animal. Otherwise it will break the collect and plant function.
            // Create new List<String> to hold the planting information.
            List<string> PlantHen = new List<string>();
            List<string> NormalHen = new List<string>();
            List<string> ThrivingHen = new List<string>();
            // add the item name your planting FIRST (Yes, the order is important.)
            PlantHen.Add("Chick");
            // add planting algorithm SECOND (Yes, the order is important.)
            PlantHen.Add("SnakeFromBorder");
            NormalHen.Add("Hen");
            NormalHen.Add("Husbandry: Spend 1 Labor and 1 Ground Grain to feed livestock.");
            ThrivingHen.Add("Thriving Hen");
            ThrivingHen.Add("Husbandry: Spend up to 5 Labor to gather eggs.");
            // always add the "Plant" List<string> first or the PlantAnimals/CollectAnimals function won't work
            Hen.Add(PlantHen);
            Hen.Add(NormalHen);
            Hen.Add(ThrivingHen);
            
            List<string> PlantChickenCoop = new List<string>();
            List<string> BrokenChickenCoop = new List<string>();
            List<string> SickChickenCoop = new List<string>();
            List<string> HungryChickenCoop = new List<string>();
            List<string> DyingChickenCoop = new List<string>();
            List<string> NormalChickenCoop = new List<string>();
            List<string> ThrivingChickenCoop = new List<string>();
            // add the item name your planting FIRST (Yes, the order is important.)
            PlantChickenCoop.Add("Bamboo Chicken Coop");
            // add planting algorithm SECOND (Yes, the order is important.)
            PlantChickenCoop.Add("SnakeFromBorder");
            BrokenChickenCoop.Add("Broken Chicken Coop");
            BrokenChickenCoop.Add("Repair the coop with 5 Labor and 2 Bamboo Stalks.");
            SickChickenCoop.Add("Sick Chicken Coop");
            SickChickenCoop.Add("Husbandry: Spend 15 Labor and 5 Livestock Supplements to treat sick livestock.");
            HungryChickenCoop.Add("Hungry Chicken Coop");
            HungryChickenCoop.Add("Husbandry: Spend 1 Labor and 3 Ground Grain to feed livestock.");
            NormalChickenCoop.Add("Small Chicken Coop");
            NormalChickenCoop.Add("Husbandry: Spend 1 Labor and 3 Ground Grain to feed livestock.");
            ThrivingChickenCoop.Add("Thriving Chicken Coop");
            ThrivingChickenCoop.Add("Husbandry: Spend up to 20 Labor to collect eggs from the cage. Has a low chance of completely shattering the cage.");
            DyingChickenCoop.Add("Dying Chicken Coop");
            DyingChickenCoop.Add("Husbandry: Spend up to 10 Labor to butcher livestock.");
            // always add the "Plant" List<string> first or the PlantAnimals/CollectAnimals function won't work
            ChickenCoop.Add(PlantChickenCoop);
            ChickenCoop.Add(BrokenChickenCoop);
            ChickenCoop.Add(SickChickenCoop);
            ChickenCoop.Add(HungryChickenCoop);
            ChickenCoop.Add(DyingChickenCoop);
            ChickenCoop.Add(NormalChickenCoop);
            ChickenCoop.Add(ThrivingChickenCoop);
            
            List<string> PlantDuckCage = new List<string>();
            List<string> BrokenDuckCage = new List<string>();
            List<string> DiseasedDuckCage = new List<string>();
            List<string> HungryDuckCage = new List<string>();
            List<string> DyingDuckCage = new List<string>();
            List<string> NormalDuckCage = new List<string>();
            List<string> ThrivingDuckCage = new List<string>();
            // add the item name your planting FIRST (Yes, the order is important.)
            PlantDuckCage.Add("Bamboo Duck Cage");
            // add planting algorithm SECOND (Yes, the order is important.)
            PlantDuckCage.Add("SnakeFromBorder");
            BrokenDuckCage.Add("Broken Duck Cage");
            BrokenDuckCage.Add("Repair the cage with 5 Labor and 2 Bamboo Stalks.");
            DiseasedDuckCage.Add("Diseased Duck Cage");
            DiseasedDuckCage.Add("Husbandry: Spend 1 Labor and 1 Livestock Supplement to treat sick livestock.");
            HungryDuckCage.Add("Hungry Duck Cage");
            HungryDuckCage.Add("Husbandry: Spend 1 Labor and 3 Ground Grain to feed livestock.");
            NormalDuckCage.Add("Small Duck Cage");
            NormalDuckCage.Add("Husbandry: Spend 1 Labor and 3 Ground Grain to feed livestock.");
            ThrivingDuckCage.Add("Thriving Duck Cage");
            ThrivingDuckCage.Add("Husbandry: Spend up to 35 Labor to collect feathers from the cage. Has a low chance of shattering the cage completely.");
            DyingDuckCage.Add("Dying Duck Cage");
            DyingDuckCage.Add("Husbandry: Spend up to 10 Labor to butcher livestock.");
            // always add the "Plant" List<string> first or the PlantAnimals/CollectAnimals function won't work
            DuckCage.Add(PlantDuckCage);
            DuckCage.Add(BrokenDuckCage);
            DuckCage.Add(DiseasedDuckCage);
            DuckCage.Add(HungryDuckCage);
            DuckCage.Add(DyingDuckCage);
            DuckCage.Add(NormalDuckCage);
            DuckCage.Add(ThrivingDuckCage);
        }
        public void CollectItems(List<uint> Scarecrow, List<object> Items) {
            foreach (uint f in Scarecrow) {
                Log("Farm ID: " + f);
                foreach (List<string> s in Items) {
                    Log("-> Collect " + s[0]);
                    CollectItemsAtFarm(s[0], s[1], f);
                }
            }
        }
        public void CollectAnimals(List<uint> Scarecrow, List<object> Items) {
            foreach (uint f in Scarecrow) {
                Log("Farm ID: " + f);
                foreach (List<object> o in Items) {
                    foreach(List<string> s in o) {
                                               //  Skip the "Plant" List<string>
                                               if (s != o[0]) {
                                     Log("-> Collect " + s[0]);
                             CollectItemsAtFarm(s[0], s[1], f);
                                               }
                    }
                }
            }
        }
        public void PlantAnimals(List<uint> Scarecrow, List<object> Items) {
            foreach (uint f in Scarecrow) {
                Log("Farm ID: " + f);
                foreach (List<object> o in Items) {
                    foreach(List<string> s in o) {
                        // It will only process the first List<string> in the List<object> and ignore the others hence why it's important to add the "Plant" List<string> first.
                        if (s == o[0]) {
                            if (s[1] == "SnakeFromBorder") {
                                SetPlantMotionType(PlantMotionType.SnakeFromBorder);
                            } else if (s[1] == "Standart") {
                                SetPlantMotionType(PlantMotionType.Standart);
                            } else if (s[1] == "SnakeFromCenter") {
                                SetPlantMotionType(PlantMotionType.SnakeFromCenter);
                            } else if (s[1] == "SecondStandart") {
                                SetPlantMotionType(PlantMotionType.SecondStandart);
                            }
                            Log("-> Plant " + s[0]);
                            PlantItemsAtFarm(s[0], f);
                        }
                    }
                }
            }
        } 
            
        public void PlantItems(List<uint> Scarecrow, List<object> Items) {
            foreach (uint f in Scarecrow) {
                Log("Farm ID: " + f);
                foreach (List<string> s in Items) {
                    if (s[3] == "SnakeFromBorder") {
                        SetPlantMotionType(PlantMotionType.SnakeFromBorder);
                    } else if (s[3] == "Standart"){
                        SetPlantMotionType(PlantMotionType.Standart);
                    }
                    Log("-> Plant " + s[2]);
                    PlantItemsAtFarm(s[2], f);
                }
            }
        }
    }
}