package com.wasu.cs.model;

import com.wasu.module.datafetch.DataFetchException;
import com.wasu.module.datafetch.ObjectBase;

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

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

/**
 * Created by jeepc on 2016/11/17.
 */

public class ChildrenData extends ObjectBase {


    private int code;
    private String message;
    private String bgPicUrl;
    private String searchUrl;
    private String layout;
    private List<ListBean> list = new ArrayList<>();

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSearchUrl() {
        return searchUrl;
    }

    public void setSearchUrl(String searchUrl) {
        this.searchUrl = searchUrl;
    }

    public String getLayout() {
        return layout;
    }

    public void setLayout(String layout) {
        this.layout = layout;
    }

    @Override
    public void createFromResponse(String response) throws DataFetchException {
        try {
            JSONObject o = new JSONObject(response);

            code = o.optInt("code");

            if (code != 200) {
                return;
            }


            JSONObject json = o.optJSONObject("data");
            if (json == null ) {
                return;
            }
            JSONObject searchJson = json.optJSONObject("search");
            if(searchJson!=null) {
                searchUrl = searchJson.optString("jsonUrl");
                layout = searchJson.optString("layout");
            }
            JSONArray dataArray = json.optJSONArray("list");
            if(dataArray == null){
                return;
            }
            if (dataArray == null) {
                return;
            }

            setBgPicUrl(o.optString("bgPicUrl"));
            int size = dataArray.length();
            for (int i = 0; i < size; i++) {
                JSONObject item = dataArray.optJSONObject(i);

                ListBean ListBean = new ListBean();
                ListBean.setCatId(item.optString("catId"));
                ListBean.setFee(item.optInt("fee"));
                ListBean.setId(item.optString("id"));
                ListBean.setJsonUrl(item.optString("jsonUrl"));
                ListBean.setLayout(item.optString("layout"));
                ListBean.setPicUrl(item.optString("picUrl"));
                ListBean.setTitle(item.optString("title"));

                list.add(ListBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }




    public String getBgPicUrl() {
        return bgPicUrl;
    }

    public void setBgPicUrl(String bgPicUrl) {
        this.bgPicUrl = bgPicUrl;
    }

    public List<ListBean> getList() {
        return list;
    }

    public void setList(List<ListBean> list) {
        this.list = list;
    }

    public static class ListBean {

        private int fee;
        private String catId;
        private String id;
        private String jsonUrl;
        private String layout;
        private String picUrl;
        private String title;

        public int getFee() {
            return fee;
        }

        public void setFee(int fee) {
            this.fee = fee;
        }

        public String getCatId() {
            return catId;
        }

        public void setCatId(String catId) {
            this.catId = catId;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getJsonUrl() {
            return jsonUrl;
        }

        public void setJsonUrl(String jsonUrl) {
            this.jsonUrl = jsonUrl;
        }

        public String getLayout() {
            return layout;
        }

        public void setLayout(String layout) {
            this.layout = layout;
        }

        public String getPicUrl() {
            return picUrl;
        }

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }
}
