package gaza;
import java.util.ArrayList;
import java.util.Scanner;
public class Manager {
	
	private ArrayList<Family> families;
	Driver drive = new Driver();
	
	// constracture methodsِ
	public Manager() {
		this.families = new ArrayList<>();
	}
	
	
	// this method to add a Family to Array List (Family registry)
	public boolean addFamily(Family family) {
	    if (!families.contains(family)) {
	    	// Add the Family to the Family list
	        families.add(family);
	        System.out.println("Family Number " + families.size() + " Added Successfully!");
	        drive.forMember();
	        return true;
	    } else {
	        System.out.println("This Family is Found before that");
	        return false;
	    }
	}
	
	
	// This method to update a Family names 
	public boolean updateFamily(String familyName , Family updateFamily) {
		Scanner in = new Scanner(System.in);
		for (Family family : families) {
			if (family.getFamilyName().equals(familyName)) {
				// we defined updateFamily a object , we need add a method get name to it until update
				System.out.println("choose what you need to do :");
				System.out.println("1. Add Member");
				System.out.println("2. Remove Member");
				System.out.println("3. Add Parent");
				System.out.println("4. Remove Parent");
				System.out.println("5. Update Family Name");
				int choice=in.nextInt();
				switch(choice) {
					case 1:
						updateFamily.addMember(drive.forMember(), familyName);
						break;
					
					case 2 :
						updateFamily.removeMember(drive.forMember());
						break;
						
					case 3:
						updateFamily.addParent(drive.forMember());
						break;
						
					case 4:
						updateFamily.removeParent(drive.forMember());
						break;
					case 5:
	                    System.out.println("Enter new family name:");
	                    String newFamilyName = in.nextLine();
	                    family.setFamilyName(newFamilyName);
	                    break;	
					
				}
				System.out.println("Family update is Successfully!");
				return true; //done
			}
		}
		System.out.println("Not Found a Family!");
		return false; // not successful
	}
	
	
	// This method for delete a family name from array (Family registry)
	public boolean deleteFamily(String familyName) {
		for(Family family: families) {
			if(family.getFamilyName().equals(familyName)) {
				families.remove(family); // successful
				System.out.println("Delete Family is Successful!");
				return true;
			}
		}	
		System.out.println("We not Found Family!");
		return false; 
	}
	
	
	// This method for search by a family name in a array(family registry) السجل العائلي
	public Family searchByName(String familyName) {
		for(Family family : families) {
			if(family.getFamilyName().equals(familyName)) {
				System.out.println("Found Family : "+familyName);
				return family; // done
			}
		}
		
		System.out.println("Family Not Found!");
		return null; // family name not found
	}
	
	
	// This method to search for person by id 
	public Person searchPersonByID(String personID) {
	    for (Family family : families) {
	        for (Person person : family.getMembers()) {
	            if (person.getID().equals(personID)) {
	                System.out.println("Found Person: " + person);
	                return person;
	            }
	        }
	    }

	    System.out.println("Person with ID " + personID + " not found.");
	    return null;
	}
	
	
	// This method to calculate a martyr member in a family // لحساب عدد الشهداء
	public int calculateTotalMartyrs() {
		int totalMartyrs = 0;
		for(Family family : families) 
			for(Person person : family.getMembers()) 
				if (person instanceof Martyr) {
					totalMartyrs++; // increase martyr member
				}
		System.out.println("Total Martyr is : "+totalMartyrs);
		return totalMartyrs;
	}
	
	
	// This method to calculate a Orphans people or member in the Family // لحساب عدد اليتامى
	public int calculateTotalOrphans() {
	    int totalOrphans = 0;

	    for (Family family : families) {
	        for (Person member : family.getMembers()) {
	            if (!(family.getParent().contains(member))) { // check if parents are has name in member list
	                boolean hasParent = false;
	                
	                for (Person parent : family.getParent()) {
	                    if (parent.getID().equals(member.getID())) {
	                        hasParent = true;
	                        break;
	                    }
	                }

	                if (!hasParent) {
	                    totalOrphans++; // increase orphans count
	                }
	            }
	        }
	    }

	    System.out.println("Total Orphans is: " + totalOrphans);
	    return totalOrphans;//return orphans number
	}

	
	
	//this method to calculate live person in a family // لحساب عدد الاشخاص اللذين على قيد الحياة في العائلة
	public int calculateTotalLivePersons() {
		int totalLivePersons =0;
		for(Family family : families) 
			totalLivePersons += family.getMembers().size();
		System.out.println("Total Live Persons is : "+totalLivePersons);
		return totalLivePersons;  // return live person member 
	}
	
		
	/* we need in this method to calculate every members in the same family, if he live or martyr or orphans. 
	 this meaning we need to use the last 3 methods in this one . and but the result in arrays*/
	
	public ArrayList<Integer> calculateFamilyStatistics(String familyName) {
	    for (Family family : families) {
        if (family.getFamilyName().equals(familyName)) {
	        int totalMartyrs = 0;
	        int totalOrphans = 0;
	        int totalLivePerson = 0;

	         for (Person person : family.getMembers()) {
	              if (person instanceof Martyr) {
	                  totalMartyrs++;
	                  } else if (!family.getParent().contains(person)) {
	                  totalOrphans++;
	                  } else {
	                  totalLivePerson++;
	                  }
	         }

	         ArrayList<Integer> familyStatistics = new ArrayList<>();
	         familyStatistics.add(totalMartyrs);
	         familyStatistics.add(totalOrphans);
	         familyStatistics.add(totalLivePerson);
	         System.out.println("Family Statistics for " + familyName + ": " + familyStatistics);
	         return familyStatistics;
        }
        }

	       System.out.println("Family Not Found!");
	       return null;
	}

	public ArrayList<Integer> calculateGlobalStatistics() {
	    int totalMartyrs = 0;
	    int totalOrphans = 0;
	    int totalLivePersons = 0;

	    for (Family family : families) {
	        for (Person person : family.getMembers()) {
	            if (person instanceof Martyr) {
	                 totalMartyrs++;
	            } else if (!family.getParent().contains(person)) {
	                 totalOrphans++;
	            } else {
	                 totalLivePersons++;
	            }
	        }
	    }

	    ArrayList<Integer> globalStatistics = new ArrayList<>();
	    globalStatistics.add(totalMartyrs);
	    globalStatistics.add(totalOrphans);
	    globalStatistics.add(totalLivePersons);

	    System.out.println("Global Statistics: " + globalStatistics);
        return globalStatistics;
	}


	@Override
	public String toString() {
		String result = "Manager : {\n" ;
		for(Family family : families) {
			result += "Family: " +family.getFamilyName() + "\n" ;
			result += "Members: "+family.getMembers() + "\n";
			result += "Parents: "+family.getParent() + "\n" + "}";
		}
		return result;
	}
}