package com.nisum;

import com.nisum.config.AppConfig;
import com.nisum.helper.EmployeeHelper;
import com.nisum.model.Employee;
import com.nisum.model.JobRole;
import com.nisum.model.impl.Contractor;
import com.nisum.model.impl.EmployeeImp;
import com.nisum.model.impl.JobRoleImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
//        traditionalApplicationFlow();
//        applicationContextXmlConfig();
//        applicationContextAnnotation();
    }


    public static void applicationContextAnnotation() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        EmployeeHelper employeeHelper = ctx.getBean(EmployeeHelper.class);

        Employee emp1 = employeeHelper.createSoftwareEngineerEmployee("Vlad", "Rez");
        Employee emp2 = employeeHelper.createSoftwareEngineerEmployee("John", "Smith");
        List<Employee> employeeList = Arrays.asList(emp1, emp2);
        employeeHelper.displayTeam(employeeList);

    }

    public static void applicationContextXmlConfig() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");

        Employee emp1 = ctx.getBean("employeeJohn", EmployeeImp.class);
        Employee emp2 = ctx.getBean("employeeWalter", EmployeeImp.class);
        Employee emp3 = ctx.getBean("employeeJane", EmployeeImp.class);

        List<Employee> employeeList = Arrays.asList(emp1, emp2, emp3);

        String techTeam = employeeList.stream().map(employee ->
            String.format("%s - %s", employee.getDisplayName(), employee.getJobRole().getTitle()))
            .collect(Collectors.joining("\n"));
        System.out.println(techTeam);

        Contractor contractor1 = ctx.getBean("jobRoleContractor", Contractor.class);
        Contractor contractor2 = ctx.getBean("jobRoleContractor", Contractor.class);
        JobRole contractor3 = ctx.getBean("jobRoleContractor", Contractor.class);
        JobRoleImp jobRoleImp = ctx.getBean("jobRoleSoftwareEngineerI", JobRoleImp.class);

        jobRoleImp.setTitle("Sales");

        String techTeam2 = employeeList.stream().map(employee ->
            String.format("%s - %s", employee.getDisplayName(), employee.getJobRole().getTitle()))
            .collect(Collectors.joining("\n"));


        System.out.println(techTeam2);

        contractor1.setTitle("Accounting");

        System.out.printf("\n%s\n%s", contractor1.getTitle(), contractor2.getTitle());



    }


    public static void traditionalApplicationFlow() {
        // Traditional Application Flow
        JobRoleImp jobRole1 = new JobRoleImp("Software Engineer I");
        JobRoleImp jobRole2 = new JobRoleImp("Software Engineer II");
        JobRoleImp jobRole3 = new JobRoleImp("Tech Lead");
        EmployeeImp employee1 = new EmployeeImp();
        employee1.setFirstName("John");
        employee1.setLastName("Smith");
        employee1.setJobRole(jobRole1);
        EmployeeImp employee2 = new EmployeeImp();
        employee2.setFirstName("Walter");
        employee2.setLastName("Bell");
        employee2.setJobRole(jobRole2);
        EmployeeImp employee3 = new EmployeeImp();
        employee3.setFirstName("Jane");
        employee3.setLastName("Doe");
        employee3.setJobRole(jobRole3);

        System.out.printf("\n%s %s %s\n",
            employee1.getDisplayName(),
            employee2.getDisplayName(),
            employee3.getDisplayName());
    }
}
