package com.wasu.cs.mvp.model;

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

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

/**
 * @Description: 首页数据解析类
 * @Author: Danxingxi
 * @CreateDate: 2017/1/12 15:17
 */
public class MainHomeDataModel extends IBaseDataModel {

    /**广告条左侧数据**/
    private List<AssetsDataModel> bannerLeft = new ArrayList<>();
    /**广告条中间数据**/
    private List<AssetsDataModel> bannerMiddle = new ArrayList<>();
    /**广告条右侧数据**/
    private List<AssetsDataModel> bannerRight = new ArrayList<>();
    /**资产展示区数据**/
    private List<AssetsDataModel> bodyList = new ArrayList<>();
    /**有的banner只有一个list，不存在 左 中 右 之分**/
    private List<AssetsDataModel> bannerList = new ArrayList<>();

    /**
     * 解析Json数据
     * @param json
     * @return 成功返回true
     */
    @Override
    public boolean parseJson(JSONObject json) {

        if(super.parseJson(json)){

            json = json.optJSONObject("data");

            if(json != null){
                JSONObject bannerObj = json.optJSONObject("banner");
                JSONObject bodyObj = json.optJSONObject("body");

                if(bannerObj != null){  //banner部分

                    if(bannerObj.optJSONArray("list") != null){
                        JSONArray listArray = bannerObj.optJSONArray("list");
                        int arrayLength = listArray.length();
                        if(arrayLength>0){
                            for(int i=0;i<arrayLength;i++){
                                AssetsDataModel assetsModel = new AssetsDataModel();
                                if(assetsModel.parseJson(listArray.optJSONObject(i))){
                                    bannerList.add(assetsModel);
                                }
                            }
                        }
                    }

                    if(bannerObj.optJSONObject("left") != null){
                        JSONArray leftArray =  bannerObj.optJSONObject("left").optJSONArray("list");
                        if(leftArray!=null){
                            int arrayLength = leftArray.length();
                            if(arrayLength>0){
                                for(int i=0;i<arrayLength;i++){
                                    AssetsDataModel assetsModel = new AssetsDataModel();
                                    if(assetsModel.parseJson(leftArray.optJSONObject(i))){
                                        bannerLeft.add(assetsModel);
                                    }
                                }
                            }
                        }
                    }

                    if(bannerObj.optJSONObject("middle") != null){
                        JSONArray middleArray =  bannerObj.optJSONObject("middle").optJSONArray("list");
                        if(middleArray!=null){
                            int arrayLength = middleArray.length();
                            if(arrayLength>0){
                                for(int i=0;i<arrayLength;i++){
                                    AssetsDataModel assetsModel = new AssetsDataModel();
                                    if(assetsModel.parseJson(middleArray.optJSONObject(i))){
                                       bannerMiddle.add(assetsModel);
                                    }
                                }
                            }
                        }
                    }

                    if(bannerObj.optJSONObject("right") != null){
                        JSONArray rightArray =  bannerObj.optJSONObject("right").optJSONArray("list");
                        if(rightArray!=null){
                            int arrayLength = rightArray.length();
                            if(arrayLength>0){
                                for(int i=0;i<arrayLength;i++){
                                    AssetsDataModel assetsModel = new AssetsDataModel();
                                    if(assetsModel.parseJson(rightArray.optJSONObject(i))){
                                        bannerRight.add(assetsModel);
                                    }
                                }
                            }
                        }
                    }
                }

                if(bodyObj != null){  //body部分
                    JSONArray bodyArray =  bodyObj.optJSONArray("list");
                    if(bodyArray!=null){
                        int arrayLength = bodyArray.length();
                        if(arrayLength>0){
                            for(int i=0;i<arrayLength;i++){
                                AssetsDataModel assetsModel = new AssetsDataModel();
                                if(assetsModel.parseJson(bodyArray.optJSONObject(i))){
                                    bodyList.add(assetsModel);
                                }
                            }
                        }
                    }
                }
            }
            return true;
        }


        return false;
    }

    public List<AssetsDataModel> getBannerRight() {
        return bannerRight;
    }

    public List<AssetsDataModel> getBannerLeft() {
        return bannerLeft;
    }

    public List<AssetsDataModel> getBannerMiddle() {
        return bannerMiddle;
    }

    public List<AssetsDataModel> getBodyList() {
        return bodyList;
    }

    public List<AssetsDataModel> getBannerList() {
        return bannerList;
    }
}
