package Übungen;
import java.util.ArrayList;

public class Whatsapp {
    private String name;
    private String teleN;
    private String nachricht;
    private static int ID = 100;
    private int id;
    private ArrayList <String> speicherContact;
    private ArrayList <String> speicherNummern;
    private ArrayList <String> blockContact;

	public Whatsapp(String name, String teleN) {
		this.name = name;
		this.nachricht = null;
		this.teleN = teleN;
		this.speicherContact = new ArrayList<>();
		this.blockContact = new ArrayList<>();
		speicherContact.add(this.name);
		this.speicherNummern = new ArrayList<>();
		speicherNummern.add(teleN);
		this.id = ++ID;
	}


	public void addContact(String name, String teleN){
		if (name != null && teleN != null){
			speicherContact.add(name);
			speicherNummern.add(teleN);
		}
	}
	
	public ArrayList<String> getContact(){
		return  this.speicherContact;
	}

	public void printContacte(){
		System.out.println("Deine KontaktListe: ");
		
		for (int i = 0; i < speicherContact.size();i++) 
			System.out.println("Name: " + speicherContact.get(i) +", Telefonummer: " + speicherNummern.get(i));
		
	}
	
	public void blocKontakt(Whatsapp block) {
		if (!blockContact.contains(block.getName())) {
			this.blockContact.add(block.getName());
			System.out.println(this.name + " hat " + block.getName() + " blockiert");
		}
			
		else
			System.err.println("Ist bereit Blockiert");
	}
	
	public ArrayList <String> getblockContact(){
		return blockContact;
	}
	
	public String searchContact(String name){
		for (int i = 0; i <speicherContact.size(); i++ )
			if (speicherContact.get(i).equalsIgnoreCase(name))
				return "Der Name ist gefunden: " +  name;
		return "Der Name exesitert nicht!";
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTeleN() {
		return teleN;
	}
	public void setTeleN(String teleN) {
		this.teleN = teleN;
	}
	public String getNachricht() {
		return nachricht;
	}
	public void setNachricht(String nachricht) {
		this.nachricht = nachricht;
	}
	
	
	@Override
	public String toString() {
		return "Whatsapp [name=" + name + ", teleN=" + teleN + "]";
	}
}

// nachrichten zwischen zwei Usern
class Nachrichten{
	private Whatsapp sender;
	private Whatsapp receiver;
	private ArrayList<String> saveMessages;
	
	public Nachrichten(Whatsapp sender, Whatsapp receiver) {
		this.sender = sender; //obai
		this.receiver = receiver; //omar
		this.saveMessages = new ArrayList<>();
	}
	
	public void sendMessage(Whatsapp sender, Whatsapp receiver, String message) {
	    if (receiver.getblockContact().contains(sender.getName())) {
	    	String blockMessage = "Du wurdest von " + receiver.getName() + " blockiert!";
	    	System.err.println(blockMessage);
	        return;
	    }
	    if (sender.getblockContact().contains(receiver.getName())) {
	    	String blockMessage = "Du hast  " + receiver.getName() + " blockiert!";
	    	System.err.println(blockMessage);
	    	return;
	    }

	    if (receiver != null) {
	        receiver.setNachricht(message);
	        String infoMessage = sender.getName() + " sendet " + receiver.getName() + " : " + receiver.getNachricht();
	        saveMessages.add(infoMessage);
	    } 
	}

	
	public ArrayList<String> getinfoMessage() {
		return saveMessages;
	}
	
	public void printsaveMessages() {
		System.out.println("Chat Room: ");
		for(String s:saveMessages) 
			System.out.println(s);
	}
	
}

class Groups{
		Whatsapp hersteller;
		ArrayList<Whatsapp> admins;
		ArrayList<Whatsapp> addmembers;
		ArrayList<String> allMessages;
		String groupsname;
		
		
		Groups(Whatsapp hersteller, String groupsname){
			this.hersteller = hersteller;
			this.groupsname = groupsname;
			this.addmembers = new ArrayList<>();
			this.admins = new ArrayList<>();
			this.allMessages = new ArrayList<>();
			addmembers.add(hersteller);
			admins.add(hersteller);
			System.out.println(this.hersteller.getName() + "(Admin): hat die Groupe " + this.groupsname + " erstellt");
		}
		
		public void sendeMessages(Whatsapp sender, String message) {
			if (addmembers.contains(sender)) {
				String speicher = sender.getName() + " : " + message ;
				allMessages.add(speicher);
			}
		}
		
		public ArrayList<String> getallMessages(){
			return allMessages;
		}
		
		public void printallMessages() {
			for (String s: allMessages)
				System.out.println(s);
		}
		
		public void removeMember(Whatsapp remover, Whatsapp toBeRemoved) {
		    if (!admins.contains(remover)) {
		        System.err.println("Du bist kein Admin");
		        return;
		    }
		    if (!addmembers.contains(toBeRemoved)) {
		        System.err.println("Mitglied nicht in der Gruppe");
		        return;
		    }
		    addmembers.remove(toBeRemoved);
		    System.out.println(toBeRemoved.getName() + " wurde aus der Gruppe entfernt.");
		}

		
		public void addMembers(Whatsapp member) {
			if (!addmembers.contains(member))
				addmembers.add(member);
			else
				System.err.println("member ist bereits da!");
		}
		
		public void addAdmin(Whatsapp admin, Whatsapp toBeAdmin) {
		    if (!admins.contains(admin)) {
		        System.err.println("Du musst Admin sein");
		        return;
		    }
		    if (admins.contains(toBeAdmin)) {
		        System.err.println(toBeAdmin.getName() + " ist bereits Admin.");
		        return;
		    }
		    admins.add(toBeAdmin);
		    System.out.println(admin.getName() + " hat " + toBeAdmin.getName() + " als Admin benannt.");
		}

		
		public ArrayList<Whatsapp> getaddmembers(){
			
			return addmembers;
		}
		
		public void printaddmembers(Whatsapp printer) {
			if (!addmembers.contains(printer)) {
				System.err.println("du bist kein Mitglied!");
				return;
			}
			
			System.out.println("Alle Members:");
			int i = 1;
			for (Whatsapp s : addmembers) {
		        String memberName = printer.getContact().contains(s.getName()) ? s.getName() : "Unbekannt";

		        if (admins.contains(s)) 
		            System.out.println("Mitglied(Admin) " + i + " : " + memberName);
		        else 
		            System.out.println("Mitglied " + i + " : " + memberName);
		        i++;
			}
		}
	}

