import unittest
from ui.UI import *
from repository.repo import *
from controller.controller import *

class Tests(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.persCtr = PersonController(PersonsRepository(), mapRepository)
        self.actCtr = ActivityController(ActivityRepository())

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def testCreatePersons(self):
        person1 = Persons("1", "Ion", "123", "Voinicenilor")
        person2 = Persons("2", "Vasile", "333", "Manastur")
        person3 = Persons("3", "Gheorghe", "911", "Slanic")
        self.assertEqual(person1.getID(), "1")
        self.assertEqual(person2.getID(), "2")
        self.assertEqual(person2.GetName(), "Vasile")
        self.assertEqual(person1.GetName(), "Ion")
        self.assertEqual(person3.GetPhone_number(), "911")
        self.assertEqual(person2.GetPhone_number(), "333")
        self.assertEqual(person3.GetAddress(), "Slanic")
        self.assertEqual(person2.GetAddress(), "Manastur")

    def testCreateActivities(self):
        my_activity = Activities(4, '2.6', '12:42', 'fun description')
        self.assertEqual(my_activity.getID(), 4)
        self.assertEqual(my_activity.GetDate(), '2.6')
        self.assertEqual(my_activity.GetTime(), '12:42')
        self.assertEqual(my_activity.GetDescription(), 'fun description')
        my_activity.SetDate('1.12')
        self.assertEqual(my_activity.GetDate(), '1.12')
        my_activity.SetTime('14:21')
        self.assertEqual(my_activity.GetTime(), '14:21')
        my_activity.SetDescription('desc')
        self.assertEqual(my_activity.GetDescription(), 'desc')

    def testAddPerson(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        self.assertEqual(self.persCtr.addPerson(person1), 1)
        self.assertEqual(self.persCtr.addPerson(person2), 1)
        self.assertEqual(self.persCtr.addPerson(person1), 0)
        self.assertEqual(self.persCtr.addPerson(person2), 0)
        self.assertEqual(self.persCtr.addPerson(person3), 1)

    def testUpdatePerson(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(3, "Lil' Johnny", '123123', "Addr")
        self.persCtr.addPerson(person1)
        self.assertEqual(self.persCtr.updatePerson(person3), 1)
        self.assertEqual(self.persCtr.updatePerson(person2), 0)

    def testCheckPersonById(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        self.persCtr.addPerson(person1)
        self.persCtr.addPerson(person2)
        self.persCtr.addPerson(person3)
        self.assertEqual(self.persCtr.checkPersonById(3), 1)
        self.assertEqual(self.persCtr.checkPersonById(9), 0)
        self.assertEqual(self.persCtr.checkPersonById(2), 1)
        self.assertEqual(self.persCtr.checkPersonById(4), 1)
        self.assertEqual(self.persCtr.checkPersonById(13), 0)
        self.assertEqual(self.persCtr.checkPersonById(223), 0)

    def testFindPersonById(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        self.persCtr.addPerson(person1)
        self.persCtr.addPerson(person2)
        self.persCtr.addPerson(person3)
        self.assertEqual(self.persCtr.findPersonById(3), person1)
        self.assertEqual(self.persCtr.findPersonById(9), -1)
        self.assertEqual(self.persCtr.findPersonById(2), person3)
        self.assertEqual(self.persCtr.findPersonById(4), person2)
        self.assertEqual(self.persCtr.findPersonById(13), -1)
        self.assertEqual(self.persCtr.findPersonById(223), -1)

    def testGet_all(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        self.persCtr.addPerson(person1)
        self.persCtr.addPerson(person2)
        self.persCtr.addPerson(person3)
        self.assertEqual(len(self.persCtr.get_all()), 3)

    def testAddActivity(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        self.assertEqual(self.actCtr.addActivity(activity1), 1)
        self.assertEqual(self.actCtr.addActivity(activity2), 1)
        self.assertEqual(self.actCtr.addActivity(activity1), 0)
        self.assertEqual(self.actCtr.addActivity(activity2), 0)
        self.assertEqual(self.actCtr.addActivity(activity3), 1)
        self.assertEqual(self.actCtr.addActivity(activity1), 0)

    def testUpdateActivity(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(1, '5.6', '18:12', 'description3')
        self.assertEqual(self.actCtr.updateActivity(activity1), 0)
        self.actCtr.addActivity(activity1)
        self.assertEqual(self.actCtr.updateActivity(activity3), 1)
        self.assertEqual(self.actCtr.updateActivity(activity2), 0)

    def testRemoveActivities(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        self.actCtr.addActivity(activity1)
        self.actCtr.addActivity(activity2)
        self.actCtr.addActivity(activity3)
        self.assertEqual(self.actCtr.removeActivities(5), 1)
        self.assertEqual(self.actCtr.removeActivities(5), 0)
        self.assertEqual(self.actCtr.removeActivities(1), 1)
        self.assertEqual(self.actCtr.removeActivities(9), 0)
        self.assertEqual(self.actCtr.removeActivities(7), 1)
        self.assertEqual(self.actCtr.removeActivities(20), 0)

    def testFindActivityById(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        self.actCtr.addActivity(activity1)
        self.actCtr.addActivity(activity2)
        self.actCtr.addActivity(activity3)
        self.assertEqual(self.actCtr.findActivityById(1), activity1)
        self.assertEqual(self.actCtr.findActivityById(20), -1)
        self.assertEqual(self.actCtr.findActivityById(7), activity3)
        self.assertEqual(self.actCtr.findActivityById(2), -1)
        self.assertEqual(self.actCtr.findActivityById(5), activity2)
        self.assertEqual(self.actCtr.findActivityById(121), -1)

    def testGet_allActivities(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        self.assertEqual(len(self.actCtr.get_all()), 0)
        self.actCtr.addActivity(activity1)
        self.actCtr.addActivity(activity2)
        self.assertEqual(len(self.actCtr.get_all()), 2)
        self.actCtr.addActivity(activity3)
        self.assertEqual(len(self.actCtr.get_all()), 3)

    def testAddMap(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")

    def testGetActivityByDate(self):
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        activity4 = Activities(9, '2.3', '18:12', 'description3')
        activity5 = Activities(10, '2.3', '18:12', 'description3')
        self.actCtr.addActivity(activity1)
        self.actCtr.addActivity(activity2)
        self.actCtr.addActivity(activity3)
        self.actCtr.addActivity(activity4)
        self.actCtr.addActivity(activity5)
        self.assertEqual(len(self.actCtr.getActivitiesByDate("2.3")), 3)
        self.assertEqual(len(self.actCtr.getActivitiesByDate("5.6")), 1)
        self.assertEqual(len(self.actCtr.getActivitiesByDate("1.8")), 1)
        self.assertEqual(len(self.actCtr.getActivitiesByDate("2.2")), 0)
        
    def testGetPersonsByName(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        person4 = Persons(8, "Florea", '1233123123', "Addr3123")
        person5 = Persons(10, "Florin", '123143223', "Addrasda")
        person6 = Persons(15, "Lelea Floare", '123123123', "Addr1ds21")
        self.persCtr.addPerson(person1)
        self.persCtr.addPerson(person2)
        self.persCtr.addPerson(person3)
        self.persCtr.addPerson(person4)
        self.persCtr.addPerson(person5)
        self.persCtr.addPerson(person6)
        self.assertEqual(len(self.persCtr.getPersonsByName('Lawi')), 1)
        self.assertEqual(len(self.persCtr.getPersonsByName('No name')), 0)
        self.assertEqual(len(self.persCtr.getPersonsByName('Flo')), 3)
        self.assertEqual(len(self.persCtr.getPersonsByName('John')), 2)
        self.assertEqual(len(self.persCtr.getPersonsByName('Floare')), 1)
        self.assertEqual(len(self.persCtr.getPersonsByName('#coolname')), 0)

    def testDateCmp(self):
        self.assertEqual(self.persCtr.datecmp('2.4', '2.4'), 0)
        self.assertEqual(self.persCtr.datecmp('1.4', '2.4'), -1)
        self.assertEqual(self.persCtr.datecmp('2.9', '2.4'), 5)
        self.assertEqual(self.persCtr.datecmp('1.4', '7.9'), -5)
        self.assertEqual(self.persCtr.datecmp('20.10', '2.4'), 6)

    def testComputeUndo(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        person4 = Persons(8, "Florea", '1233123123', "Addr3123")
        person5 = Persons(10, "Florin", '123143223', "Addrasda")
        self.persCtr.addPerson(person1)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person2)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person3)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person4)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person5)
        self.persCtr.appendUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 5)
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 4)
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 3)
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 2)
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 1)
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 0)
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 0)

    def testComputeRedo(self):
        person1 = Persons(3, 'John', '222222', 'Address')
        person2 = Persons(4, 'Lawi', '323232', 'Another Address')
        person3 = Persons(2, "Lil' Johnny", '123123', "Addr")
        person4 = Persons(8, "Florea", '1233123123', "Addr3123")
        person5 = Persons(10, "Florin", '123143223', "Addrasda")
        self.persCtr.addPerson(person1)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person2)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person3)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person4)
        self.persCtr.appendUndo()
        self.persCtr.addPerson(person5)
        self.persCtr.appendUndo()
        self.persCtr.computeUndo()
        self.assertEqual(len(self.persCtr._repository._undo), 4)
        self.persCtr.computeRedo()
        self.assertEqual(len(self.persCtr._repository._undo), 5)
        self.persCtr.computeUndo()
        self.persCtr.computeUndo()
        self.persCtr.computeUndo()
        self.persCtr.computeRedo()
        self.assertEqual(len(self.persCtr._repository._undo), 3)

    def testSortByDate(self):
        new_list = []
        activity1 = Activities(1, '2.3', '12:42', 'description')
        activity2 = Activities(5, '1.8', '15:26', 'description2')
        activity3 = Activities(7, '5.6', '18:12', 'description3')
        activity4 = Activities(9, '2.3', '18:12', 'description3')
        activity5 = Activities(10, '2.3', '18:12', 'description3')
        new_list.append(activity1)
        new_list.append(activity2)
        new_list.append(activity3)
        new_list.append(activity4)
        new_list.append(activity5)
        new_list = self.persCtr.sortByDate(new_list)
        self.assertEqual(len(new_list), 5)
        self.assertEqual(new_list[0], activity1)
        self.assertEqual(new_list[1], activity4)
        self.assertEqual(new_list[2], activity5)
        self.assertEqual(new_list[3], activity3)
        self.assertEqual(new_list[4], activity2)

if __name__ == '__main__':
        unittest.main()
