package com.dstsystems.hackathon.autotempo.main;

import com.dstsystems.hackathon.autotempo.models.*;
import com.dstsystems.hackathon.autotempo.service.AppointmentServiceImpl;
import com.dstsystems.hackathon.autotempo.service.FirstMatchRuleSetProcessor;
import com.dstsystems.hackathon.autotempo.service.UserProfileService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by user on 06/11/2015.
 */
public class AutoTempoApp {

    public static final String DEFAULT_USER_PROFILE_PATH = "/user.profile";

    private static Date generateDateFromString(String dateInString ) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date date = new Date();
        try {
            date = sdf.parse(dateInString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static void main(String[] args) {

        AppointmentServiceImpl appointmentService = new AppointmentServiceImpl();
        UserProfileService userProfileService = new UserProfileService();

        String userProfilePath = DEFAULT_USER_PROFILE_PATH;

        if (args.length > 0 && args[0] != null) {
            userProfilePath = args[0];
        }

        // To get appointments.
        ExchangeUserProfileModel xchangeUserProfile = userProfileService.getExChangeUserProfile(userProfilePath);
        Date startDate = generateDateFromString( "11/07/2015" );
        Date endDate = generateDateFromString( "11/07/2015" );
        try {
            List<AppointmentModel> appointmentList = appointmentService.downloadExchangeAppointments(xchangeUserProfile, startDate, endDate);
            RuleSet ruleSet = getSimpleRuleSet();

            for (int i = 0; i < appointmentList.size(); i++ )
            {
                AppointmentModel appointmentModel = appointmentList.get(i);

                WorklogModel worklogModel = new WorklogModel();
                new FirstMatchRuleSetProcessor().process(worklogModel, appointmentModel, ruleSet);

                System.out.println( worklogModel.getAccountKey() );

                System.out.println( worklogModel.getComment() );

                System.out.println( worklogModel.getIssueKey() );

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static RuleSet getSimpleRuleSet() {
        SimpleCategoryMappingRule rule = new SimpleCategoryMappingRule();
        rule.setComment("Test comment");
        rule.setIssueKey("INT-1");
        rule.setAccountKey("ATT02");
        rule.setCategory("INTERNAL");

        RuleSet ruleSet = new RuleSet();
        ruleSet.setRuleList(Arrays.asList((Rule) rule));
        return ruleSet;
    }


}

@@ -95,16 +95,97 @@
 dels.*;%0A
+import com.dstsystems.hackathon.autotempo.service.AcceptedAppointmentListFilter;%0A
 import c
@@ -397,16 +397,80 @@
 Service;
+%0Aimport com.dstsystems.hackathon.autotempo.tempo.TempoSubmitter;
 %0A%0Aimport
@@ -531,16 +531,44 @@
 Format;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1483,40 +1483,8 @@
  %7D%0A%0A
-        // To get appointments.%0A
     
@@ -1593,16 +1593,67 @@
 ePath);%0A
+        System.out.println(%22UserProfile loaded%22);%0A%0A
         
@@ -1778,32 +1778,33 @@
 ;%0A        try %7B%0A
+%0A
             List
@@ -1945,42 +1945,344 @@
     
-RuleSet ruleSet = getSimpleRuleSet
+System.out.println(%22Appointments Loaded%22);%0A%0A            AcceptedAppointmentListFilter acceptedAppointmentListFilter = new AcceptedAppointmentListFilter();%0A            acceptedAppointmentListFilter.filter(appointmentList);%0A%0A            RuleSet ruleSet = getSimpleRuleSet();%0A%0A            List%3CWorklogModel%3E worklogModels = new ArrayList%3C%3E
 ();%0A
@@ -2805,33 +2805,84 @@
 );%0A%0A            
-%7D
+    worklogModels.add(worklogModel);%0A            %7D%0A%0A
 %0A        %7D catch

Apply AcceptedAppointmentListFilter and add some log messages