package com.wasu.cs.model;

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

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

import com.wasu.util.StringUtils;

@SuppressWarnings("serial")
public class TagModel extends Model {
	private int rate;
	private String tag;
	private List<VideoModel> videoList = new ArrayList<VideoModel>();

	@Override
	public boolean from(JSONObject json) {
		boolean result = super.from(json);

		if (result) {
			rate = json.optInt("rate", 0);
			tag = json.optString("tag", "");

			JSONArray jsonArr = json.optJSONArray("items");
			if (jsonArr != null && jsonArr.length() > 0) {
				VideoModel item;
				for (int i = 0, len = jsonArr.length(); i < len; i++) {
					item = new VideoModel();
					if (item.from(jsonArr.optJSONObject(i))) {
						videoList.add(item);
					}
				}
			}
		}

		return result;
	}

	public int getRate() {
		return rate;
	}

	public String getTag() {
		return tag;
	}

	public List<VideoModel> getVideoList() {
		return videoList;
	}
	
	public static class VideoModel extends Model {
		private String datetime;
		private int episode;
		private int itemId;
		private String title;
		private List<String> playUrls = new ArrayList<String>();
        private String picUrl;

        @Override
		public boolean from(JSONObject json) {
			boolean result = super.from(json);

			if (result) {
				datetime = json.optString("datetime", "");
				episode = json.optInt("episode", 0);
				itemId = json.optInt("itemId", 0);
				title = json.optString("title", "");
                picUrl = json.optString("picUrl");
				
				JSONArray jsonArr = json.optJSONArray("playUrls");
				if(jsonArr != null && jsonArr.length() > 0) {
					String playUrl;
					for(int i=0,len=jsonArr.length(); i<len; i++) {
						playUrl = jsonArr.optString(i);
						if(!StringUtils.isBlank(playUrl)) {
							playUrls.add(playUrl);
						}
					}
				}
			}

			return result;
		}

		public String getDatetime() {
			return datetime;
		}

		public int getEpisode() {
			return episode;
		}

		public int getItemId() {
			return itemId;
		}

		public String getTitle() {
			return title;
		}

		public List<String> getPlayUrls() {
			return playUrls;
		}

        public String getPicUrl() {
            return picUrl;
        }
    }
}
