package com.wasu.cs.model;

import android.text.TextUtils;

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 栏目数据
 * @author LiuYang
 * Created by LiuYang on 7/2/15.
 */
public class CatData extends Model implements Serializable{

    /**
     */
    private static final long serialVersionUID = 1L;
    private Cat cat;
    private List<Cat> childCats = new ArrayList<Cat>();
    private List<Element> elements = new ArrayList<Element>();
    private List<AssetElement> assets = new ArrayList<AssetElement>();

    private int total = 0;

    public Cat getCat() {
        return cat;
    }

    public int getTotal() {
        return total;
    }

    public List<Cat> getChildCats() {
        return childCats;
    }

    public List<Element> getElements() {
        return elements;
    }

    public List<AssetElement> getAssets() {
        return assets;
    }

    @Override
    public boolean from(JSONObject json) {
        if (super.from(json)) {

            json = json.optJSONObject("data");

            if(json != null) {
                cat = new Cat();

                JSONObject catObj = json.optJSONObject("cat");
                if(catObj != null) {
                    cat.from(json.optJSONObject("cat"));
                }
                else {
                    cat.from(json);
                }

                childCats.clear();
                JSONArray catArray = json.optJSONArray("childCats");
                if(catArray != null) {
                    for(int i = 0;i < catArray.length();i++) {
                        Cat cat = new Cat();
                        cat.from(catArray.optJSONObject(i));
                        childCats.add(cat);
                    }
                }

                elements.clear();
                JSONArray elementArray = json.optJSONArray("elements");
                if(elementArray != null) {
                    for(int i = 0; i < elementArray.length();i++) {
                        Element element = new Element();
                        element.from(elementArray.optJSONObject(i));
                        elements.add(element);
                    }
                }

                JSONArray assetArray = json.optJSONArray("assets");
                if(assetArray != null) {
                    for(int i = 0; i < assetArray.length();i++) {
                        AssetElement element = new AssetElement();
                        element.from(assetArray.optJSONObject(i));
                        assets.add(element);
                    }
                }

                total = json.optInt("total");
            }

            return true;
        }
        return false;
    }

    //数据抽取基类
    public class BaseElement extends Model implements Serializable{
        /**
         */
        private static final long serialVersionUID = 1L;
        private String layout;
        private String name;
        private String jsonUrl;
        private String picUrl;
        private int selected;
        private String recommendUrl;

        public BaseElement() {

        }

        public BaseElement(BaseElement element) {
            this.layout = element.layout;
            this.name = element.name;
            this.jsonUrl = element.jsonUrl;
        }

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

        public void setName(String name) {
            this.name = name;
        }

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

        public void setSelected(int selected) {
            this.selected = selected;
        }

        public String getLayout() {
            return layout;
        }

        public String getName() {
            return name;
        }

        public String getJsonUrl() {
            return jsonUrl;
        }

        public int getSelected() {
            return selected;
        }

        public String getRecommendUrl() {
            return recommendUrl;
        }

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

        public String getPicUrl() {
            return picUrl;
        }
        
        @Override
        public boolean from(JSONObject json) {
            if(json != null) {
                layout = json.optString("layout");
                name = json.optString("name");
                jsonUrl = json.optString("jsonUrl");
                selected = json.optInt("selected");
                recommendUrl = json.optString("recommendUrl");
                picUrl = json.optString("picUrl");

                if(TextUtils.isEmpty(name)) {
                    name = json.optString("title");
                }

                if(TextUtils.isEmpty(name)) {
                    name = json.optString("content");
                }

                if(TextUtils.isEmpty(jsonUrl)) {
                    jsonUrl = json.optString("json");
                }

                return true;
            }

            return false;
        }
    }

    //栏目数据结构
    public class Cat extends BaseElement implements Serializable {
        /**
         */
        private static final long serialVersionUID = 1L;
        
        private String bgImage;//
        private int catType;//栏目类型，参见字典定义
        private String engName;//栏目英文名称
        private String bgColor;//栏目背景色
        private String assetListUrl;//
        /*标识是什么栏目*/
        private int catProp;//
        /*哪一个tab被选中*/
        private int selected;//
        private String name; // 名称
        private int catId;
        
        public String getBgImage() {
            return bgImage;
        }

        public int getCatType() {
            return catType;
        }

        public String getEngName() {
            return engName;
        }

        public String getBgColor() {
            return bgColor;
        }

        public String getAssetListUrl() {
            return assetListUrl;
        }
        
        public int getCatProp() {
			return catProp;
		}
        
        public int getSelected(){
        	return selected;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public void setName(String name) {
            this.name = name;
        }

        public int getCatId() {
            return catId;
        }

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

        @Override
        public boolean from(JSONObject json) {
            if(super.from(json)) {
                bgImage = json.optString("bgImage");
                catType = json.optInt("catType");
                engName = json.optString("engName");
                bgColor = json.optString("bgColor");
                assetListUrl = json.optString("assetListUrl");
                catProp = json.optInt("catProp", -1);
                selected = json.optInt("selected");
                name = json.optString("name");
                catId = json.optInt("catId", 0);
                return true;
            }
            return false;
        }
    }

    //元素数据类
    public class Element extends BaseElement implements Serializable {

        /**
         */
        private static final long serialVersionUID = 1L;
        private String eid;//元素id
        private String position;//元素位置标识
        private String tbgColor;//图片元素相关文本之遮罩颜色；文本元素为空
        private int elementType;//元素类型，参见字典常量定义，决定content内容
        private String description;//图片元素相关文本之描述；文本元素为空
        private String content;//元素类型，文本或图片地址，根据elementType确定，如图片元素只需要一张图片，取该值即可，如图片元素为多值，则取值于picUrls
        private String title;//
        private int linkType;//点击元素后，决定请求资源的方式
        private String summary;//图片元素相关文本之看点，简述；文本元素为空
        private String descColor;//元素文本描述字颜色
        private String nameColor;//标题字颜色
        private String summaryColor;//看点文字颜色
//        private List<TagElement> tags = new ArrayList<>();
        private List<AssetElement> assets = new ArrayList<AssetElement>();//

        public Element() {

        }

        public Element(Element element) {
            super(element);
            this.eid = element.eid;
            this.title = element.title;
            this.elementType = element.elementType;
            this.summary = element.summary;
        }

        public String getEid() {
            return eid;
        }

        public String getPosition() {
            return position;
        }

        public String getTbgColor() {
            return tbgColor;
        }

        public int getElementType() {
            return elementType;
        }

//        public List<TagElement> getTags() {
//            return tags;
//        }

        public String getDescription() {
            return description;
        }

        public String getContent() {
            return content;
        }

        public String getTitle() {
            return title;
        }

//        public LinkType getLinkType() {
//            return LinkType.fromInt(linkType);
//        }

        public String getDescColor() {
            return descColor;
        }

        public String getNameColor() {
            return nameColor;
        }

        public String getSummaryColor() {
            return summaryColor;
        }

        public List<AssetElement> getAssets() {
            return assets;
        }

        public String getSummary() {
            return summary;
        }

        @Override
        public boolean from(JSONObject json) {
            boolean res = false;
            if(super.from(json)) {
                eid = json.optString("eid");
                position = json.optString("position");
                tbgColor = json.optString("tbgColor");
                elementType = json.optInt("elementType");
                description = json.optString("description");
                content = json.optString("content");
                title = json.optString("title");
                linkType = json.optInt("linkType");
                summary = json.optString("summary");

                nameColor = json.optString("nameColor");
                descColor = json.optString("descColor");
                summaryColor = json.optString("summaryColor");

                if(elementType == 0) {
                    setPicUrl(content);
                }

//                tags.clear();
//                JSONArray tagArray = json.optJSONArray("tags");
//                if(tagArray != null) {
//                    for(int i = 0; i < tagArray.length();i++) {
//                        TagElement tagElement = new TagElement();
//                        tagElement.from(tagArray.optJSONObject(i));
//                        tags.add(tagElement);
//                    }
//                }

                assets.clear();
                JSONArray assetArray = json.optJSONArray("assetsList");
                if(assetArray != null) {
                    for(int i = 0;i < assetArray.length();i++) {
                        AssetElement assetElement = new AssetElement();
                        assetElement.from(assetArray.optJSONObject(i));
                        assets.add(assetElement);
                    }
                }
                res = true;
            }
            return res;
        }
    }

    //资产数据类
    public class AssetElement extends BaseElement implements Serializable {
        /**
         */
        private static final long serialVersionUID = 1L;
        
        private int catId;
        private String id;
        private String title;
        private String summary;
        private String datetime;
        private String assetType;
        private String bgImage;
        /**增加豆瓣评分字段**/
        private String points;
        /**增加角标字段**/
        private String cmark;
        /**CS模板请求url**/
        private String csJsonUrl;
        /**CS模板layoutcode**/
        private String csLayout;
        private String catName;
        private int total;
        private String playUrl;
        //增加时间字段
        private String time;

        private List<Detail> details = new ArrayList<Detail>();

        public AssetElement() {

        }

        public int getCatId() {
            return catId;
        }

        public String getId() {
            return id;
        }

        public String getTitle() {
            return title;
        }

        public String getSummary() {
            return summary;
        }

        public String getDatetime() {
            return datetime;
        }

        public String getCatName() {
            return catName;
        }

        public int getTotal() {
            return total;
        }

        public String getBgImage() {
            return bgImage;
        }

        public String getAssetType() {
            return assetType;
        }

        public String getPlayUrl() {
            return playUrl;
        }

        public void setPlayUrl(String playUrl) {
            this.playUrl = playUrl;
        }

        public List<Detail> getDetails() {
            return details;
        }

        public String getPoints() {
			return points;
		}

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

		public void setCmark(String cmark) {
			this.cmark = cmark;
		}

        public String getCsJsonUrl() {
            return csJsonUrl;
        }

        public String getCsLayout() {
            return csLayout;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        @Override
        public boolean from(JSONObject json) {
            if(super.from(json)) {
                id = json.optString("id");
                title = json.optString("title");
                time = json.optString("time");
                summary = json.optString("summary");
                datetime = json.optString("datetime");
                assetType = json.optString("assetType");
                catId = json.optInt("catId");
                catName = json.optString("catName");
                total = json.optInt("total");
                bgImage = json.optString("bgImage");
                points = json.optString("points");
                cmark = json.optString("cmark");
                csJsonUrl = json.optString("csJsonUrl" ,"");
                csLayout = json.optString("csLayout" ,"");

                details.clear();
                JSONArray detailArray = json.optJSONArray("details");
                if(detailArray != null) {
                    for(int i = 0;i < detailArray.length();i++) {
                        Detail detail = new Detail();
                        detail.from(detailArray.optJSONObject(i));
                        details.add(detail);
                    }
                }

                return true;
            }
            return false;
        }

    }

    public class Detail extends BaseElement implements Serializable {
        /**
         */
        private static final long serialVersionUID = 1L;

        @Override
        public boolean from(JSONObject json) {
            return super.from(json);
        }
    }
}
