/*
 *
 *  Copyright 2012-2014 Eurocommercial Properties NV
 *
 *
 *  Licensed under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.estatio.module.asset.fixtures.property.builders;

import java.util.List;

import javax.inject.Inject;

import org.joda.time.LocalDate;

import org.apache.isis.applib.fixturescripts.BuilderScriptAbstract;

import org.isisaddons.module.security.dom.tenancy.ApplicationTenancyRepository;

import org.incode.module.country.dom.impl.Country;
import org.incode.module.country.dom.impl.CountryRepository;
import org.incode.module.country.dom.impl.StateRepository;

import org.estatio.module.asset.dom.Property;
import org.estatio.module.asset.dom.PropertyRepository;
import org.estatio.module.asset.dom.PropertyType;
import org.estatio.module.asset.dom.Unit;
import org.estatio.module.asset.dom.UnitRepository;
import org.estatio.module.asset.dom.UnitType;
import org.estatio.module.asset.dom.role.FixedAssetRoleTypeEnum;
import org.estatio.module.base.platform.fake.EstatioFakeDataService;
import org.estatio.module.party.dom.Party;
import org.estatio.module.party.dom.PartyRepository;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import static org.incode.module.base.integtests.VT.ld;

@EqualsAndHashCode(of={"reference"})
@Accessors(chain = true)
public class PropertyAndUnitsAndOwnerAndManagerBuilder
        extends BuilderScriptAbstract<PropertyAndUnitsAndOwnerAndManagerBuilder> {

    @Getter @Setter
    private String reference;

    @Getter @Setter
    private String name;

    @Getter @Setter
    private String city;

    @Getter @Setter
    private Country country;

    @Getter @Setter
    private PropertyType propertyType;

    @Getter @Setter
    private LocalDate acquireDate;

    @Getter @Setter
    private LocalDate openingDate;

    @Getter @Setter
    private String locationStr;

    @Getter @Setter
    private Party owner;
    @Getter @Setter
    private LocalDate ownerStartDate;
    @Getter @Setter
    private LocalDate ownerEndDate;

    @Getter @Setter
    private Party manager;
    @Getter @Setter
    private LocalDate managerStartDate;
    @Getter @Setter
    private LocalDate managerEndDate;

    @Getter @Setter
    private Integer numberOfUnits;

    @Getter
    private Property property;


    @Getter
    private List<Unit> units;

    @Override
    protected void execute(final ExecutionContext executionContext) {

        defaultParam("numberOfUnits", executionContext, fakeDataService.values().anInt(10,20));

        final PropertyBuilder propertyBuilder = new PropertyBuilder();
        property = propertyBuilder
                .setReference(reference)
                .setName(name)
                .setCity(city)
                .setCountry(country)
                .setPropertyType(propertyType)
                .setAcquireDate(acquireDate)
                .setOpeningDate(openingDate)
                .setLocationStr(locationStr)
                .build(this, executionContext)
                .getProperty();



        if(owner != null) {
            //final PropertyOwnerBuilder propertyOwnerBuilder = new PropertyOwnerBuilder();

            defaultParam("ownerStartDate", executionContext, ld(1999, 1, 1));
            defaultParam("ownerEndDate", executionContext, ld(2000, 1, 1));

            property.addRoleIfDoesNotExist(owner, FixedAssetRoleTypeEnum.PROPERTY_OWNER, ownerStartDate, ownerEndDate);
//            wrap(property).newRole(FixedAssetRoleTypeEnum.PROPERTY_OWNER, getOwner(), getAcquireDate(), null);
        }
        if(manager != null) {
            //final PropertyManagerBuilder propertyManagerBuilder = new PropertyManagerBuilder();
            property.addRoleIfDoesNotExist(manager, FixedAssetRoleTypeEnum.ASSET_MANAGER, managerStartDate, managerEndDate);
//            wrap(property).newRole(FixedAssetRoleTypeEnum.ASSET_MANAGER, getManager(), getAcquireDate(), null);
        }

        final PropertyUnitsBuilder propertyUnitsBuilder = new PropertyUnitsBuilder();
        units = propertyUnitsBuilder.setProperty(property)
                .setNumberOfUnits(numberOfUnits)
                .build(this, executionContext)
                .getUnits();
//        for (int i = 0; i < getNumberOfUnits(); i++) {
//            int unitNumber = i + 1;
//            wrap(property).newUnit(String.format("%s-%03d", property.getReference(), unitNumber), "Unit " + unitNumber, unitType(i)).setArea(new BigDecimal((i + 1) * 100));
//
////            final String unitRef = buildUnitReference(property.getReference(), i);
////            final UnitType unitType = fakeDataService.collections().anEnum(UnitType.class);
////            final String unitName = fakeDataService.name().firstName();
////            wrap(property).newUnit(unitRef, unitName, unitType);
//        }
    }

    private UnitType unitType(int n) {
        final UnitType[] unitTypes = UnitType.values();
        return unitTypes[n % unitTypes.length];
    }

    String buildUnitReference(final String propertyReference, final Integer unitNum) {
        return String.format("%1$s-%2$03d", propertyReference, unitNum);
    }

    // //////////////////////////////////////

    @Inject
    protected StateRepository stateRepository;

    @Inject
    protected CountryRepository countryRepository;

    @Inject
    protected PropertyRepository propertyRepository;

    @Inject
    protected UnitRepository unitRepository;

    @Inject
    protected PartyRepository partyRepository;

    @Inject
    protected ApplicationTenancyRepository applicationTenancyRepository;

    @Inject
    EstatioFakeDataService fakeDataService;


}

@@ -696,16 +696,45 @@
 lders;%0A%0A
+import java.math.BigDecimal;%0A
 import j
@@ -4509,280 +4509,8 @@
  %7D%0A%0A
-        final PropertyUnitsBuilder propertyUnitsBuilder = new PropertyUnitsBuilder();%0A        units = propertyUnitsBuilder.setProperty(property)%0A                .setNumberOfUnits(numberOfUnits)%0A                .build(this, executionContext)%0A                .getUnits();%0A//
     
@@ -4560,18 +4560,16 @@
  i++) %7B%0A
-//
         
@@ -4596,18 +4596,16 @@
  i + 1;%0A
-//
         
@@ -4773,15 +4773,11 @@
 ));%0A
-//%0A/
+%0A
 //
-/
     
@@ -4855,18 +4855,16 @@
 (), i);%0A
-//
 //      
@@ -4949,18 +4949,16 @@
 class);%0A
-//
 //      
@@ -5023,18 +5023,16 @@
 Name();%0A
-//
 //      
@@ -5086,26 +5086,24 @@
  unitType);%0A
-//
         %7D%0A  

Revert "EST-1176: minor refactoring to use PropertyUnitsBuilder"