public void addToSinglesList(int newPersonIndex, TreePerson.PersonSex sex)
        {
            if (sex == TreePerson.PersonSex.Female)
                BachelorettePersonIndexList.Add(newPersonIndex);
            else
                BachelorPersonIndexList.Add(newPersonIndex);

        }
        public void removeFromSinglesList(int deadPersonIndex, TreePerson.PersonSex sex)
        {
            if (sex == TreePerson.PersonSex.Female)
                BachelorettePersonIndexList.Remove(deadPersonIndex);
            else
                BachelorPersonIndexList.Remove(deadPersonIndex);

        }
        public int AddChild(int familyIndex, string PID, Person familySearchPerson = null)
        {
            TreePerson myChild = new TreePerson(TreePerson.PersonType.FamilySearch, PID, familySearchPerson);
            int childPersonIndex = myPeople.addToAllPeople(myChild);
            allFamilies[familyIndex].AddChildIndex(childPersonIndex);
            myChild.BirthFamilyIndex = familyIndex;
            return childPersonIndex;

        }
        private void btnSimulation_Click(object sender, EventArgs e)
        {
            using (SimulationFormWizard simulationForm = new SimulationFormWizard())
            {
                txtOutput.Text = "Lets go forth and replentish the Earth, shall we?";
                this.btnPlay3DFT.Enabled = false;  // only enabled after saving the file.
                this.btnOpenFile.Enabled = false;


                var dialogRet = simulationForm.ShowDialog();

                if (dialogRet == DialogResult.Yes)
                {
                    //Debug.WriteLine("Awake");
                    _courtHouse = new CourtHouse();

                    _matchMaker = new Matchmaker(ref _courtHouse);
                    _matchMaker.init();
                    int lastYear = simulationForm.StartYear;
                    int currentYear = simulationForm.StartYear;
                    int endYear = currentYear + simulationForm.NumberOfYears;

                    TreePerson Adam = new TreePerson(TreePerson.PersonType.Adam, currentYear.ToString());
                    TreePerson Eve = new TreePerson(TreePerson.PersonType.Eve, currentYear.ToString());
                    var personAIndex = _matchMaker.courtHouse.myPeople.addToAllPeople(Adam);
                    var personEIndex = _matchMaker.courtHouse.myPeople.addToAllPeople(Eve);
                    _matchMaker.addToSinglesList(personAIndex, Adam.Sex);
                    _matchMaker.addToSinglesList(personEIndex, Eve.Sex);

                    //Debug.WriteLine("Hello " + Adam.Name + " and " + Eve.Name + " !");
                    //Debug.WriteLine(Adam.GetSex() + " and " + Eve.GetSex());

                    for (currentYear = simulationForm.StartYear;
                        currentYear <= endYear;
                        currentYear++)
                    {

                        //Debug.WriteLine("Happy New Year!!.  It is year: " + currentYear.ToString());
                        //Debug.WriteLine("We have " + _matchMaker.BachelorPersonIndexList.Count + " Bachelors, and " +
                        //          _matchMaker.BachelorettePersonIndexList.Count + " Bachelorettes");
                        //Debug.WriteLine("Our people count is " + _matchMaker.courtHouse.myPeople.allPeople.Count + ", with alive count =" +
                        //          _matchMaker.courtHouse.myPeople.livingCount());
                        //Debug.WriteLine("We have " + _matchMaker.courtHouse.allFamilies.Count + " Families <-----------------------");

                        _matchMaker.doWeddings(currentYear);
                        _matchMaker.beFruitfullAndMultiply(currentYear);
                        _matchMaker.mortality(currentYear);

                        lastYear = currentYear;
                    }
                    //Debug.WriteLine("We are done with populating the earth!");

                    //Debug.WriteLine("We have " + _matchMaker.BachelorPersonIndexList.Count + " Bachelors, and " +
                    //                _matchMaker.BachelorettePersonIndexList.Count + " Bachelorettes");
         

                    txtOutput.Clear();
                    txtOutput.AppendText(string.Format("Our simulation created {0} Families, and {1} Individuals",
                        _courtHouse.allFamilies.Count, _courtHouse.myPeople.allPeople.Count));
                    txtOutput.AppendText(System.Environment.NewLine + System.Environment.NewLine + "--> Next, Save this file, so that you can play it.");

                    //fileNameForSaving = string.Format("3DTree_Sim_F{0}_P{1}", _courtHouse.allFamilies.Count,
                    //    _courtHouse.myPeople.allPeople.Count);

                    HELPER_EnableOutputUI(true);

                }
                else
                {
                    txtOutput.Clear();
                    txtOutput.AppendText("Simulation, Canceled.");
                    this.btnPlay3DFT.Enabled = true;
                    this.btnOpenFile.Enabled = true;


                }
            }
        }
        public void beFruitfullAndMultiply(int currentYear)
        {

            foreach (Family myFamily in courtHouse.allFamilies)
            {
                if (BabyTime(myFamily, currentYear))
                {
                    TreePerson MyChild = new TreePerson(TreePerson.PersonType.Unique, currentYear.ToString());
                    int childPersonIndex = courtHouse.myPeople.addToAllPeople(MyChild);
                    myFamily.AddChildIndex(childPersonIndex);
                    addToSinglesList(childPersonIndex, MyChild.Sex);
                    MyChild.BirthFamilyIndex = courtHouse.allFamilies.IndexOf(myFamily);
                }
            }
        }
        private bool RecAddPersonsFamily(PersonState personState, int treePersonIndex, int generation = 1)
        {
            //Person must already be added to CourtHouse

            HELPER_Add_portrait((FamilyTreePersonState)personState, treePersonIndex);

            DescendancyResultsState descendacyState = personState.ReadDescendancy(
                FamilySearchOptions.IncludePersonDetails(),
                FamilySearchOptions.IncludeMarriageDetails(),
                FamilySearchOptions.IncludePersons(),
                QueryParameter.Generations(2)
                );

            if (_worker.CancellationPending == true) return false;

            //DescendancyResultsState
            DescendancyTree.DescendancyNode myNode = descendacyState.Tree.Root;
            int treePersonSpouceIndex;
            int familyIndex;
            string marriageDateString = "";

            TreePerson treePersonSpouce;

            if (myNode.Spouse != null)
            {
                treePersonSpouce = new TreePerson(TreePerson.PersonType.FamilySearch, myNode.Spouse.Id,
                    myNode.Spouse);

                treePersonSpouceIndex = _courtHouse.myPeople.addToAllPeople(treePersonSpouce);

                marriageDateString = myNode.Person.DisplayExtension.MarriageDate;

                HELPER_Add_portrait(ft.ReadPersonById(myNode.Spouse.Id), treePersonSpouceIndex);
                
            }
            else  // only used no start a family if needed (if this family with out a spouse has children)
            {
                treePersonSpouce = new TreePerson(TreePerson.PersonType.Null);
                treePersonSpouceIndex = _courtHouse.myPeople.addToAllPeople(treePersonSpouce);
                marriageDateString = "";
            }

            var nextGenerationValue = generation - 1;

            // Are we done with our recursion??

            if (nextGenerationValue == 0) return true;

            List<DescendancyTree.DescendancyNode> children = myNode.Children;

            if (children != null)
            {
                // We did not start a family because Spouse was null, however if we have children, lets add a place holder Bull Spouse
                    
                familyIndex = _courtHouse.StartFamily(treePersonIndex, treePersonSpouceIndex,
                    marriageDateString);

             
                // Add all the children to the family
                foreach (var child in children)
                {
                    if (_worker.CancellationPending == true) return false;

                    var childPersonIndex =_courtHouse.AddChild(familyIndex, child.Person.Id, child.Person);

                    FamilyTreePersonState childPersonState = ft.ReadPersonById(child.Person.Id);

                    if (RecAddPersonsFamily(childPersonState, childPersonIndex, nextGenerationValue) == false)
                        return false;

                }

            }
            else
            {   // if there are no children, but I still have a spouse - we need to start a family
                if (myNode.Spouse != null)
                {
                    familyIndex = _courtHouse.StartFamily(treePersonIndex, treePersonSpouceIndex,
                        marriageDateString);
                }
            }

            return true;
        }
        /// <summary>
        /// Add this person as a parent to their own family
        /// as well a Generation-1 other descendants
        /// Assumes this is a Descendancy type of flow
        /// </summary>
        /// <param name="personState">The Family Search Person to act on</param>
        /// <param name="generation">The Generation Radius that is left over.  When 1, only return Spouse if exists, decrement to Zero and return
        /// Otherwise if Generation Radius is more than Zero after decrement, then call This function recursivley on all children found.</param>
        public bool AddPersonsFamily(PersonState personState, int generation = 1)
        {

            var person = personState.Person;

            TreePerson treePerson = new TreePerson(TreePerson.PersonType.FamilySearch, person.Id, person);
            var treePersonIndex = _courtHouse.myPeople.addToAllPeople(treePerson);

            //Rec function assumes that the person is already added to the CourtHouse

            return RecAddPersonsFamily(personState, treePersonIndex, generation);


        }
 public int addToAllPeople(TreePerson person)
 {
     allPeople.Add(person);
     return allPeople.IndexOf(person);
 }
        public void Add(TreePerson newPerson)
        {

            allPeople.Add(newPerson);

        }