package com.codegym.task.task08.task0816;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.*;


/*
TITLE OF EXERCISE: Kind Emma and the summer holidays
INSTRUCTIONS
Create a Map<String, Date> and add ten entries that represent (last name, birth date) pairs.
Remove from the map all people born in the summer.
Hint: At CodeGym, summer lasts from June 1 to August 31.

Requirements:
The program should not display text on the screen.
The program should not read values from the keyboard.
The createMap() method must create and return a HashMap that has (String, Date) elements and contains 10 entries.
The removeAllSummerPeople() method should remove from the map all people born in the summer.

*/
public class Solution {
    public static HashMap<String, Date> createMap() throws ParseException {
        DateFormat df = new SimpleDateFormat("MMMMM d yyyy", Locale.ENGLISH);
        HashMap<String, Date> map = new HashMap<String, Date>();
        map.put("Stallone", df.parse("JUNE 1 1980"));

        //write your code here
        map.put("Rivermeade", df.parse("JULY 20 2004"));
        map.put("Sanders", df.parse("JANUARY 5 1974"));
        map.put("Smith", df.parse("FEBRUARY 12 1989"));
        map.put("Rawlston", df.parse("MARCH 18 2010"));
        map.put("Peters", df.parse("AUGUST 8 1975"));
        map.put("James", df.parse("SEPTEMBER 25 1963"));
        map.put("Jones", df.parse("JULY 1 1954"));
        map.put("Lyttle", df.parse("MAY 18 1998"));
        map.put("Ferdinand", df.parse("OCTOBER 11 2017"));

        return map;
    }

    public static void removeAllSummerPeople(HashMap<String, Date> map) {
        Set<Map.Entry<String, Date>> mapEntries = map.entrySet();

         //get the iterator
        Iterator<Map.Entry<String,Date>> mapIterator = mapEntries.iterator();

        /*
        create a formatter to get the "month part" of the current map entry Date
        value
        if <3 characters are used to specify the format, a number is returned
        else a string is returned
        */
        DateFormat formatTheDate = new SimpleDateFormat("MM", Locale.ENGLISH);


        //iterate through the map
        while(mapIterator.hasNext()){
            //get the current map entry
            Map.Entry<String, Date> mapEntryKey = mapIterator.next();

            //get the value of the date from the current map entry
            Date valueOfCurrentMapEntry = map.get(mapEntryKey);

            //format valueOfCurrentMapEntry to get the "month part"
            String monthOfDate = formatTheDate.format(valueOfCurrentMapEntry);

			//check if the month part is equal to the numbers for June = 06, July = 07, and
			//August
            if(monthOfDate == "6" || monthOfDate == "7" || monthOfDate == "8"){
                mapIterator.remove();
            }

            mapIterator.next();
        }

        return;

    }

    public static void main(String[] args) throws ParseException {
        HashMap<String, Date> newMap = createMap();
        removeAllSummerPeople(newMap);
        /*
            for(Map.Entry<String, Date> eachEntry: newMap.entrySet()) {
            System.out.format("key: %s, value: %d%n", eachEntry.getKey(), eachEntry.getValue());
        }
        */

    }
}