package com.github.neunkasulle.chronocommand.control;

import com.github.neunkasulle.chronocommand.model.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDateTime;

import static org.junit.Assert.*;

/**
 * Created by Janze on 01.02.2016.
 *
 */
public class MainControlTest {

    @Before
    public void setUp() throws Exception {
        MainControl mainControl = MainControl.getInstance();

        mainControl.startup();

    }

    @After
    public void tearDown() throws Exception {

    }


    @Test
    public void testStartup() throws Exception {

    }

    @Test
    public void checkDB() {
        UserDAO userDAO = UserDAO.getInstance();

        User user = userDAO.findUser("tom");

        assertTrue(user.getEmail().equals("tom@chronocommand.eu"));
    }

    @Test
    public void testLogin() {
        LoginControl loginControl = LoginControl.getInstance();
        try {
            loginControl.login("tom", "cat", true);
        }
        catch (ChronoCommandException e) {
            fail();
        }

        assertTrue(loginControl.isLoggedIn());

    }

    @Test(expected = ChronoCommandException.class)
    public void testFailingLogin() throws Exception {
        LoginControl loginControl = LoginControl.getInstance();
        try {
            loginControl.login("tom", "dog", true);
        }
        catch (ChronoCommandException e) {
            assertTrue(e.getReason() == Reason.BADCREDENTIALS);
            throw e;
        }
    }

    @Test(expected = ChronoCommandException.class)
    public void testFailingCategory() throws Exception {
        TimeSheetControl timeSheetControl = TimeSheetControl.getInstance();
        UserDAO userDAO = UserDAO.getInstance();

        try {
            timeSheetControl.addTimeToSheet(LocalDateTime.now(), LocalDateTime.now(), new Category("FUU"), "BAR" , userDAO.findUser("tom"));
        }
        catch (ChronoCommandException e) {
            assert(e.getReason() == Reason.CATEGORYNOTFOUND);
            throw e;
        }

    }

    @Test
    public void testRightTimeRecordAdd() {
        TimeSheetControl timeSheetControl = TimeSheetControl.getInstance();
        UserDAO userDAO = UserDAO.getInstance();
        Category category = CategoryDAO.getInstance().findCategoryByString("Programming");
        try {
            timeSheetControl.newTimeRecord(category, "testing", userDAO.findUser("tom"));
            timeSheetControl.closeTimeRecord(userDAO.findUser("tom"));
        }
        catch (ChronoCommandException e) {
            assert false;
        }
        try {
            timeSheetControl.newTimeRecord(userDAO.findUser("tom"));
            timeSheetControl.closeTimeRecord(category, "testing", userDAO.findUser("tom"));
             //       LocalDateTime.now(), );
        }
        catch (ChronoCommandException e) {
            assert false;
        }
        assert true;

    }

    @Test(expected = ChronoCommandException.class)
    public void testFalseTimeRecordAdd() throws Exception {
        TimeSheetControl timeSheetControl = TimeSheetControl.getInstance();
        UserDAO userDAO = UserDAO.getInstance();
        Category category = CategoryDAO.getInstance().findCategoryByString("Programming");

        try {
            timeSheetControl.newTimeRecord(category, "testing", userDAO.findUser("tom"));
            timeSheetControl.closeTimeRecord(category, "testing", userDAO.findUser("tom"));
            assert false;

        }
        catch (ChronoCommandException e) {
            try {
                timeSheetControl.newTimeRecord(userDAO.findUser("tom"));
                timeSheetControl.closeTimeRecord(userDAO.findUser("tom"));
                assert false;
            }
            catch (ChronoCommandException exc) {
                assert true;
            }
        }
    }

}
@@ -2945,50 +2945,9 @@
 Test
-(expected = ChronoCommandException.class)
 %0A
+
     
@@ -3276,37 +3276,16 @@
 eRecord(
-category, %22testing%22, 
 userDAO.
@@ -3347,37 +3347,16 @@
 eRecord(
-category, %22testing%22, 
 userDAO.
@@ -3399,17 +3399,16 @@
  false;%0A
-%0A
         
@@ -3413,265 +3413,8 @@
   %7D%0A
-        catch (ChronoCommandException e) %7B%0A            try %7B%0A                timeSheetControl.newTimeRecord(userDAO.findUser(%22tom%22));%0A                timeSheetControl.closeTimeRecord(userDAO.findUser(%22tom%22));%0A                assert false;%0A            %7D%0A    
     
@@ -3462,28 +3462,24 @@
             
- 
-   
 assert true;
@@ -3483,22 +3483,8 @@
 ue;%0A
-            %7D%0A
     

fix unit tests