package com.lightinit.hsdatashow.impl.admin;

import com.lightinit.hsdatashow.dictionary.DicAverageExpectedRate;
import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.*;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.admin.IPeopleGrowthService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

@Service
public class PeopleGrowthServiceImpl implements IPeopleGrowthService {

    @Autowired
    private AverageLifetimeMapper lifetimeMapper ;

    @Autowired
    private AverageExpectedRateMapper expectedRateMapper ;

    @Autowired
    private DependencyRatioMapper dependencyRatioMapper ;

    @Autowired
    private PopulationSummaryMapper populationSummaryMapper ;

    @Autowired
    private AgeStructureMapper ageStructureMapper ;

    @Autowired
    private PopulationRegionMapper populationRegionMapper ;

    @Override
    public GroupNameAndValueModel getLifetime() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<Integer> groupNameAndValueModel = new GroupNameAndValueModel() ;
        AverageLifetimeExample example = new AverageLifetimeExample();
        List<AverageLifetimeModel> averageLifetimeModels = lifetimeMapper.getLifetime(example) ;
        List<String> name = new ArrayList<>();
        List<Integer> value = new ArrayList<>() ;
        for (AverageLifetimeModel averageLifetimeModel : averageLifetimeModels) {
           name.add(averageLifetimeModel.getRegionName()) ;
            value.add(averageLifetimeModel.getAge()) ;
        }
        groupNameAndValueModel.setCategories(name);
        groupNameAndValueModel.setValue(value);
        return groupNameAndValueModel;
    }

    @Override
    public  AverageExpectedRateResultModel getExpectedRate() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AverageExpectedRateResultModel averageExpectedRateResultModel = new AverageExpectedRateResultModel() ;
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<AverageExpectedRate> averageExpectedRates  = expectedRateMapper.getExpectedRate(year) ;
        List<String> yearAndMonth = new ArrayList<>();
        List<BigDecimal> deathValue = new ArrayList<>();
        List<BigDecimal> birthValue = new ArrayList<>();
        List<BigDecimal> increaseValue = new ArrayList<>();
        if (averageExpectedRates.size()>0) {
            for (AverageExpectedRate averageExpectedRate : averageExpectedRates) {
                if (!yearAndMonth.contains(averageExpectedRate.getDataYear()+"/"+averageExpectedRate.getDataMonth())){
                    yearAndMonth.add(averageExpectedRate.getDataYear()+"/"+averageExpectedRate.getDataMonth()) ;
                }
                if (averageExpectedRate.getDataType().equals(DicAverageExpectedRate.BIRTH)){
                    birthValue.add(averageExpectedRate.getAvgValue()) ;
                }
                if (averageExpectedRate.getDataType().equals(DicAverageExpectedRate.DEATH)){
                    deathValue.add(averageExpectedRate.getAvgValue()) ;
                }
                if (averageExpectedRate.getDataType().equals(DicAverageExpectedRate.INCREASE)){
                    increaseValue.add(averageExpectedRate.getAvgValue()) ;
                }
            }
        }
        averageExpectedRateResultModel.setYearAndMonth(yearAndMonth);
        averageExpectedRateResultModel.setBirthValue(birthValue);
        averageExpectedRateResultModel.setDeathValue(deathValue);
        averageExpectedRateResultModel.setIncreaseValue(increaseValue);
        return averageExpectedRateResultModel;
    }

    @Override
    public GroupNameAndValueModel getDependencyRatio() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<BigDecimal> groupNameAndValueModel = new GroupNameAndValueModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<DependencyRatio> dependencyRatios = dependencyRatioMapper.getDependencyRatio(year) ;
        if (dependencyRatios.size()>0) {
            groupNameAndValueModel.setDataValue(dependencyRatios.get(0).getDataValue());
            groupNameAndValueModel.setGrowth(dependencyRatios.get(0).getGrowth());
            List<String> name = new ArrayList<>();
            List<BigDecimal> value = new ArrayList<>();
            for (DependencyRatio dependencyRatio : dependencyRatios) {
                name.add(dependencyRatio.getDataYear().toString());
                value.add(dependencyRatio.getDataValue());
            }
            groupNameAndValueModel.setCategories(name);
            groupNameAndValueModel.setValue(value);
        }
        return groupNameAndValueModel;
    }

    @Override
    public PopulationSummaryListModel getPopulationSummary() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PopulationSummaryListModel populationSummaryListModel = new PopulationSummaryListModel() ;
        Calendar calendar = Calendar.getInstance() ;
        int year = calendar.get(Calendar.YEAR)-5 ;
        List<PopulationSummaryReturnModel> populationSummaryReturnModels = populationSummaryMapper.getPopulationSummary(year) ;
        if (populationSummaryReturnModels.size()>0) {
            //TODO
            //populationSummaryListModel.setDataValue(populationSummaryReturnModels.get(0).getDataValue());
            //populationSummaryListModel.setGrowth(populationSummaryReturnModels.get(0).getGrowth());
            List<BigDecimal> manDataValue = new ArrayList<>() ;
            List<BigDecimal> girlDataValue = new ArrayList<>() ;
            List<BigDecimal> allDataValue = new ArrayList<>() ;
            List<Integer> dataYear = new ArrayList<>() ;
            for (PopulationSummaryReturnModel populationSummaryReturnModel : populationSummaryReturnModels) {
                manDataValue.add(populationSummaryReturnModel.getManValue());
                girlDataValue.add(populationSummaryReturnModel.getGirlValue());
                allDataValue.add(populationSummaryReturnModel.getGirlValue().add(populationSummaryReturnModel.getManValue())) ;
                dataYear.add(populationSummaryReturnModel.getDataYear()) ;
            }
            populationSummaryListModel.setManDataValue(manDataValue);
            populationSummaryListModel.setGirlDataValue(girlDataValue);
            populationSummaryListModel.setAllDataValue(allDataValue);
            populationSummaryListModel.setDataYear(dataYear);
        }
        return populationSummaryListModel;
    }

    @Override
    public AgeStructureReturnModel getAgeStructure() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AgeStructureReturnModel ageStructureReturnModel = new AgeStructureReturnModel() ;
        AgeStructureExample example = new AgeStructureExample() ;
        List<AgeStructure> ageStructures = ageStructureMapper.selectByExample(example) ;
        if (ageStructures.size()>0){
            List<String> ageName = new ArrayList<>() ;
            List<AgeStructureNameAndValueModel> ageStructureNameAndValueModels = new ArrayList<>() ;
            for (AgeStructure ageStructure : ageStructures) {
                ageName.add(ageStructure.getAgeRange()) ;
                ageStructureNameAndValueModels.add(new AgeStructureNameAndValueModel(ageStructure.getAgeRange(),ageStructure.getQuantity())) ;
            }
            ageStructureReturnModel.setAgeName(ageName);
            ageStructureReturnModel.setAgeStructureNameAndValueModels(ageStructureNameAndValueModels);
        }
        return ageStructureReturnModel;
    }

    @Override
    public List<PopulationRegionResultModel> getPopulationRegion() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PopulationRegionExample example = new PopulationRegionExample();
        return populationRegionMapper.getPopulationRegion(example);
    }

    @Override
    public GroupNameAndValueModel getPopulationRegionPicture() {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        GroupNameAndValueModel<Long> groupNameAndValueModel = new GroupNameAndValueModel<>() ;
        PopulationRegionExample example = new PopulationRegionExample();
        List<PopulationRegionResultModel> populationRegionResultModels = populationRegionMapper.getPopulationRegion(example) ;
        List<String> name = new ArrayList<>() ;
        List<Long> value = new ArrayList<>() ;
        if (populationRegionResultModels.size()>0){
            for (PopulationRegionResultModel populationRegionResultModel : populationRegionResultModels) {
                name.add(populationRegionResultModel.getRegionName()) ;
                value.add(populationRegionResultModel.getQuantity()) ;
            }
            groupNameAndValueModel.setCategories(name);
            groupNameAndValueModel.setValue(value);
        }
        return groupNameAndValueModel;
    }
}
