package cn.momia.api.base;

import cn.momia.api.base.region.Region;
import cn.momia.api.base.city.City;
import cn.momia.api.base.region.CityDistricts;
import cn.momia.common.api.AbstractServiceApi;
import cn.momia.common.api.http.MomiaHttpParamBuilder;
import cn.momia.common.api.http.MomiaHttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

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

public class BaseServiceApi extends AbstractServiceApi {
    public static CityServiceApi CITY = new CityServiceApi();
    public static RegionServiceApi REGION = new RegionServiceApi();
    public static FeedbackServiceApi FEEDBACK = new FeedbackServiceApi();
    public static RecommendServiceApi RECOMMEND = new RecommendServiceApi();
    public static SmsServiceApi SMS = new SmsServiceApi();

    public void init() {
        CITY.setService(service);
        REGION.setService(service);
        FEEDBACK.setService(service);
        RECOMMEND.setService(service);
        SMS.setService(service);
    }

    public static class CityServiceApi extends BaseServiceApi {
        public List<City> getAll() {
            MomiaHttpRequest request = MomiaHttpRequest.GET(url("city"));
            JSONArray citiesJson = (JSONArray) executeRequest(request);

            List<City> cities = new ArrayList<City>();
            for (int i = 0; i < citiesJson.size(); i++) {
                JSONObject cityJson = citiesJson.getJSONObject(i);
                cities.add(JSON.toJavaObject(cityJson, City.class));
            }

            return cities;
        }
    }

    public static class RegionServiceApi extends BaseServiceApi {
        public List<Region> getAll() {
            MomiaHttpRequest request = MomiaHttpRequest.GET(url("region"));
            JSONArray regionsJson = (JSONArray) executeRequest(request);

            List<Region> regions = new ArrayList<Region>();
            for (int i = 0; i < regionsJson.size(); i++) {
                JSONObject regionJson = regionsJson.getJSONObject(i);
                regions.add(JSON.toJavaObject(regionJson, Region.class));
            }

            return regions;
        }

        public List<CityDistricts> getCityDistrictTree() {
            MomiaHttpRequest request = MomiaHttpRequest.GET(url("region/district/tree"));
            JSONArray citiesDistrictsJson = (JSONArray) executeRequest(request);

            List<CityDistricts> citiesDistricts = new ArrayList<CityDistricts>();
            for (int i = 0; i < citiesDistrictsJson.size(); i++) {
                JSONObject cityDistrictsJson = citiesDistrictsJson.getJSONObject(i);
                citiesDistricts.add(JSON.toJavaObject(cityDistrictsJson, CityDistricts.class));
            }

            return citiesDistricts;
        }
    }

    public static class FeedbackServiceApi extends BaseServiceApi {
        public void addFeedback(String content, String email) {
            MomiaHttpParamBuilder builder = new MomiaHttpParamBuilder()
                    .add("content", content)
                    .add("email", email);
            MomiaHttpRequest request = MomiaHttpRequest.POST(url("feedback"), builder.build());
            executeRequest(request);
        }
    }

    public static class RecommendServiceApi extends BaseServiceApi {
        public void addRecommend(String content, String time, String address, String contacts) {
            MomiaHttpParamBuilder builder = new MomiaHttpParamBuilder()
                    .add("content", content)
                    .add("time", time)
                    .add("address", address)
                    .add("contacts", contacts);
            MomiaHttpRequest request = MomiaHttpRequest.POST(url("recommend"), builder.build());
            executeRequest(request);
        }
    }

    public static class SmsServiceApi extends BaseServiceApi {
        public void send(String mobile, String type) {
            MomiaHttpParamBuilder builder = new MomiaHttpParamBuilder()
                    .add("mobile", mobile)
                    .add("type", type);
            MomiaHttpRequest request = MomiaHttpRequest.POST(url("sms/send"), builder.build());
            executeRequest(request);
        }

        public void verify(String mobile, String code) {
            MomiaHttpParamBuilder builder = new MomiaHttpParamBuilder()
                    .add("mobile", mobile)
                    .add("code", code);
            MomiaHttpRequest request = MomiaHttpRequest.POST(url("sms/verify"), builder.build());
            executeRequest(request);
        }

        public void notify(String mobile, String msg) {
            MomiaHttpParamBuilder builder = new MomiaHttpParamBuilder()
                    .add("mobile", mobile)
                    .add("msg", msg);
            MomiaHttpRequest request = MomiaHttpRequest.POST(url("sms/notify"), builder.build());
            executeRequest(request);
        }
    }
}

@@ -292,24 +292,71 @@
 ttpRequest;%0A
+import cn.momia.common.api.http.util.CastUtil;%0A
 import com.a
@@ -1310,31 +1310,31 @@
     
-JSONArray citiesJson = 
+return CastUtil.toList(
 (JSO
@@ -1368,300 +1368,21 @@
 est)
-;%0A%0A            List%3CCity%3E cities = new ArrayList%3CCity%3E();%0A            for (int i = 0; i %3C citiesJson.size(); i++) %7B%0A                JSONObject cityJson = citiesJson.getJSONObject(i);%0A                cities.add(JSON.toJavaObject(cityJson, City.class));%0A            %7D%0A%0A            return cities
+, City.class)
 ;%0A  
@@ -1593,32 +1593,31 @@
     
-JSONArray regionsJson = 
+return CastUtil.toList(
 (JSO
@@ -1651,315 +1651,23 @@
 est)
-;%0A%0A            List%3CRegion%3E regions = new ArrayList%3CRegion%3E();%0A            for (int i = 0; i %3C regionsJson.size(); i++) %7B%0A                JSONObject regionJson = regionsJson.getJSONObject(i);%0A                regions.add(JSON.toJavaObject(regionJson, Region.class));%0A            %7D%0A%0A            return regions
+, Region.class)
 ;%0A  
@@ -1840,40 +1840,31 @@
     
-JSONArray citiesDistrictsJson = 
+return CastUtil.toList(
 (JSO
@@ -1898,390 +1898,30 @@
 est)
-;%0A%0A            List%3CCityDistricts%3E citiesDistricts = new ArrayList%3CCityDistricts%3E();%0A            for (int i = 0; i %3C citiesDistrictsJson.size(); i++) %7B%0A                JSONObject cityDistrictsJson = citiesDistrictsJson.getJSONObject(i);%0A                citiesDistricts.add(JSON.toJavaObject(cityDistrictsJson, CityDistricts.class));%0A            %7D%0A%0A            return citiesDistricts
+, CityDistricts.class)
 ;%0A  

add cast util