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 ChildrenAllLikeData extends ObjectBase {


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

    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 List<ListBean> getRow1() {
        return row1;
    }

    public void setRow1(List<ListBean> row1) {
        this.row1 = row1;
    }

    public List<ListBean> getRow2() {
        return row2;
    }

    public void setRow2(List<ListBean> row2) {
        this.row2 = row2;
    }

    public List<ListBean> getRow3() {
        return row3;
    }

    public void setRow3(List<ListBean> row3) {
        this.row3 = row3;
    }

    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 dataArray1 = json.optJSONArray("row1");
            JSONArray dataArray2 = json.optJSONArray("row2");
            JSONArray dataArray3 = json.optJSONArray("row3");

            if (dataArray1 == null || dataArray2 == null || dataArray3 == null) {
                return;
            }

            setBgPicUrl(o.optString("bgPicUrl"));
            for (int i = 0; i < dataArray1.length(); i++) {
                JSONObject item = dataArray1.optJSONObject(i);
                row1.add(getListBean(item));
            }
            for (int i = 0; i < dataArray2.length(); i++) {
                JSONObject item = dataArray2.optJSONObject(i);
                row2.add(getListBean(item));
            }
            for (int i = 0; i < dataArray3.length(); i++) {
                JSONObject item = dataArray3.optJSONObject(i);
                row3.add(getListBean(item));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private ListBean getListBean(JSONObject item){
        ListBean listBean = new ListBean();
        listBean.setCatId(item.optString("catId"));
        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"));
        return listBean;
    }



    public String getBgPicUrl() {
        return bgPicUrl;
    }

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


    public static class ListBean {

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


        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;
        }
    }
}
