<commit_before>package seedu.todo.controllers;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;

import seedu.todo.commons.exceptions.UnmatchedQuotesException;
import seedu.todo.commons.util.DateUtil;
import seedu.todo.commons.util.StringUtil;
import seedu.todo.controllers.concerns.Renderer;
import seedu.todo.controllers.concerns.Tokenizer;
import seedu.todo.models.TodoListDB;

public class ClearController implements Controller {
    
    private static final String NAME = "Clear";
    private static final String DESCRIPTION = "Clear all tasks/events or by specify date.";
    private static final String COMMAND_SYNTAX = "clear";
    
    private static final String MESSAGE_CLEAR_SUCCESS = "A total of %s tasks and events have been deleted!";
    private static final String MESSAGE_CLEAR_FAILURE = "Invalid format for clear command. Date entered : %s";
    
    private static CommandDefinition commandDefinition =
            new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX); 

    public static CommandDefinition getCommandDefinition() {
        return commandDefinition;
    }

    @Override
    public float inputConfidence(String input) {
        return (input.toLowerCase().startsWith(COMMAND_SYNTAX)) ? 1 : 0;
    }

    /**
     * Get the token definitions for use with <code>tokenizer</code>.<br>
     * This method exists primarily because Java does not support HashMap
     * literals...
     * 
     * @return tokenDefinitions
     */
    private static Map<String, String[]> getTokenDefinitions() {
        Map<String, String[]> tokenDefinitions = new HashMap<String, String[]>();
        tokenDefinitions.put("default", new String[] {"clear"});
        tokenDefinitions.put("eventType", new String[] { "event", "task" });
        tokenDefinitions.put("time", new String[] { "at", "by", "on", "before", "time" });
        tokenDefinitions.put("timeFrom", new String[] { "from" });
        tokenDefinitions.put("timeTo", new String[] { "to" });
        return tokenDefinitions;
    }
    
    @Override
    public void process(String input) {
        TodoListDB db = TodoListDB.getInstance();

        Map<String, String[]> parsedResult;
        try {
            parsedResult = Tokenizer.tokenize(getTokenDefinitions(), input);
        } catch (UnmatchedQuotesException e) {
            System.out.println("Unmatched quote!");
            return ;
        }
        
        String[] parsedDates = parseDates(parsedResult);
        
        //no dates provided
        if (parsedDates == null) {
            destroyAll(db);
            return;
        }
        
        String naturalOn = parsedDates[0];
        String naturalFrom = parsedDates[1];
        String naturalTo = parsedDates[2];
        // if all are null = no date provided
        
        // Parse natural date using Natty.
        LocalDateTime dateOn = naturalOn == null ? null : parseNatural(naturalOn); 
        LocalDateTime dateFrom = naturalFrom == null ? null : parseNatural(naturalFrom); 
        LocalDateTime dateTo = naturalTo == null ? null : parseNatural(naturalTo);
        
        destroyByDate(db, naturalOn, naturalFrom, naturalTo, dateOn, dateFrom, dateTo);

    }

    private void destroyByDate(TodoListDB db, String naturalOn, String naturalFrom, String naturalTo,
            LocalDateTime dateOn, LocalDateTime dateFrom, LocalDateTime dateTo) {
        if (dateOn != null) {
            destroyByDate(db, dateOn);
            return;
        } else if (dateFrom != null || dateTo != null) {
            destroyByRange(db, dateFrom, dateTo);
            return;
        } else { //natty deem all dates as invalid
            displayErrorMessage(db, naturalOn, naturalFrom, naturalTo);
        }
    }

    /**
     * clear all tasks and events of given date range that exist in the database.
     * 
     * @param TodoListDB, dateFrom, dateTo
     */
    private void destroyByRange(TodoListDB db, LocalDateTime dateFrom, LocalDateTime dateTo) {
        int totalCalendarItems = db.getEventByRange(dateFrom, dateTo).size() + db.getTaskByRange(dateFrom, dateTo).size();
        db.destroyAllEventByRange(dateFrom, dateTo);
        db.destroyAllTaskByRange(dateFrom, dateTo);
        Renderer.renderIndex(db, String.format(MESSAGE_CLEAR_SUCCESS, totalCalendarItems));
        
    }

    /**
     * display error message due to failure in parsing given date
     * 
     * @param TodoListDB, naturalOn, nautralFrom, naturalTo
     */<commit_msg>update comments for method<commit_after>package seedu.todo.controllers;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;

import seedu.todo.commons.exceptions.UnmatchedQuotesException;
import seedu.todo.commons.util.DateUtil;
import seedu.todo.commons.util.StringUtil;
import seedu.todo.controllers.concerns.Renderer;
import seedu.todo.controllers.concerns.Tokenizer;
import seedu.todo.models.TodoListDB;

public class ClearController implements Controller {
    
    private static final String NAME = "Clear";
    private static final String DESCRIPTION = "Clear all tasks/events or by specify date.";
    private static final String COMMAND_SYNTAX = "clear";
    
    private static final String MESSAGE_CLEAR_SUCCESS = "A total of %s tasks and events have been deleted!";
    private static final String MESSAGE_CLEAR_FAILURE = "Invalid format for clear command. Date entered : %s";
    
    private static CommandDefinition commandDefinition =
            new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX); 

    public static CommandDefinition getCommandDefinition() {
        return commandDefinition;
    }

    @Override
    public float inputConfidence(String input) {
        return (input.toLowerCase().startsWith(COMMAND_SYNTAX)) ? 1 : 0;
    }

    /**
     * Get the token definitions for use with <code>tokenizer</code>.<br>
     * This method exists primarily because Java does not support HashMap
     * literals...
     * 
     * @return tokenDefinitions
     */
    private static Map<String, String[]> getTokenDefinitions() {
        Map<String, String[]> tokenDefinitions = new HashMap<String, String[]>();
        tokenDefinitions.put("default", new String[] {"clear"});
        tokenDefinitions.put("eventType", new String[] { "event", "task" });
        tokenDefinitions.put("time", new String[] { "at", "by", "on", "before", "time" });
        tokenDefinitions.put("timeFrom", new String[] { "from" });
        tokenDefinitions.put("timeTo", new String[] { "to" });
        return tokenDefinitions;
    }
    
    @Override
    public void process(String input) {
        TodoListDB db = TodoListDB.getInstance();

        Map<String, String[]> parsedResult;
        try {
            parsedResult = Tokenizer.tokenize(getTokenDefinitions(), input);
        } catch (UnmatchedQuotesException e) {
            System.out.println("Unmatched quote!");
            return ;
        }
        
        String[] parsedDates = parseDates(parsedResult);
        
        //no dates provided
        if (parsedDates == null) {
            destroyAll(db);
            return;
        }
        
        String naturalOn = parsedDates[0];
        String naturalFrom = parsedDates[1];
        String naturalTo = parsedDates[2];
        // if all are null = no date provided
        
        // Parse natural date using Natty.
        LocalDateTime dateOn = naturalOn == null ? null : parseNatural(naturalOn); 
        LocalDateTime dateFrom = naturalFrom == null ? null : parseNatural(naturalFrom); 
        LocalDateTime dateTo = naturalTo == null ? null : parseNatural(naturalTo);
        
        destroyByDate(db, naturalOn, naturalFrom, naturalTo, dateOn, dateFrom, dateTo);

    }
    
    /**
     * Clear all tasks and events by a single or a range of date that exist in the database.
     * 
     * @param db
     *            TodoListDB object
     * @param naturalOn, naturalFrom, naturalTo
     *            null if not entered, or natural date that user has entered
     * @param dateOn, dateFrom
     *            null if parsing failed or Due date for Task or start date for Event
     * @param dateTo
     *            null if parsing failed or End date for Event
     */
    private void destroyByDate(TodoListDB db, String naturalOn, String naturalFrom, String naturalTo,
            LocalDateTime dateOn, LocalDateTime dateFrom, LocalDateTime dateTo) {
        if (dateOn != null) {
            destroyByDate(db, dateOn);
            return;
        } else if (dateFrom != null || dateTo != null) {
            destroyByRange(db, dateFrom, dateTo);
            return;
        } else { //natty deem all dates as invalid
            displayErrorMessage(db, naturalOn, naturalFrom, naturalTo);
        }
    }

    /**
     * clear all tasks and events of given date range that exist in the database.
     * 
     * @param TodoListDB, dateFrom, dateTo
     */
    private void destroyByRange(TodoListDB db, LocalDateTime dateFrom, LocalDateTime dateTo) {
        int totalCalendarItems = db.getEventByRange(dateFrom, dateTo).size() + db.getTaskByRange(dateFrom, dateTo).size();
        db.destroyAllEventByRange(dateFrom, dateTo);
        db.destroyAllTaskByRange(dateFrom, dateTo);
        Renderer.renderIndex(db, String.format(MESSAGE_CLEAR_SUCCESS, totalCalendarItems));
        
    }

    /**
     * display error message due to failure in parsing given date
     * 
     * @param TodoListDB, naturalOn, nautralFrom, naturalTo
     */
{'fraction_non_alphanumeric': 0.09688934217236105, 'fraction_numerical': 0.0010198878123406426, 'mean_word_length': 2.794891640866873, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '934049', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2650, 'n_words': 756}