package com.zhaohaoting.framework.dto;

import com.zhaohaoting.framework.abs.entity.AreaCode;
import com.zhaohaoting.framework.utils.Utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * author：Created by zhaohaoting on 2018/8/2
 * email：526309416@qq.com
 * desc：转换ea_area.json文件类使用
 */
public class RegionAddressDTO implements BaseListDTO<AreaCode, RegionProvinceDTO<AreaCode>> {

    protected List<RegionProvinceDTO<AreaCode>> provinceList = new ArrayList<>();

    private static RegionAddressDTO regionAddressDTO;

    private RegionAddressDTO() {
    }

    public static RegionAddressDTO getInstance() {
        if (regionAddressDTO == null) {
            synchronized (RegionAddressDTO.class) {
                if (regionAddressDTO == null) {
                    regionAddressDTO = new RegionAddressDTO();
                }
            }
        }
        return regionAddressDTO;
    }

    @Override
    public List<RegionProvinceDTO<AreaCode>> format(List<AreaCode> areaCodeList) {
        if (!Utils.listIsEmpty(areaCodeList)) {
            Iterator<AreaCode> iterator = areaCodeList.iterator();
            while (iterator.hasNext()) {
                AreaCode areaCode = iterator.next();
                if (areaCode.getArea_level() == 0) {
                    RegionProvinceDTO<AreaCode> provinceDTO = new RegionProvinceDTO<>(areaCode);
                    provinceList.add(provinceDTO);
                    iterator.remove();
                } else if (areaCode.getArea_level() == 1) {
                    root1:
                    for (RegionProvinceDTO<AreaCode> regionProvinceDTO : provinceList) {
                        if (regionProvinceDTO.getProvince().getArea_id() == areaCode.getParent_id()) {
                            RegionProvinceDTO.CityDTO<AreaCode> cityDTO = new RegionProvinceDTO.CityDTO<AreaCode>(areaCode);
                            regionProvinceDTO.getCityList().add(cityDTO);
                            iterator.remove();
                            break root1;
                        }
                    }
                } else if (areaCode.getArea_level() == 2) {
                    root2:
                    for (RegionProvinceDTO<AreaCode> regionProvinceDTO : provinceList) {
                        for (RegionProvinceDTO.CityDTO<AreaCode> cityDTO : regionProvinceDTO.getCityList()) {
                            if (cityDTO.getCity().getArea_id() == areaCode.getParent_id()) {
                                RegionProvinceDTO.CityDTO.DistrictDTO<AreaCode> districtDTO = new RegionProvinceDTO.CityDTO.DistrictDTO<AreaCode>(areaCode);
                                cityDTO.districtList.add(districtDTO);
                                iterator.remove();
                                break root2;
                            }
                        }
                    }
                }
            }
            format(areaCodeList);
        } else {
            return provinceList;
        }
        return provinceList;
    }
}
