package com.nisum.mytime.controllertest;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nisum.mytime.controller.ProjectController;
import com.nisum.mytime.model.Account;
import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.model.Project;
import com.nisum.mytime.repository.AccountRepo;
import com.nisum.mytime.service.ProjectService;
import com.nisum.mytime.service.UserService;

public class ProjectControllerTest {

    @Mock
    UserService userService;

    @Mock
    ProjectService projectService;

    @Mock
    AccountRepo accountRepo;

    @InjectMocks
    ProjectController projectController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(projectController).build();
    }

    @Test
    public void testgetEmployeeRole() throws Exception {
    	EmployeeRoles employeesRole = new EmployeeRoles();
    	employeesRole.setEmailId("bsatyanarayana@nisum.com");
        when(userService.getEmployeesRole("bsatyanarayana@nisum.com"))
                .thenReturn(employeesRole);
        mockMvc.perform(
                get("/project/employee").param("emailId", "bsatyanarayana@nisum.com"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(userService).getEmployeesRole("bsatyanarayana@nisum.com");
    }

    @Test
    public void testaddProject() throws Exception {
        List<String> list = new ArrayList<>();
        list.add("16101");
        list.add("16102");
        list.add("16103");
        
        Project project = new Project();
        project.setProjectName("Test");
        project.setDomainId("DOM001");
        project.setAccountId("Acc001");
        project.setStatus("Active");
        project.setManagerIds(list);
        
        
        
        String jsonvalue = (new ObjectMapper()) .writeValueAsString(project).toString();
        
        Account account = new Account(new ObjectId("5a4f03661dca211ea7f94c02"),"Acc001", "Macys", 1, "Active","Hyderabad","Retail",list);
        when(projectService.addProject(project)).thenReturn(project);
        when(accountRepo.findByAccountName("Macys")).thenReturn(account);
        
        mockMvc.perform(post("/project/addProject")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonvalue))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }

    @Test
    public void testupdateEmployeeRole() throws Exception {
        List<String> list1 = new ArrayList<>();
        list1.add("16104");
        list1.add("16105");
        list1.add("16106");
        
       // Project project = new Project(new ObjectId("5b697949eb429617f41df26b"),"Nisum0000","Bench","Nisum","Active",list1,list1,"Acc001","DOM001",null,null,list1);
        Project project = new Project();
        project.setProjectId("Nisum0000");
        project.setDomainId("DOM001");
        project.setAccountId("Acc001");
        
        project.setEmployeeIds(list1);
        project.setDeliveryLeadIds(list1);
        
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(project);
        when(projectService.updateProject(any())).thenReturn(project);
       // when(projectService.updateProject(project)).thenReturn(project);
       /* mockMvc.perform(post("/project/updateProject")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonString))
                .andExpect(MockMvcResultMatchers.status().isOk());*/
        //verify(projectService).updateProject(any());
        verify(projectService).updateProject(project);
    }

    @Test
    public void testdeleteProject() throws Exception {
        mockMvc.perform(
                delete("/project/deleteProject").param("projectId", "101"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(projectService).deleteProject("101");
    }

    @Test
    public void testgetProjects() throws Exception {
    	List<HashMap<Object,Object>> projects = CreateProjectDetails();
        when(projectService.getProjects()).thenReturn(projects);
        mockMvc.perform(get("/project/getProjects")
                .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(projectService).getProjects();
    }

    @Test
    public void testgetEmployeeRoleData() throws Exception {
    	 EmployeeRoles employeesRole = new EmployeeRoles("5b307d7e708ef705c4ca6429", "16999", "B.V.S Satyanarayana", "bsatyanarayana@nisum.com", "Employee", "Employee", "", 
          		"Java J2EE", null, "Hyderabad", "java, Spring, SpringBoot", "9848012345", "9848012345", 
          		"test@test.com", "ES", "Active", "Full Time", new Date(), new Date(), "Male", null, null, null, null, new Date(), new Date(), null, "16000", "16000");
       
    	 when(userService.getEmployeesRoleData("16127")) .thenReturn(employeesRole);
        mockMvc.perform(
                get("/project/getEmployeeRoleData").param("empId", "16127")
                        .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(userService).getEmployeesRoleData("16127");
    }

    private List<HashMap<Object,Object>>CreateProjectDetails() {
    	List<HashMap<Object,Object>> data = new ArrayList<HashMap<Object,Object>> ();
    	HashMap<Object,Object> map1 = new HashMap<Object,Object>();
    	HashMap<Object,Object> map2 = new HashMap<Object,Object>();
    	
        Project data1 = new Project();
        data1.setId(new ObjectId("5976ef15874c902c98b8a05d"));
        data1.setProjectId("101");
        data1.setProjectName("MOSAIC");
        data1.setStatus("Billable");
        
        List<String> list = new ArrayList<>();
        list.add("16101");
        list.add("16102");
        list.add("16103");
        
        data1.setEmployeeIds(list);
        map1.put(new ObjectId("5976ef15874c902c98b8a05d"), data1);

        Project data2 = new Project();
        data2.setId(new ObjectId("9976ef15874c902c98b8a05d"));
        data2.setProjectId("102");
        data2.setProjectName("OMS");
        data2.setStatus("Non-Billable");
        List<String> lists = new ArrayList<>();
        lists.add("16104");
        lists.add("16105");
        lists.add("16106");
        data2.setEmployeeIds(lists);
        map2.put(new ObjectId("9976ef15874c902c98b8a05d"), data2);

        data.add(map1);
        data.add(map2);

        return data;
    }
}
