/**

 * Karyatid v2.0
 * Released under GNU GPLv3 license,
 * Open Source English Quiz Application
 * Copyright (C) Oct 2, 2022 Java Blackboard
 * 
 * This program is free software: 
 * you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.


---------------------------------------------------------------------------
 YouTube Description:
 Karyatid is released under GNU GPLv3 license, 
 ensuring that this educational tool remains free and open source forever. 
 Anyone can use, modify, and distribute the code, 
 but derivative works must also be open source under the same license. 
 #GPL #OpenSource #Copyleft"
 ---------------------------------------------------------------------------


*/


import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

public class Karyatid {
	
	public static String ogrenciIsmi = "";
	public static int resimSayisi = 22;
	public static ArrayList<Integer> secilenSoruListesiKaryatid;
	public static int soruPaneliNo = 0;

	public static String[] secenek = new String[(resimSayisi*4)];
	public static ButtonGroup[] secenekGroup = new ButtonGroup[resimSayisi];
	public static int[] cevapNo = new int[resimSayisi];	// Dogru cevaplar
	
	int x_baslangic = 25;
	int y_baslangic = 100;
	int rb_satirArasi = 18;	// JRadioButton() arasi yukseklik farki.
	int satirArasi = 30; 	// satirarasi
	int sutunArasi = 200; 	// sutun arasi blok farki...
	
	int x = x_baslangic;
	int y = y_baslangic;
	
	int rb_width = 140;  	// JRadioButton genislik
	int rb_height = 20;  	// JRadioButton yukseklik
	
	public static float dogru = 0.0f;	// dogru cevap sayisi
	public static float yanlis = 0.0f;	// yanlis cevap sayisi
	public static float bos = 0.0f;		// bos cevap sayisi
	
	String aciklama = " "; 	// puan hesabi yapildigindaki pencerede aciklama yazisi
		
	public static JPanel baslikPaneli() {
		
		JPanel bPanel = new JPanel();
		bPanel.setLayout(null);
		bPanel.setBackground(new Color(217, 208, 199));
		
		JLabel labelAppName = new JLabel("Karyatid | Picture Vocabulary Test");
		JLabel labelKonu = new JLabel("OBJECTS");
		
		JLabel labelOgrenciIsmiEtiketi = new JLabel("Ögrenci Ismi:");
		JLabel labelOgrenciIsmi = new JLabel(ogrenciIsmi);
		
		Color colorOgrenciIsmiEtiketi = new Color(2, 81, 89);
		labelOgrenciIsmiEtiketi.setHorizontalAlignment(SwingConstants.LEFT);
		labelOgrenciIsmiEtiketi.setForeground(colorOgrenciIsmiEtiketi);
		labelOgrenciIsmiEtiketi.setFont(new Font("American Typewriter", Font.PLAIN, 23));
		labelOgrenciIsmiEtiketi.setBounds(20, 45, 1000, 25);	
		
		Color colorOgrenci = new Color(2, 81, 89);
		labelOgrenciIsmi.setHorizontalAlignment(SwingConstants.LEFT);
		labelOgrenciIsmi.setForeground(colorOgrenci);
		labelOgrenciIsmi.setFont(new Font("American Typewriter", Font.PLAIN, 23));
		labelOgrenciIsmi.setBounds(20, 70, 1000, 25);	
		
		JLabel labelTarih = new JLabel();
		JLabel labelSaat = new JLabel();
		
		LocalDateTime tarihSaat = LocalDateTime.now();
		DateTimeFormatter tarihFormat = DateTimeFormatter.ofPattern("dd / MM / yyyy");
		labelTarih.setText(""+tarihSaat.format(tarihFormat));
		
		DateTimeFormatter saatFormat = DateTimeFormatter.ofPattern("E HH:mm:ss");  // E günün ilk üç harfini yazdirir.
		labelSaat.setText(""+tarihSaat.format(saatFormat));
		
		Color colorTarih = new Color(2, 81, 89);
		labelTarih.setHorizontalAlignment(SwingConstants.RIGHT);
		labelTarih.setForeground(colorTarih);
		labelTarih.setFont(new Font("American Typewriter", Font.PLAIN, 23));
		labelTarih.setBounds(0, 45, 1000, 25);	
		
		labelSaat.setHorizontalAlignment(SwingConstants.RIGHT);
		labelSaat.setForeground(colorTarih);
		labelSaat.setFont(new Font("American Typewriter", Font.PLAIN, 23));
		labelSaat.setBounds(0, 70, 1000, 25);	
		
		Color colorAppName = new Color(2, 81, 89);
		labelAppName.setHorizontalAlignment(SwingConstants.CENTER);
		labelAppName.setForeground(colorAppName);
		labelAppName.setFont(new Font("American Typewriter", Font.PLAIN, 33));
		labelAppName.setBounds(0, 0, 1024, 40);	
		
		Color colorKonu = new Color(2, 81, 89);
		labelKonu.setHorizontalAlignment(SwingConstants.CENTER);
		labelKonu.setForeground(colorKonu);
		labelKonu.setFont(new Font("American Typewriter", Font.BOLD, 65));
		labelKonu.setBounds(0, 40, 1024, 60);	
		
		bPanel.add(labelOgrenciIsmiEtiketi);
		bPanel.add(labelOgrenciIsmi);
		
		bPanel.add(labelTarih);
		bPanel.add(labelSaat);
			
		bPanel.add(labelAppName);
		bPanel.add(labelKonu);
		
		bPanel.setBounds(0, 0, 1024, 100);
		
		return bPanel;
	}
	
	public static JPanel butonPaneli() {
		
		JPanel bPanel = new JPanel();
		bPanel.setLayout(null);
		bPanel.setBackground(new Color(244, 224, 232));
		
		JButton finish = new JButton ("Finish the test");	// Testi bitiren buton.
		JButton clear = new JButton ("Clear the test");		// Cevaplari temizleyen buton.
		JButton close = new JButton ("Exit the test");		// Programi sonlandiran buton.
		
		JLabel copyright = new JLabel();
		copyright = new JLabel("© 2022 Java Blackboard - Vocabulary | Objects.");
		
		Color copyrightColor = new Color(1, 32, 48);
		copyright.setHorizontalAlignment(SwingConstants.CENTER);
		copyright.setForeground(copyrightColor);
		copyright.setFont(new Font("Verdana", Font.PLAIN, 13));
		copyright.setBounds(0, 70, 1024, 15);	
		
		bPanel.add(copyright);
		
		finish.setBounds(257, 30, 150, 30);
		clear.setBounds(437, 30, 150, 30);
		close.setBounds(617, 30, 150, 30);
		
		finish.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent event) {
 				
 				float soruSayisi = secilenSoruListesiKaryatid.size();
 				float soruPuan = (float) (100.0/soruSayisi); // her bir sorunun puani
 				float toplamPuan = 0.0f;
 								
 				String[] cevap = new String[resimSayisi];
 				for (int i=0 ; i<resimSayisi ; i++) cevap[i] = "-" ;

 				int liste = 0;	// Kulllanicinin seçtigi soru listesini temsil eden degisken.
 								// Mesela 5,7,11,19,20 gibi. Seçilen listedeki sorularin soru numaralari.
 			
 				for (int i=0 ; i<secilenSoruListesiKaryatid.size() ; i++) {
 					
 					// soruNo'yu tespit ediyoruz.
 					liste = secilenSoruListesiKaryatid.get(i);  // ArrayList'deki sorulari tek tek aliyoruz.
 					
 					//Ögrencinin cevabini aliyoruz.
 					cevap[liste] = secenekGroup[liste].getSelection().getActionCommand();
 					
 					//Dogru cevabi aliyoruz.
 					String dogruCevap = secenek[cevapNo[liste]];
 					
 					//Ögrencinin cevabi ile dogru cevapi karsilastiriyoruz.
 					if (cevap[liste].equals(dogruCevap)) {
 						
 						dogru++;
 						toplamPuan = dogru * soruPuan;
 						
 					//  console'a test amaçli cevaplari yazdirmak icin sysout kullaniyoruz.	
 					//	System.out.println("Gelen Cevap   : " + cevap[liste]);
 					//	System.out.println("Dogru Cevap   : " + dogruCevap); 
 					//	System.out.println("Dogru Sayisi  : " + dogru);
 					//	System.out.println("Test Puan     : " + toplamPuan + "\n");
 						
 					} else {
 						
 						yanlis++;
 						
 					//  console'a test amaçli cevaplari yazdirmak icin sysout kullaniyoruz.		
 					//	System.err.println("Gelen Cevap   : " + cevap[liste]);
 					//	System.out.println("Dogru Cevap   : " + dogruCevap); 
 					//	System.out.println("Yanlis Sayisi : " + yanlis);
 					//	System.out.println("Test Puan     : " + toplamPuan + "\n");
 						
 					}
 					
 				}  
 				 
 				ImageIcon resim = new ImageIcon("src/images/testPuan.png");
            			      	
		      	DecimalFormatSymbols toplamPuanSembolu = new DecimalFormatSymbols();
		      	toplamPuanSembolu.setDecimalSeparator('.');
		      	toplamPuanSembolu.setGroupingSeparator(';');
		      	DecimalFormat toplamPFormat = new DecimalFormat("###.00", toplamPuanSembolu);
		      
		      	// OptionPane'deki açiklamayi hazirliyoruz.
		       	String aciklama = "Ögrenci test sonucu olarak\n\n" + toplamPFormat.format(toplamPuan) + "\n\npuan almistir.\nDogru cevap sayisi : " + dogru + "\nYanlis cevap sayisi : " + yanlis; 
		        JOptionPane.showMessageDialog(null, aciklama, "Test puani", JOptionPane.ERROR_MESSAGE, resim);
		       
			}
		 			
		});
		
		
		// clear butonuna tiklaninca ne olacak?
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				for (int i=0 ; i<secilenSoruListesiKaryatid.size() ; i++) {
					
					secenekGroup[secilenSoruListesiKaryatid.get(i)].clearSelection();
				
				}
				
			}
			
		});
					
	
		// alttaki kisim "Kapat." dügmesinin fonksiyonunu icra eder :)
		close.addActionListener(new ActionListener() {
		 			
			public void actionPerformed(ActionEvent event) {
		 				
				System.exit(0);
		 				
			}
		 			
		});

		bPanel.add(finish);
		bPanel.add(clear);
		bPanel.add(close);
		
		bPanel.setBounds(0, 530, 1024, 100);
		
		return bPanel;
	}

	public static JScrollPane soruPaneli () {
		
		JPanel sPanel = new JPanel();
		sPanel.setLayout(null);
		sPanel.setBackground(new Color(54, 70, 89));
			
		sPanel.setPreferredSize(new Dimension(30+(secilenSoruListesiKaryatid.size()*220),380));
		
		JScrollPane levha = new JScrollPane(sPanel, JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		levha.setBounds(30,120,970,380);
		
		for(int i = 0; i<secilenSoruListesiKaryatid.size(); i++) {
			
			sPanel.add(soruEkle(i, secilenSoruListesiKaryatid.get(i)));
			
		}
		
		return levha;
		
	}
	
	public static JPanel soruEkle (int panelNo, int soruNo) {
		
		String[] resimDosyasi = new String[resimSayisi];
		
		resimDosyasi[0] = "01_padlock.jpg";
		resimDosyasi[1] = "02_pliers.jpg";
		resimDosyasi[2] = "03_wood_saw.jpg";
		resimDosyasi[3] = "04_handbell.jpg";
		resimDosyasi[4] = "05_harrow.jpg";
		resimDosyasi[5] = "06_blue_bead.jpg";
		resimDosyasi[6] = "07_boat.jpg";
		resimDosyasi[7] = "08_broom.jpg";
		resimDosyasi[8] = "09_cup.jpg";
		resimDosyasi[9] = "10_mug.jpg";
		resimDosyasi[10] = "11_spade.jpg";
		resimDosyasi[11] = "12_house.jpg";
		resimDosyasi[12] = "13_window.jpg";
		resimDosyasi[13] = "14_key.jpg";
		resimDosyasi[14] = "15_faucet.jpg";
		resimDosyasi[15] = "16_bodyParts_heart.jpg";
		resimDosyasi[16] = "17_bodyParts_kidney.jpg";
		resimDosyasi[17] = "18_bodyParts_liver.jpg";
		resimDosyasi[18] = "19_bodyParts_large_intestine.jpg";
		resimDosyasi[19] = "20_bodyParts_lungs.jpg";
		resimDosyasi[20] = "21_bodyParts_spleen.jpg";
		resimDosyasi[21] = "22_bodyParts_stomach.jpg";
		
		// 01
		secenek[0] = "keyholder";
		secenek[1] = "padlock";		// b  
		secenek[2] = "ring";
		secenek[3] = "screw";
		
		// 02
		secenek[4] = "pliers";		// a
		secenek[5] = "pin";			
		secenek[6] = "tongs";
		secenek[7] = "gripper";
		
		// 03
		secenek[8] = "filter";
		secenek[9] = "torque";
		secenek[10] = "machine";
		secenek[11] = "wood saw";	// d
		
		// 04
		secenek[12] = "doorbell";
		secenek[13] = "handbell";		// b
		secenek[14] = "snowberry";
		secenek[15] = "peppercorn";
		
		// 05
		secenek[16] = "narrow";
		secenek[17] = "sparrow";
		secenek[18] = "barrow";		
		secenek[19] = "harrow";		// d 
		
		// 06
		secenek[20] = "blue bead";	// a
		secenek[21] = "blue bird";
		secenek[22] = "blue whale";
		secenek[23] = "blue eye";
		
		// 07
		secenek[24] = "boar";
		secenek[25] = "boat";			// b  
		secenek[26] = "goat";
		secenek[27] = "coat";
		
		// 08
		secenek[28] = "broom";			// a
		secenek[29] = "groom";
		secenek[30] = "bride";
		secenek[31] = "pride";
		
		// 09
		secenek[32] = "scissors";
		secenek[33] = "fork";
		secenek[34] = "cup";				// c
		secenek[35] = "chalice";
		
		// 10
		secenek[36] = "dag";
		secenek[37] = "sag";
		secenek[38] = "hug";
		secenek[39] = "mug";				// d
		
		// 11
		secenek[40] = "spoon";
		secenek[41] = "spade";			// b  
		secenek[42] = "lattice";
		secenek[43] = "grille";
		
		// 12
		secenek[44] = "construction";
		secenek[45] = "cave";
		secenek[46] = "house";			 // c  
		secenek[47] = "barrack";
		
		// 13
		secenek[48] = "widow";
		secenek[49] = "winded";
		secenek[50] = "wildebeest";
		secenek[51] = "window";		// d
		
		// 14
		secenek[52] = "key";				// a  
		secenek[53] = "switch";
		secenek[54] = "knife";
		secenek[55] = "nail";
		
		// 15
		secenek[56] = "hose";				  
		secenek[57] = "faucet";		// b
		secenek[58] = "reservoir";
		secenek[59] = "fountain";
		
		// 16
		secenek[60] = "brain";				  
		secenek[61] = "pancreas";
		secenek[62] = "humerus";
		secenek[63] = "heart";		//d
		
		// 17
		secenek[64] = "liver";				  
		secenek[65] = "kidney";		//b
		secenek[66] = "lungs";
		secenek[67] = "knee";
		
		// 18
		secenek[68] = "stomach";				  
		secenek[69] = "lungs";
		secenek[70] = "liver";		//c
		secenek[71] = "heart";
		
		// 19
		secenek[72] = "large intestine";	//a			 
		secenek[73] = "ileum";
		secenek[74] = "colon";
		secenek[75] = "esophagus";
		
		// 20
		secenek[76] = "liver";				  
		secenek[77] = "small intestine";
		secenek[78] = "lungs";				//c
		secenek[79] = "trachea";
		
		// 21
		secenek[80] = "macrophage";				  
		secenek[81] = "spleen";		//b
		secenek[82] = "hilus";
		secenek[83] = "capsule";
		
		// 22
		secenek[84] = "serosa";				  
		secenek[85] = "cardia";
		secenek[86] = "fundus";
		secenek[87] = "stomach";	//d
		
		//23
	//	secenek[88] = "....";
	//	secenek[89] = "....";
	//	secenek[90] = "....";		// c
	//	secenek[91] = "....";
		
		cevapNo[0] = 1; 	// 01-1
		cevapNo[1] = 4; 	// 02-4
		cevapNo[2] = 11; 	// 03-11
		cevapNo[3] = 13; 	// 04-13
		cevapNo[4] = 19; 	// 05-19
		cevapNo[5] = 20; 	// 06-20
		cevapNo[6] = 25; 	// 07-25
		cevapNo[7] = 28; 	// 08-28
		cevapNo[8] = 34; 	// 09-34
		cevapNo[9] = 39; 	// 10-39
		cevapNo[10] = 41;	// 11-41
		cevapNo[11] = 46; 	// 12-46
		cevapNo[12] = 51; 	// 13-51
		cevapNo[13] = 52; 	// 14-52
		cevapNo[14] = 57; 	// 15-57
		cevapNo[15] = 63; 	// 16-63
		cevapNo[16] = 65; 	// 17-65
		cevapNo[17] = 70; 	// 18-70
		cevapNo[18] = 72; 	// 19-72
		cevapNo[19] = 78; 	// 20-78
		cevapNo[20] = 81; 	// 21-81
		cevapNo[21] = 87; 	// 22-87
	
		JPanel tekSoruluPanel = new JPanel();
		tekSoruluPanel.setLayout(null);
		
		ImageIcon sorudakiResim = new ImageIcon("src/images/"+resimDosyasi[soruNo]);
		
		JLabel labelSoruMetni = new JLabel();
		
		labelSoruMetni = new JLabel((panelNo+1) + ". What is this? ["+(soruNo+1)+"]", sorudakiResim, JLabel.CENTER);
		labelSoruMetni.setVerticalTextPosition(JLabel.TOP);
		labelSoruMetni.setHorizontalTextPosition(JLabel.CENTER);
		labelSoruMetni.setBounds(20, 20, 150, 170);  
		
		// çoktan seçmeli seçeneler
		JRadioButton rb_0 = new JRadioButton(secenek[((soruNo*4))]);
		JRadioButton rb_1 = new JRadioButton(secenek[((soruNo*4)+1)]);	
		JRadioButton rb_2 = new JRadioButton(secenek[((soruNo*4)+2)]);
		JRadioButton rb_3 = new JRadioButton(secenek[((soruNo*4)+3)]);
		
		rb_0.setActionCommand(secenek[((soruNo*4))]);
		rb_1.setActionCommand(secenek[((soruNo*4)+1)]);
		rb_2.setActionCommand(secenek[((soruNo*4)+2)]);
		rb_3.setActionCommand(secenek[((soruNo*4)+3)]);
		
		int x = 25;
		int y = 200;
		int rb_width = 140;  	// JRadioButton genislik
		int rb_height = 20;  	// JRadioButton yukseklik
		int rb_satirArasi = 18;	// JRadioButton() arasi yukseklik farki.
		
		rb_0.setBounds(x, y, rb_width, rb_height); y = y + rb_satirArasi ; 
		rb_1.setBounds(x, y, rb_width, rb_height); y = y + rb_satirArasi ;
		rb_2.setBounds(x, y, rb_width, rb_height); y = y + rb_satirArasi ;
		rb_3.setBounds(x, y, rb_width, rb_height); y = y + rb_satirArasi ;
		
		
		tekSoruluPanel.add(labelSoruMetni);
		tekSoruluPanel.add(rb_0);
		tekSoruluPanel.add(rb_1);
		tekSoruluPanel.add(rb_2);
		tekSoruluPanel.add(rb_3);
		
		secenekGroup[soruNo] = new ButtonGroup();	// Bu satir unutulursa nullPointerException hatasi alirsiniz.
		
		secenekGroup[soruNo].add(rb_0);
		secenekGroup[soruNo].add(rb_1);
		secenekGroup[soruNo].add(rb_2);
		secenekGroup[soruNo].add(rb_3);
		
		tekSoruluPanel.setBackground(new Color(223, 224, 242));
		tekSoruluPanel.setBounds(30+(220*panelNo), 30, 190, 300);
		
		return tekSoruluPanel;
		
	}
	
	public Karyatid(String isim, ArrayList<Integer> secilenSoruListesi) {
		
		ogrenciIsmi = isim;
		secilenSoruListesiKaryatid = secilenSoruListesi;
		
		JFrame frame = new JFrame();
		
		frame.add(baslikPaneli());
		frame.add(soruPaneli());
		frame.add(butonPaneli());
		
		x = x + 150 + x_baslangic;
		y = y + 20;
		
		frame.setTitle("Karyatid v2.0 | Picture Vocabulary Test. [ Java Blackboard ]");	
		frame.setSize(1024, 665);
		frame.setLayout(null);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

}