import java.awt.Color;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;
import java.util.Map.*;

import javax.swing.*;

public class BelediyeNufusCheckBox {
	
	public static String deger = "";

	public static boolean tfAdiyaman;
	public static boolean tfAntalya;
	public static boolean tfIstanbul;
	
	BelediyeNufusCheckBox() {
		
		int x = 10;
		int y = 10;
		int w = 100; 
		int h = 30;
		int xf = w + 15;
		int yf = h + 15;
		
		JFrame frame = new JFrame("Belediyeler HashMap ile CheckBox Kullanımı");
		frame.setLayout(null);
		
		JPanel anapanel = new JPanel();
		anapanel.setLayout(null);
		anapanel.setBackground(new Color(146, 216, 161));
		anapanel.setBounds(25,25,350,450);
		
		JLabel sehirSecBaslik = new JLabel("Şehir İşaretleyiniz");
		sehirSecBaslik.setFont(new Font("Ubuntu", Font.BOLD ,20));
		sehirSecBaslik.setBounds(x,y,w+100,h); x = 15; y = y + yf;
		
		JCheckBox cbAdiyaman = new JCheckBox("Adıyaman");
		cbAdiyaman.setOpaque(false);
		cbAdiyaman.setBounds(x,y,w,h); x = x + xf;
		
		JCheckBox cbAntalya = new JCheckBox("Antalya");
		cbAntalya.setOpaque(false);
		cbAntalya.setBounds(x, y, w, h); x = x + xf;

		JCheckBox cbIstanbul = new JCheckBox("İstanbul");
		cbIstanbul.setOpaque(false);
		cbIstanbul.setBounds(x, y, w, h); x = 15; y = y + yf;
		
		JLabel belediyelerBaslik = new JLabel("Belediyeler");
		belediyelerBaslik.setHorizontalAlignment(JLabel.CENTER);
		belediyelerBaslik.setFont(new Font("Ubuntu", Font.BOLD ,25));
		belediyelerBaslik.setBounds(x,y,350,30); y = y + yf;
		
		JTextArea texta = new JTextArea();
		texta.setEditable(false);
		texta.setFont(new Font("Ubuntu", Font.PLAIN ,17));
		
		JScrollPane scrollP = new JScrollPane(texta);
	    scrollP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollP.setBounds(x,y,320,270); y = y + yf;
				
	    cbAdiyaman.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {  
				
				tfAdiyaman = cbAdiyaman.isSelected();  // T/F döndürür...
				tfAntalya = cbAntalya.isSelected();
			    tfIstanbul = cbIstanbul.isSelected();
				
			    texta.setText(hashMapSehirler(tfAdiyaman,tfAntalya,tfIstanbul));
			    
			}
		});
		
		cbAntalya.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				
				tfAdiyaman = cbAdiyaman.isSelected();
				tfAntalya = cbAntalya.isSelected();
			    tfIstanbul = cbIstanbul.isSelected();
				
			    texta.setText(hashMapSehirler(tfAdiyaman,tfAntalya,tfIstanbul));

			}
		});
		
		cbIstanbul.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				
				tfAdiyaman = cbAdiyaman.isSelected();
				tfAntalya = cbAntalya.isSelected();
			    tfIstanbul = cbIstanbul.isSelected();
				
			    texta.setText(hashMapSehirler(tfAdiyaman,tfAntalya,tfIstanbul));
	
			}
		});
		
		anapanel.add(sehirSecBaslik);
		anapanel.add(cbAdiyaman);
		anapanel.add(cbAntalya);
		anapanel.add(cbIstanbul);
		anapanel.add(belediyelerBaslik);
		anapanel.add(scrollP);
		
		frame.add(anapanel);
		
		frame.setSize(400,540);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
	}
	
	public static String hashMapSehirler(boolean tf1, boolean tf2, boolean tf3) {
		
		HashMap<String, String> belediyeler = new HashMap<>();
		
		        // key         value
		belediyeler.put("Gerger","Adıyaman");
		belediyeler.put("Besni","Adıyaman");
		belediyeler.put("Gölbaşı","Adıyaman");
		belediyeler.put("Kahta","Adıyaman");
		belediyeler.put("Çelikhan", "Adıyaman");
		
		belediyeler.put("Alanya","Antalya");
		belediyeler.put("Muratpaşa","Antalya");
		belediyeler.put("Kemer","Antalya");
		belediyeler.put("Serik","Antalya");
		belediyeler.put("Kepez", "Antalya");
		
		belediyeler.put("Kadıköy","İstanbul");
		belediyeler.put("Adalar","İstanbul");
		belediyeler.put("Beşiktaş","İstanbul");
		belediyeler.put("Üsküdar","İstanbul");
		belediyeler.put("Fatih", "İstanbul");
		
		HashMap<Integer, String> nufus = new HashMap<>();
		
		nufus.put(525395,"Üsküdar");
		nufus.put(485233, "Kadıköy");
		nufus.put(1789358, "Beşiktaş");
		nufus.put(16372, "Adalar");
		nufus.put(382990, "Fatih");
		
		nufus.put(591895, "Kepez");
		nufus.put(521183, "Muratpaşa");
		nufus.put(350636, "Alanya");
		nufus.put(134953, "Serik");
		nufus.put(46615, "Kemer");
		
		nufus.put(127534, "Kahta");
		nufus.put(16416, "Gerger");
		nufus.put(15294, "Çelikhan");
		nufus.put(50150, "Gölbaşı");
		nufus.put(77207, "Besni");
		
		String deger = "";
	
		if(tf1) { 
			
			for(Entry giris: belediyeler.entrySet()) {
				if(giris.getValue().equals("Adıyaman")) {
					
						String ilce = (String) giris.getKey();
					
					for(Entry nufusGiris: nufus.entrySet()) {
						if(nufusGiris.getValue().equals(ilce)) {
							
			      		deger = deger + giris.getValue() + " - " + ilce + "\n\t( " + nufusGiris.getKey() + " ) \n";
							
						}
						
					}
					
				}	            	
				
			}
		}
		
		if(tf2) { 
			
			for(Entry giris: belediyeler.entrySet()) {
				if(giris.getValue().equals("Antalya")) {
					
						String ilce = (String) giris.getKey();
					
					for(Entry nufusGiris: nufus.entrySet()) {
						if(nufusGiris.getValue().equals(ilce)) {
							
			      		deger = deger + giris.getValue() + " - " + ilce + "\n\t( " + nufusGiris.getKey() + " ) \n";
							
						}
						
					}
					
				}	            	
				
			}
		}
		
		if(tf3) { 
			
			for(Entry giris: belediyeler.entrySet()) {
				if(giris.getValue().equals("İstanbul")) {
					
						String ilce = (String) giris.getKey();
					
					for(Entry nufusGiris: nufus.entrySet()) {
						if(nufusGiris.getValue().equals(ilce)) {
							
			      		deger = deger + giris.getValue() + " - " + ilce + "\n\t( " + nufusGiris.getKey() + " ) \n";
							
						}
						
					}
					
				}	            	
				
			}
		}
		
		if(tf1 == false && tf2 == false && tf3 == false) deger = "Şehir saeçilmedi\n\n[ false false false ]";
		
		return deger;
				
	}

}
