import grails.util.GrailsUtil

class BootStrap 
{
    //Required to be right here for Acegi plugin.
    def authenticateService
    Boolean BootStrapDemoDataSuccessful = true

    def init = { servletContext ->

    println "**** BootStrap GrailsUtil.environment = ${GrailsUtil.environment}"
    
        switch (GrailsUtil.environment)
        {
            case "development":
                        bootStrapDemoData()
                        break
            case "test":
                        break
            case "production":
                        bootStrapDemoData()
                        break 
        }
    
    }

    def destroy = {
    }

    //Insert some demo/startup data.
    void bootStrapDemoData()
    {
        println "BootStrapping demo data..."
    
        //TypeOfPersonGroup
//         new PersonGroupType(name:"Department").save()
//         new PersonGroupType(name:"Contractor").save()
//         new PersonGroupType(name:"ProjectTeam").save()
    
        //PersonGroup
//         new PersonGroup(personGroupType:PersonGroupType.findByName("Department"),
//                         name:"Electrical").save()
//         new PersonGroup(personGroupType:PersonGroupType.get(2),
//                         name:"Kewl AirCon Guys").save()
//         new PersonGroup(personGroupType:PersonGroupType.get(3),
//                         name:"gnuMims").save()

        //Authority
        def authInstance

        authInstance = new Authority(description:"Application Admin",
                                        authority:"ROLE_ADMIN")
        BootStrapSaveAndTest(authInstance)

        authInstance = new Authority(description:"Application Admin",
                                        authority:"ROLE_USER")
        BootStrapSaveAndTest(authInstance)
            
        //Person
        def passwordEncoded = authenticateService.encodePassword("pass")
        def personInstance

        personInstance = new Person(loginName:"admin",
                                    firstName:"Admin",
                                    lastName:"Powers",
                                    password:passwordEncoded,
                                    email:"admin@example.com")
        BootStrapSaveAndTest(personInstance)
        personInstance.addToAuthorities(Authority.get(1))
        personInstance.addToAuthorities(Authority.get(2))

        personInstance = new Person(loginName:"user",
                                    firstName:"Demo",
                                    lastName:"Danza",
                                    password:passwordEncoded,
                                    email:"user@example.com")
        BootStrapSaveAndTest(personInstance)
        personInstance.addToAuthorities(Authority.get(2))

        personInstance = new Person(loginName:"craig",
                                    firstName:"Craig",
                                    lastName:"SuperTech",
                                    password:passwordEncoded,
                                    email:"user@example.com")
        BootStrapSaveAndTest(personInstance)
        personInstance.addToAuthorities(Authority.get(2))

        personInstance = new Person(loginName:"joe",
                                    firstName:"Joe",
                                    lastName:"Samples",
                                    password:passwordEncoded,
                                    email:"user@example.com")
        BootStrapSaveAndTest(personInstance)
        personInstance.addToAuthorities(Authority.get(2))

        personInstance = new Person(loginName:"mann",
                                    firstName:"Production",
                                    lastName:"Mann",
                                    password:passwordEncoded,
                                    email:"user@example.com")
        BootStrapSaveAndTest(personInstance)
        personInstance.addToAuthorities(Authority.get(2))

                
        //TaskGroup
//         new TaskGroup(name:"Engineering",
//                       description:"Engineering task group").save()
//         new TaskGroup(name:"Production",
//                       description:"Production task group").save()
//         new TaskGroup(name:"NewProject(s)",
//                       description:" ").save()
                      
        
        //Task
//         new Task(taskGroup:TaskGroup.findByName("Engineering"),
//                  leadPerson:Person.get(3),
//                  name:"Check specific level sensor",
//                  description:"Has been noted as problematic, try recallibrating",
//                  scheduledDate: new Date(),
//                  targetDate: new Date() ).save()
//         new Task(taskGroup:TaskGroup.findByName("Production"),
//                  leadPerson:Person.get(5),
//                  name:"Production Report",
//                  description:"Production report for specific production run or shift",
//                  scheduledDate: new Date(),
//                  targetDate: new Date() ).save()
//         new Task(taskGroup:TaskGroup.findByName("NewProject(s)"),
//                  leadPerson:Person.get(1),
//                  name:"Make killer CMMS app",
//                  description:"Use Grails and get a move on!",
//                  scheduledDate: new Date(),
//                  targetDate: new Date() ).save()

        //EntryType
//         new EntryType(name:"Fault").save()
//         new EntryType(name:"WorkDone").save()
//         new EntryType(name:"Production Report").save()

        //ModificationType
//         new ModificationType(name:"Created").save()
//         new ModificationType(name:"Completed").save()
//         new ModificationType(name:"Closed").save()
//         new ModificationType(name:"Altered").save()
//         new ModificationType(name:"TargetDateModified").save()
//         new ModificationType(name:"ScheduledDateModified").save()
//         new ModificationType(name:"DescriptionModified").save()
//         new ModificationType(name:"AssignedToModified").save()
//         new ModificationType(name:"NameModified").save()
        
        if(BootStrapDemoDataSuccessful) {
            println "BootStrapping demo data...successful."
        }
        else println "BootStrapping demo data...failed."
    }
    
    void BootStrapSaveAndTest(object) {
        if(!object.save()) {
            BootStrapDemoDataSuccessful = false
            println "'${object}' failed to save!"
            println object.errors

        }
    } 
}
