package com.zhaohaoting.framework.dto;


import com.zhaohaoting.framework.view.picker.framework.entity.City;
import com.zhaohaoting.framework.view.picker.framework.entity.County;
import com.zhaohaoting.framework.view.picker.framework.entity.Province;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * author：Created by zhaohaoting on 2018/1/10 11:51
 * email：526309416@qq.com
 * desc：转换province.json文件使用 使用picker类实体转换 做滚动选项使用
 */

public class ProvinceDTO {
    private static ArrayList<Province> provinces = new ArrayList<>();

    public static ArrayList<Province> parseAreaJson(String json) {
        provinces.clear();
        try {
            JSONArray provinceArray = new JSONArray(json);
            for (int i = 0; i < provinceArray.length(); i++) {
                JSONObject provinceObject = provinceArray.optJSONObject(i);
                String provinceName = provinceObject.optString("name");
                Province province = new Province();
                province.setAreaName(provinceName);
                provinces.add(province);
                JSONArray cityArray = provinceObject.optJSONArray("city");
                for (int j = 0; j < cityArray.length(); j++) {
                    JSONObject cityObject = cityArray.optJSONObject(j);
                    String cityName = cityObject.optString("name");
                    City city = new City();
                    city.setAreaName(cityName);
                    provinces.get(i).getCities().add(city);
                    JSONArray countyArray = cityObject.optJSONArray("area");
                    for (int c = 0; c < countyArray.length(); c++) {
                        County county = new County();
                        county.setAreaName(countyArray.get(c).toString());
                        provinces.get(i).getCities().get(j).getCounties().add(county);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return provinces;
    }

    public static ArrayList<Province> getProvinces() {
        return provinces;
    }
}
