package com.wasu.cs.model;

import android.os.Parcel;
import android.os.Parcelable;

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;

/**
 * 二级列表
 * @author zhangrm@wasu.com
 */
public class DemandList extends ObjectBase implements Parcelable, IAssetList {

	/**
     */
    private static final long serialVersionUID = 1L;
    private static final String FIELD_CODE = "code";
	private static final String FIELD_MESSAGE = "message";
	private static final String FIELD_DATA = "data";

	private int mCode;
	private String mMessage;
	private Datum mDatum;

	public DemandList() {

	}

	public void setCode(int code) {
		mCode = code;
	}

	public int getCode() {
		return mCode;
	}

	public void setMessage(String message) {
		mMessage = message;
	}

	public String getMessage() {
		return mMessage;
	}

	public void setDatum(Datum datum) {
		mDatum = datum;
	}

	public Datum getDatum() {
		return mDatum;
	}

	public DemandList(Parcel in) {
		mCode = in.readInt();
		mMessage = in.readString();
		mDatum = in.readParcelable(Datum.class.getClassLoader());
	}

	@Override
	public int describeContents() {
		return 0;
	}

	public static final Parcelable.Creator<DemandList> CREATOR = new Parcelable.Creator<DemandList>() {
		public DemandList createFromParcel(Parcel in) {
			return new DemandList(in);
		}

		public DemandList[] newArray(int size) {
			return new DemandList[size];
		}
	};

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(mCode);
		dest.writeString(mMessage);
		dest.writeParcelable(mDatum, flags);
	}

	@Override
	public String toString() {
		return "code = " + mCode + ", message = " + mMessage + ", datum = " + mDatum;
	}

	@Override
	public void createFromResponse(String arg0) throws DataFetchException {
		try {
			JSONObject json = new JSONObject(arg0);
			
			int code = json.getInt(FIELD_CODE);
			if (code != 200) {
				throw new JSONException("");
			}
			
			this.mCode = code;
			this.mMessage = json.getString(FIELD_MESSAGE);
			
			JSONObject data = json.getJSONObject(FIELD_DATA);
			Datum datum = new Datum();
			datum.createFromResponse(data.toString());
			this.mDatum = datum;
			
		} catch (JSONException e) {
			e.printStackTrace();
			throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
		}
	}
	
	public static class Datum extends ObjectBase implements Parcelable {

		/**
         */
        private static final long serialVersionUID = 1L;
        private static final String FIELD_BG_COLOR = "bgColor";
		private static final String FIELD_BG_IMAGE = "bgImage";
		private static final String FIELD_NAME_COLOR = "nameColor";
		private static final String FIELD_ASSETS = "assets";
		private static final String FIELD_MORE = "more";
		private static final String FIELD_CAT_NAME = "catName";
		private static final String FIELD_CAT_ID = "catId";
		private static final String FIELD_PAGE = "page";
		private static final String FIELD_SCENE = "scene";
		private static final String FIELD_TOTAL = "total";

		private String mBgColor;
		private String mBgImage;
		private String mNameColor;
		private List<Asset> mAssets;
		private More mMore;
		private String mCatName;
		private int mCatId;
		private int mPage;
		private int mScene;
		private int mTotal;

		public Datum() {

		}

		public void setBgColor(String bgColor) {
			mBgColor = bgColor;
		}

		public String getBgColor() {
			return mBgColor;
		}

		public void setBgImage(String bgImage) {
			mBgImage = bgImage;
		}

		public String getBgImage() {
			return mBgImage;
		}

		public void setNameColor(String nameColor) {
			mNameColor = nameColor;
		}

		public String getNameColor() {
			return mNameColor;
		}

		public void setAssets(List<Asset> assets) {
			mAssets = assets;
		}

		public List<Asset> getAssets() {
			return mAssets;
		}

		public void setMore(More more) {
			mMore = more;
		}

		public More getMore() {
			return mMore;
		}

		public void setCatName(String catName) {
			mCatName = catName;
		}

		public String getCatName() {
			return mCatName;
		}

		public void setCatId(int catId) {
			mCatId = catId;
		}

		public int getCatId() {
			return mCatId;
		}

		public void setPage(int page) {
			mPage = page;
		}

		public int getPage() {
			return mPage;
		}

		public void setScene(int scene) {
			mScene = scene;
		}

		public int getScene() {
			return mScene;
		}

		public void setTotal(int total) {
			mTotal = total;
		}

		public int getTotal() {
			return mTotal;
		}

		public Datum(Parcel in) {
			mBgColor = in.readString();
			mBgImage = in.readString();
			mNameColor = in.readString();
			mAssets = new ArrayList<Asset>();
			in.readTypedList(mAssets, Asset.CREATOR);
			mMore = in.readParcelable(More.class.getClassLoader());
			mCatName = in.readString();
			mCatId = in.readInt();
			mPage = in.readInt();
			mScene = in.readInt();
			mTotal = in.readInt();
		}

		@Override
		public int describeContents() {
			return 0;
		}

		public static final Parcelable.Creator<Datum> CREATOR = new Parcelable.Creator<Datum>() {
			public Datum createFromParcel(Parcel in) {
				return new Datum(in);
			}

			public Datum[] newArray(int size) {
				return new Datum[size];
			}
		};

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(mBgColor);
			dest.writeString(mBgImage);
			dest.writeString(mNameColor);
			dest.writeTypedList(mAssets);
			dest.writeParcelable(mMore, flags);
			dest.writeString(mCatName);
			dest.writeInt(mCatId);
			dest.writeInt(mPage);
			dest.writeInt(mScene);
			dest.writeInt(mTotal);
		}

		@Override
		public String toString() {
			return "bgColor = " + mBgColor + ", bgImage = " + mBgImage + ", nameColor = " + mNameColor + ", assets = " + mAssets + ", more = " + mMore + ", catName = " + mCatName + ", catId = " + mCatId + ", page = " + mPage + ", scene = " + mScene + ", total = " + mTotal;
		}

		@Override
		public void createFromResponse(String arg0) throws DataFetchException {
			try {
				JSONObject json = new JSONObject(arg0);
				
				JSONArray assets = json.getJSONArray(FIELD_ASSETS);
				List<Asset> list = new ArrayList<Asset>();
				for (int i = 0; i < assets.length(); i++) {
					Asset asset = new Asset();
					asset.createFromResponse(assets.getJSONObject(i).toString());
					list.add(asset);
				}
				this.mAssets = list;
				
				this.mBgColor = json.getString(FIELD_BG_COLOR);
				this.mBgImage = json.getString(FIELD_BG_IMAGE);
				this.mCatId = json.getInt(FIELD_CAT_ID);
				this.mCatName = json.getString(FIELD_CAT_NAME);
				
				JSONObject more = json.getJSONObject(FIELD_MORE);
				More m = new More();
				m.createFromResponse(more.toString());
				this.mMore = m;
				
				this.mNameColor = json.getString(FIELD_NAME_COLOR);
				this.mPage = json.getInt(FIELD_PAGE);
				this.mScene = json.getInt(FIELD_SCENE);
				this.mTotal = json.getInt(FIELD_TOTAL);
				
			} catch (JSONException e) {
				e.printStackTrace();
				throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
			}
		}

	}
	
	public static class Asset extends ObjectBase implements Parcelable {

		/**
         */
        private static final long serialVersionUID = 1L;
        
        private static final String FIELD_ID = "id";
		private static final String FIELD_JSON_URL = "jsonUrl";
		private static final String FIELD_DATETIME = "datetime";
		private static final String FIELD_SUMMARY = "summary";
		private static final String FIELD_TITLE = "title";
		private static final String FIELD_PIC_URL = "picUrl";
		private static final String FIELD_CAT_ID = "catId";
		private static final String FIELD_LAYOUT = "layout";

		private long mId;
		private String mJsonUrl;
		private String mDatetime;
		private String mSummary;
		private String mTitle;
		private String mPicUrl;
		private int mCatId;
		private String mLayout;

		public Asset() {

		}

		public void setId(long id) {
			mId = id;
		}

		public long getId() {
			return mId;
		}

		public void setJsonUrl(String jsonUrl) {
			mJsonUrl = jsonUrl;
		}

		public String getJsonUrl() {
			return mJsonUrl;
		}

		public void setDatetime(String datetime) {
			mDatetime = datetime;
		}

		public String getDatetime() {
			return mDatetime;
		}

		public void setSummary(String summary) {
			mSummary = summary;
		}

		public String getSummary() {
			return mSummary;
		}

		public void setTitle(String title) {
			mTitle = title;
		}

		public String getTitle() {
			return mTitle;
		}

		public void setPicUrl(String picUrl) {
			mPicUrl = picUrl;
		}

		public String getPicUrl() {
			return mPicUrl;
		}

		public void setCatId(int catId) {
			mCatId = catId;
		}

		public int getCatId() {
			return mCatId;
		}

		public void setLayout(String layout) {
			mLayout = layout;
		}

		public String getLayout() {
			return mLayout;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof Asset) {
				return ((Asset) obj).getId() == mId;
			}
			return false;
		}

		@Override
		public int hashCode() {
			return ((Long) mId).hashCode();
		}

		public Asset(Parcel in) {
			mId = in.readLong();
			mJsonUrl = in.readString();
			mDatetime = in.readString();
			mSummary = in.readString();
			mTitle = in.readString();
			mPicUrl = in.readString();
			mCatId = in.readInt();
			mLayout = in.readString();
		}

		@Override
		public int describeContents() {
			return 0;
		}

		public static final Parcelable.Creator<Asset> CREATOR = new Parcelable.Creator<Asset>() {
			public Asset createFromParcel(Parcel in) {
				return new Asset(in);
			}

			public Asset[] newArray(int size) {
				return new Asset[size];
			}
		};

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeLong(mId);
			dest.writeString(mJsonUrl);
			dest.writeString(mDatetime);
			dest.writeString(mSummary);
			dest.writeString(mTitle);
			dest.writeString(mPicUrl);
			dest.writeInt(mCatId);
			dest.writeString(mLayout);
		}

		@Override
		public String toString() {
			return "id = " + mId + ", jsonUrl = " + mJsonUrl + ", datetime = " + mDatetime + ", summary = " + mSummary + ", title = " + mTitle + ", picUrl = " + mPicUrl + ", catId = " + mCatId + ", layout = " + mLayout;
		}

		@Override
		public void createFromResponse(String arg0) throws DataFetchException {
			try {
				JSONObject json = new JSONObject(arg0);
				
				this.mCatId = json.getInt(FIELD_CAT_ID);
				this.mDatetime = json.getString(FIELD_DATETIME);
				this.mId = json.getLong(FIELD_ID);
				this.mJsonUrl = json.getString(FIELD_JSON_URL);
				this.mLayout = json.getString(FIELD_LAYOUT);
				this.mPicUrl = json.getString(FIELD_PIC_URL);
				this.mSummary = json.getString(FIELD_SUMMARY);
				this.mTitle = json.getString(FIELD_TITLE);
				
			} catch (JSONException e) {
				e.printStackTrace();
				throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
			}
		}

	}
	
	public static class More extends ObjectBase implements Parcelable {

		/**
         */
        private static final long serialVersionUID = 1L;
        private static final String FIELD_JSON_URL = "jsonUrl";
		private static final String FIELD_PIC_URL = "picUrl";
		private static final String FIELD_LAYOUT = "layout";

		private String mJsonUrl;
		private String mPicUrl;
		private String mLayout;

		public More() {

		}

		public void setJsonUrl(String jsonUrl) {
			mJsonUrl = jsonUrl;
		}

		public String getJsonUrl() {
			return mJsonUrl;
		}

		public void setPicUrl(String picUrl) {
			mPicUrl = picUrl;
		}

		public String getPicUrl() {
			return mPicUrl;
		}

		public void setLayout(String layout) {
			mLayout = layout;
		}

		public String getLayout() {
			return mLayout;
		}

		public More(Parcel in) {
			mJsonUrl = in.readString();
			mPicUrl = in.readString();
			mLayout = in.readString();
		}

		@Override
		public int describeContents() {
			return 0;
		}

		public static final Parcelable.Creator<More> CREATOR = new Parcelable.Creator<More>() {
			public More createFromParcel(Parcel in) {
				return new More(in);
			}

			public More[] newArray(int size) {
				return new More[size];
			}
		};

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(mJsonUrl);
			dest.writeString(mPicUrl);
			dest.writeString(mLayout);
		}

		@Override
		public String toString() {
			return "jsonUrl = " + mJsonUrl + ", picUrl = " + mPicUrl + ", layout = " + mLayout;
		}

		@Override
		public void createFromResponse(String arg0) throws DataFetchException {
			try {
				JSONObject json = new JSONObject(arg0);
				
				this.mJsonUrl = json.getString(FIELD_JSON_URL);
				this.mLayout = json.getString(FIELD_LAYOUT);
				this.mPicUrl = json.getString(FIELD_PIC_URL);
				
			} catch (JSONException e) {
				e.printStackTrace();
				throw new DataFetchException(WError.ERROR_INVALID_DATA, null);
			}
		}

	}

	@Override
	public String getJsonUrl(int index) {
		Datum data = getDatum();
		if (data == null) {
			return null;
		}
		
		List<Asset> assets = data.getAssets();
		if (assets == null) {
			return null;
		}

		if (index >= assets.size()) {
			return null;
		}
		
		Asset asset = assets.get(index);
		if (asset == null) {
			return null;
		}
		
		
		return asset.getJsonUrl();
	}

	@Override
	public int getSize() {
		Datum data = getDatum();
		if (data == null) {
			return 0;
		}
		
		return data.getTotal();
	}

	@Override
	public int getRealSize() {
		Datum data = getDatum();
		if (data == null) {
			return 0;
		}

		List<Asset> assets = data.getAssets();
		if (assets == null) {
			return 0;
		}

		return assets.size();
	}

	@Override
	public void append(IAssetList assetList) {
		if (assetList == null) {
			return;
		}

		DemandList demandList = (DemandList) assetList;

		if (demandList.getDatum() == null || demandList.getDatum().getAssets() == null) {
			return;
		}

		if (getDatum() == null || getDatum().getAssets() == null) {
			return;
		}

		getDatum().getAssets().addAll(demandList.getDatum().getAssets());
	}

	@Override
	public String getPoint(int index) {
		return "";
	}

	@Override
	public String getTitle(int index) {
		Datum data = getDatum();
		if (data == null) {
			return null;
		}

		List<Asset> assets = data.getAssets();
		if (assets == null) {
			return null;
		}

		if (index >= assets.size()) {
			return null;
		}

		Asset asset = assets.get(index);
		if (asset == null) {
			return null;
		}


		return asset.getTitle();
	}

	@Override
	public String getPicUrl(int index) {
		Datum data = getDatum();
		if (data == null) {
			return null;
		}

		List<Asset> assets = data.getAssets();
		if (assets == null) {
			return null;
		}

		if (index >= assets.size()) {
			return null;
		}

		Asset asset = assets.get(index);
		if (asset == null) {
			return null;
		}


		return asset.getPicUrl();
	}
}