package com.unittest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections.map.LinkedMap;

import com.eassessment.model.AdminDao;
import com.eassessment.model.CoordinateDao;
import com.eassessment.pojo.Options;
import com.eassessment.pojo.Questions;
import com.eassessment.pojo.Topics;
import com.eassessment.util.Constants;
import com.eassessment.util.StringUtil;

public class ReadCVS {

	public static void main(String[] args) {

		// for (int i = 0; i < 5; i++) {
		//
		// System.out.println( Long.toHexString((new Date()).getTime()));
		// String str=(new Date()).toString();
		// try {
		// //System.out.println(str+StringUtil.toSHA256(str));
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }}

		// List<String> list = new ArrayList<String>();
		// List<String> arrlist = new ArrayList<String>();
		// String[] str = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
		// "K",
		// "L", "M", "N" };
		// list = Arrays.asList(str);
		// Map<Integer, String> map = new LinkedHashMap<Integer, String>();
		//
		// Map<Integer, String> map2 = new LinkedHashMap<Integer, String>();
		// for (int i = 0; i < list.size(); i++) {
		// // System.out.println( ((int)(Math.random()*10)));
		// map.put(i, list.get(i));
		// }
		// int n = map.size();
		// int count = 1;
		// while (map2.size() < map.size()) {
		// int ind = ((int) (Math.random() * n));
		// // System.out.println("ind="+ind);
		// if (map.containsKey(ind)) {
		// map2.put(count++, map.get(ind));
		// System.out.println(map.get(ind));
		// // map.remove(ind);
		// }
		// // System.out.println("size="+map.size()+","+map2.size());
		// }
		// System.out.println("==>" + map2.values());
		//
		// Iterator<Entry<Integer, String>> it = map2.entrySet().iterator();
		//
		// while (it.hasNext()) {
		//
		// System.out.println(it.next());
		// }

		ReadCVS obj = new ReadCVS();
//		obj.runtopic();
//		obj.runQues();
//		obj.runOptions();

	}

	public void run2() {
		BufferedReader br = null;
		try {
			String line = "";

			br = new BufferedReader(new FileReader(new File(
					"E:\\booknewcomma.xlsx")));// "E:\\J2eeWorkspace\\emyassessment\\WebContent\\WEB-INF\\country.properties")));
			while ((line = br.readLine()) != null) {
				System.out.println("<option value='" + line + "'>" + line
						+ "</option>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void runtopic() {

		String csvFile = "E:\\topic.txt";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = "\t";
		int i = 1;
		try {

			Map<String, String> maps = new HashMap<String, String>();

			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {

				// use comma as separator
				String[] arr = line.split(cvsSplitBy);

				// maps.put(country[4], country[5]);
				String responseString = "";
				try {
					Topics topic = new Topics();
					topic.setTopic_id(Integer.parseInt(arr[0]));
					topic.setTopic_name(arr[1]);

					try {

						responseString = CoordinateDao.getInstance().saveTopic(
								topic);
					} catch (Exception e) {
						e.printStackTrace();
					}
					System.out.println("responseString:" + responseString);

				} catch (Exception e) {

				}

				// if (i > 88)
				// break;
			}

		} catch (FileNotFoundException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} catch (IOException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} finally {
			System.out.println(">>" + line);
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// e.printStackTrace();
				}
			}
		}

		System.out.println("Done");
	}

	public void runQues() {

		String csvFile = "E:\\question.txt";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = "\t";
		int current = 0;
		int prev = 0;
		try {

			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {

				// use comma as separator
				String[] arr = line.split(cvsSplitBy);

				// maps.put(country[4], country[5]);
				String responseString = "";
				try {
					current = Integer.parseInt(arr[1]);
				} catch (Exception e) {

				}
				try {

					if (current != prev) {
						Topics topic = AdminDao.getInstance().getTopic(arr[0]);
						Questions question = new Questions();
						question.setQuestion_id(current);
						question.setQuestion_name(arr[4]);
						question.setQuestion_type(arr[3]);
						question.setSub_topic_from_book(arr[2]);
						question.setTopic(topic);

						try {

							responseString += CoordinateDao.getInstance()
									.saveQuestion(question);
						} catch (Exception e) {
							e.printStackTrace();
						}
						System.out.println("responseString:" + responseString);

					}

				} catch (Exception e) {

				}
				prev = current;
				// if (i > 88)
				// break;
			}

		} catch (FileNotFoundException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} catch (IOException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} finally {
			System.out.println(">>" + line);
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// e.printStackTrace();
				}
			}
		}

		System.out.println("Done");
	}

	public void runOptions() {

		String csvFile = "E:\\Assessmentdata\\o7.txt";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = "\t";
		int current = 0;
		int prev = 0;
		try {

			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {

				// use comma as separator
				String[] arr = line.split(cvsSplitBy);
//				System.out.println(arr[3]+")arr len"+arr.length);
//				if(true) continue;
				// maps.put(country[4], country[5]);
				String responseString = "";
				String opt_name = "";
				String ques_no="";
				try {
					// current = Integer.parseInt(arr[1]);
					opt_name = arr[7];
					ques_no=arr[3];
				} catch (Exception e) {

				}
				try {

					if (!ques_no.trim().equals("")&&!opt_name.trim().equals("")&&opt_name != null && opt_name.length() > 1 && !opt_name.trim().equals("Options")) {

						Questions question = AdminDao.getInstance()
								.getQuestion(ques_no);
						if (question != null) {
							String ques_type = question.getQuestion_type();

							Options opt = new Options();
							//opt.setOptionId(Integer.parseInt(arr[54]));
							opt.setQuestions(question);
							
							opt.setOption_name(opt_name);
							double mark = Double.parseDouble(arr[9-1]);
							String out_asses_test = StringUtil
									.getEmptyString(arr[10-1]);
							String personality_traits = StringUtil
									.getEmptyString(arr[11-1]);
							String user_input = StringUtil
									.getEmptyString(arr[12-1]);
							String strengths = StringUtil
									.getEmptyString(arr[13-1]);
							String explore_initiate = StringUtil
									.getEmptyString(arr[14-1]);
							String need_coaching_guide = StringUtil
									.getEmptyString(arr[15-1]);
							String self_improvement_opportunities = StringUtil
									.getEmptyString(arr[16-1]);
							String advantages_conclusion = StringUtil
									.getEmptyString(arr[17-1]);

							opt.setOption_name(opt_name);
							opt.setMarks(mark);
							opt.setOutput_for_assessment_test(out_asses_test);
							opt.setPersonality_traits(personality_traits);
							opt.setUser_input(user_input);
							opt.setStrengths(strengths);
							opt.setExplore_initiate(explore_initiate);
							opt.setNeed_coaching_guidance(need_coaching_guide);
							opt.setSelf_improvement_opportunities(self_improvement_opportunities);
							opt.setAdvantages_you_have_got(advantages_conclusion);

							if (ques_type.equals("A")) {
								opt.setSelf_organized(StringUtil
										.getEmptyDouble(arr[18-1]));
								opt.setSelf_motivated(StringUtil
										.getEmptyDouble(arr[19-1]));
								opt.setProper_coaching(StringUtil
										.getEmptyDouble(arr[20-1]));
								opt.setGuidance(StringUtil
										.getEmptyDouble(arr[21-1]));
								opt.setEnviornment(StringUtil
										.getEmptyDouble(arr[22-1]));
								opt.setEncouragement(StringUtil
										.getEmptyDouble(arr[23-1]));
								opt.setHard_work(StringUtil
										.getEmptyDouble(arr[24-1]));
							} else if (ques_type.equals("B")) {

								opt.setSleeping_habits(StringUtil
										.getEmptyDouble(arr[25-1]));
								opt.setExercise(StringUtil
										.getEmptyDouble(arr[26-1]));
								opt.setMorning_person(StringUtil
										.getEmptyDouble(arr[27-1]));
							} else if (ques_type.equals("C")) {
								opt.setBalanced_diet(StringUtil
										.getEmptyDouble(arr[28-1]));
								opt.setRight_quantity(StringUtil
										.getEmptyDouble(arr[29-1]));
								opt.setSpecfied_time(StringUtil
										.getEmptyDouble(arr[30-1]));
							} else if (ques_type.equals("D")) {
								opt.setFixed_study_schedule(StringUtil
										.getEmptyDouble(arr[31-1]));
								opt.setHobbies(StringUtil
										.getEmptyDouble(arr[32-1]));
								opt.setEntertainment(StringUtil
										.getEmptyDouble(arr[33-1]));
								
							} else if (ques_type.equals("E")) {
								opt.setAchivers(StringUtil
										.getEmptyString(arr[50-1]));
								opt.setBelievers(StringUtil
										.getEmptyString(arr[51-1]));
								opt.setAnalysers(StringUtil
										.getEmptyString(arr[52-1]));
								opt.setNon_conformist(StringUtil
										.getEmptyString(arr[53-1]));
								opt.setRadical(StringUtil
										.getEmptyString(arr[54-1]));
							} else if (ques_type.equals("F")) {	
								opt.setSelf_leaner(StringUtil
										.getEmptyDouble(arr[34-1]));
								opt.setAwarness(StringUtil
										.getEmptyDouble(arr[35-1]));
								opt.setReady_to_change(StringUtil
										.getEmptyDouble(arr[36-1]));
								
							} else if (ques_type.equals("G")) {
								opt.setCq(StringUtil.getEmptyDouble(arr[37-1]));
								opt.setEq(StringUtil.getEmptyDouble(arr[38-1]));
								opt.setAq(StringUtil.getEmptyDouble(arr[39-1]));
								opt.setIq(StringUtil.getEmptyDouble(arr[40-1]));

							} else if (ques_type.equals("H")) {

								opt.setYes(StringUtil.getEmptyDouble(arr[41-1]));
								opt.setNot_sure(StringUtil
										.getEmptyDouble(arr[42-1]));
								opt.setNo(StringUtil.getEmptyDouble(arr[43-1]));
								
							} else if (ques_type.equals("I")) {
								opt.setCoaching_classes(StringUtil
										.getEmptyString(arr[44-1]));
								opt.setCrash_course(StringUtil
										.getEmptyString(arr[45-1]));
								opt.setPrivate_tuition(StringUtil
										.getEmptyString(arr[46-1]));
								opt.setCrash_course_coaching_classes(StringUtil
										.getEmptyString(arr[47-1]));
								opt.setTrues(StringUtil.getEmptyString(arr[48-1]));
								opt.setFalses(StringUtil
										.getEmptyString(arr[49-1]));
							} else if (ques_type.equals("I")) {
								
							}
							try {
								responseString = CoordinateDao.getInstance()
										.saveOption(opt);
							} catch (Exception e) {
								e.printStackTrace();
							}
							System.out.println("responseString:"
									+ responseString);
						}
					}

				} catch (Exception e) {
e.printStackTrace();
				}
				// prev=current;
				// if (i > 88)
				// break;
			}

		} catch (FileNotFoundException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} catch (IOException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} finally {
			System.out.println(">>" + line);
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// e.printStackTrace();
				}
			}
		}

		System.out.println("Done");
	}

	public void run() {

		String csvFile = "E:\\kk.csv";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = "\t";
		int i = 1;
		try {

			Map<String, String> maps = new HashMap<String, String>();

			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {

				// use comma as separator
				String[] arr = line.split(cvsSplitBy);

				// maps.put(country[4], country[5]);
				String responseString = "------------------------";
				try {
					String opt_name = "";
					String ques_id = "";
					String ques_name = "";
					String ques_type = "";
					String topic_id = "";
					try {
						ques_type = arr[0];
						ques_name = arr[6];
						ques_id = arr[4];
						opt_name = arr[8];
						topic_id = arr[1];
						System.out.println("Questions::::" + ques_id);
					} catch (Exception e) {

					}
					if ((opt_name.trim().length() > 15
							|| opt_name.trim().equalsIgnoreCase("TRUE") || opt_name
							.trim().equalsIgnoreCase("FALSE"))
							&& ques_type.trim().length() == 1) {
						System.out.println(arr[7] + ")" + line);
						Topics topic = new Topics();
						topic.setTopic_id(Integer.parseInt(topic_id));
						topic.setTopic_name(arr[2]);

						Questions question = new Questions();
						question.setQuestion_id(Integer.parseInt(ques_id));
						question.setQuestion_name(ques_name);
						question.setQuestion_type(ques_type);
						question.setSub_topic_from_book(arr[5]);
						question.setTopic(topic);

						try {
							if (AdminDao.getInstance().getTopic(topic_id) == null) {
								responseString = CoordinateDao.getInstance()
										.saveTopic(topic);
							}
						} catch (Exception e) {
							// TODO: handle exception
						}
						try {
							if (AdminDao.getInstance().getQuestion(ques_id) == null) {
								responseString += CoordinateDao.getInstance()
										.saveQuestion(question);
							}
						} catch (Exception e) {
							// TODO: handle exception
						}

						// Options opt = new Options();
						// double mark = Double.parseDouble(arr[7]);
						// String out_asses_test = StringUtil
						// .getEmptyString(arr[8]);
						// String personality_traits = StringUtil
						// .getEmptyString(arr[9]);
						// String user_input =
						// StringUtil.getEmptyString(arr[10]);
						// String strengths =
						// StringUtil.getEmptyString(arr[11]);
						// String explore_initiate = StringUtil
						// .getEmptyString(arr[12]);
						// String need_coaching_guide = StringUtil
						// .getEmptyString(arr[13]);
						// String self_improvement_opportunities = StringUtil
						// .getEmptyString(arr[14]);
						// String advantages_conclusion = StringUtil
						// .getEmptyString(arr[15]);
						//
						// double SELF_ORGANIZED =
						// Constants.EassessmentVar.SELF_ORGANIZED
						// * mark;
						// double SELF_MOTIVATED =
						// Constants.EassessmentVar.SELF_MOTIVATED
						// * mark;
						// double PROPER_COACHING =
						// Constants.EassessmentVar.PROPER_COACHING
						// * mark;
						// double GUIDANCE = Constants.EassessmentVar.GUIDANCE
						// * mark;
						// double ENVIORNMENT =
						// Constants.EassessmentVar.ENVIORNMENT
						// * mark;
						// double ENCOURAGEMENT =
						// Constants.EassessmentVar.ENCOURAGEMENT
						// * mark;
						// double HARD_WORK = Constants.EassessmentVar.HARD_WORK
						// * mark;
						//
						// opt.setOption_name(opt_name);
						// opt.setMarks(mark);
						// opt.setOutput_for_assessment_test(out_asses_test);
						// opt.setPersonality_traits(personality_traits);
						// opt.setUser_input(user_input);
						// opt.setStrengths(strengths);
						// opt.setExplore_initiate(explore_initiate);
						// opt.setNeed_coaching_guidance(need_coaching_guide);
						// opt.setSelf_improvement_opportunities(self_improvement_opportunities);
						// opt.setAdvantages_you_have_got(advantages_conclusion);
						//
						// opt.setSelf_organized(SELF_ORGANIZED);
						// opt.setSelf_motivated(SELF_MOTIVATED);
						// opt.setProper_coaching(PROPER_COACHING);
						// opt.setGuidance(GUIDANCE);
						// opt.setEnviornment(ENVIORNMENT);
						// opt.setEncouragement(ENCOURAGEMENT);
						// opt.setHard_work(HARD_WORK);
						//
						// opt.setQuestions(AdminDao.getInstance().getQuestion(
						// ques_id));
						// String responseString = CoordinateDao.getInstance()
						// .saveOption(opt);

						System.out.println("responseString:" + responseString);

					}

				} catch (Exception e) {

				}

				// if (i > 88)
				// break;
			}

			// loop map
			// for (Map.Entry<String, String> entry : maps.entrySet()) {
			//
			// System.out.println("Country [code= " + entry.getKey() +
			// " , name="
			// + entry.getValue() + "]");
			//
			// }

		} catch (FileNotFoundException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} catch (IOException e) {
			System.out.println(">>" + line);
			// e.printStackTrace();
		} finally {
			System.out.println(">>" + line);
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// e.printStackTrace();
				}
			}
		}

		System.out.println("Done");
	}

}