package com.wasu.cs.model;

import com.wasu.common.WError;
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;

/**
 * 退出页推荐数据
 */

public class RecommendListEntry extends ObjectBase{
    private final int maxSize = 5;

    private String id;//ID
    private String catId;//栏目ID
    private String layout;
    private String picUrl;
    private String title;
    private String jsonUrl;
    private String points;

        private List<RecommendListEntry> mList = new ArrayList<>();

    public RecommendListEntry(){}
    public RecommendListEntry(String id, String catId, String layout, String picUrl, String title, String jsonUrl, String points){
            this.id = id;
            this.catId = catId;
            this.layout = layout;
            this.picUrl = picUrl;
            this.title = title;
            this.jsonUrl = jsonUrl;
            this.points = points;
        }

        @Override
        public void createFromResponse(String response) throws DataFetchException {
            try {
                JSONObject JsonData = new JSONObject(response);
                if (JsonData.getInt("code")!=200){
                    throw new DataFetchException(WError.ERROR_INVALID_DATA, JsonData.getString("message"));
                }
                // 获得json
                JSONObject recommendJon = JsonData.getJSONObject("data");
                // 获得推荐位数据
                JSONArray array = recommendJon.optJSONArray("list");
                if (array == null) {
                    throw new DataFetchException(WError.ERROR_INVALID_DATA, "没有推荐数据");
                }

                int length = array.length() > maxSize ? maxSize:array.length(); // 最大五条
                for (int i = 0; i < length; i++){
                    RecommendListEntry list = optJson(array.optJSONObject(i));
                    mList.add(list);
                }

            }catch (JSONException e) {
                e.printStackTrace();
                throw new DataFetchException(WError.ERROR_INVALID_DATA, "推荐资产数据错误");
            }
        }

        private RecommendListEntry optJson(JSONObject jsondata){
            id = jsondata.optString("id");
            catId = jsondata.optString("catId");
            layout = jsondata.optString("layout");
            title = jsondata.optString("title");
            picUrl = jsondata.optString("picUrl");
            jsonUrl = jsondata.optString("jsonUrl");
            points = jsondata.optString("points");

            RecommendListEntry list = new RecommendListEntry(
                    id == null?"":id,
                    catId == null?"":catId,
                    layout == null? "" :layout,
                    picUrl == null?"" :picUrl,
                    title == null? "":title,
                    jsonUrl == null? "" : jsonUrl,
                    points == null? "":points);
            return list;
        }

        public String getId() {
            return id;
        }

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

        public String getCatId() {
            return catId;
        }

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

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

    public String getJsonUrl() {
        return jsonUrl;
    }

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

    public String getPoints() {
        return points;
    }

    public void setPoints(String points) {
        this.points = points;
    }

    public List<RecommendListEntry> getmList() {
        return mList;
    }

    public void setmList(List<RecommendListEntry> mList) {
        this.mList = mList;
    }
}
