import at.clockwork.time.domain.*
import at.clockwork.terminal.domain.*
import at.clockwork.common.person.domain.*

import at.clockwork.singleton.SystemConfigurations
import at.clockwork.transfer.gwtTransfer.client.enumeration.*
import at.clockwork.generated.service.*

import grails.util.Holders

class UpdateScriptService {

	def balanceDefinitionWriteService = Holders.grailsApplication.mainContext.getBean 'balanceDefinitionWriteService'
	def balanceDefinition2DetailWriteService = Holders.grailsApplication.mainContext.getBean 'balanceDefinition2DetailWriteService'
	def terminalWriteService = Holders.grailsApplication.mainContext.getBean 'terminalWriteService'
	def person2BalanceWriteService = Holders.grailsApplication.mainContext.getBean 'person2BalanceWriteService'
	def systemConfigurationService = Holders.grailsApplication.mainContext.getBean 'systemConfigurationService'
	def initSystemConfigurationService = Holders.grailsApplication.mainContext.getBean 'initSystemConfigurationService'

    SystemConfigurations systemConfigurations = SystemConfigurations.getInstance()

	def run(def connection, def dataSourceFields) {

        // damit die SystemConfiguration 400 vorhanden ist
        initSystemConfigurationService.setupSystemConfiguration()

        BalanceDefinition balanceDefinition = new BalanceDefinition()

        balanceDefinition.name = "1"
        balanceDefinition.description = "allgemein"

        balanceDefinition.balancePer_de = systemConfigurations.getItem(401, "strValue")
        balanceDefinition.balancePer_en = systemConfigurations.getItem(401, "strValue")

        balanceDefinitionWriteService.save(balanceDefinition)
        systemConfigurationService.updateByCwID((int) 400, (int) balanceDefinition.id)

        saveDetail(balanceDefinition, 1, systemConfigurations.getItem(402, "strValue"), systemConfigurations.getItem(422, "strValue"), systemConfigurations.getItem(442, "strValue"))
        saveDetail(balanceDefinition, 2, systemConfigurations.getItem(403, "strValue"), systemConfigurations.getItem(423, "strValue"), systemConfigurations.getItem(443, "strValue"))
        saveDetail(balanceDefinition, 3, systemConfigurations.getItem(404, "strValue"), systemConfigurations.getItem(424, "strValue"), systemConfigurations.getItem(444, "strValue"))
        saveDetail(balanceDefinition, 4, systemConfigurations.getItem(405, "strValue"), systemConfigurations.getItem(425, "strValue"), systemConfigurations.getItem(445, "strValue"))
        saveDetail(balanceDefinition, 5, systemConfigurations.getItem(406, "strValue"), systemConfigurations.getItem(426, "strValue"), systemConfigurations.getItem(446, "strValue"))
        saveDetail(balanceDefinition, 6, systemConfigurations.getItem(407, "strValue"), systemConfigurations.getItem(427, "strValue"), systemConfigurations.getItem(447, "strValue"))
        saveDetail(balanceDefinition, 7, systemConfigurations.getItem(408, "strValue"), systemConfigurations.getItem(428, "strValue"), systemConfigurations.getItem(448, "strValue"))
        saveDetail(balanceDefinition, 8, systemConfigurations.getItem(409, "strValue"), systemConfigurations.getItem(429, "strValue"), systemConfigurations.getItem(449, "strValue"))
        saveDetail(balanceDefinition, 9, systemConfigurations.getItem(410, "strValue"), systemConfigurations.getItem(430, "strValue"), systemConfigurations.getItem(450, "strValue"))
        saveDetail(balanceDefinition, 10, systemConfigurations.getItem(411, "strValue"), systemConfigurations.getItem(431, "strValue"), systemConfigurations.getItem(451, "strValue"))

        Terminal.findAll().each {
            it.balanceDefinition = balanceDefinition
            terminalWriteService.save(it)
        }

        Person2Balance.findAll().each {
            it.balanceDefinition = balanceDefinition
            person2BalanceWriteService.save(it)
        }

		true

	}

	void saveDetail(BalanceDefinition balanceDefinition, int position, String description, String calculation, String timeFormatEnumString) {

	    if (!balanceDefinition || balanceDefinition.hasErrors())
	        return
	    if (position == 0)
	        return
	    if (!description || !calculation || !timeFormatEnumString)
	        return

	    TimeFormatEnum timeFormatEnum
        try {
            timeFormatEnum = TimeFormatEnum.valueOf(timeFormatEnumString)
        } catch (e) {
            timeFormatEnum = TimeFormatEnum.HHMM
        }

        BalanceDefinition2Detail balanceDefinition2Detail = new BalanceDefinition2Detail()

        balanceDefinition2Detail.balanceDefinition = balanceDefinition
        balanceDefinition2Detail.position = position
        balanceDefinition2Detail.description_de = description
        balanceDefinition2Detail.description_en = description
        balanceDefinition2Detail.calculation = calculation
        balanceDefinition2Detail.timeFormatEnum = timeFormatEnum

        balanceDefinition2DetailWriteService.save(balanceDefinition2Detail)

	}


}