package com.wasu.cs.model;

import android.text.TextUtils;

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;

/**
 * Created by zhangrm on 16-3-11.
 */
public class LivePlayInfo extends ObjectBase implements ILiveAssets {

    public static final String PLAY_TYPE_WASU_LIVE = "1";//华数直播
    public static final String PLAY_TYPE_TV_BUG = "2";//电视购物直播

    public static final String CDN_TYPE_HTTP = "0";//http优先
    public static final String CDN_TYPE_P2P = "1";//p2p优先

    private List<PlayInfo> channels = new ArrayList<PlayInfo>();

    public List<PlayInfo> getChannels() {
        return channels;
    }

    public LivePlayInfo(String json) throws DataFetchException {
        createFromResponse(json);
    }

    @Override
    public void createFromResponse(String s) throws DataFetchException {
        try {
            JSONObject o = new JSONObject(s);

            JSONArray a = o.optJSONArray("playInfo");

            if (a == null) {
                return;
            }

            int size = a.length();
            for (int i = 0; i < size; i++) {
                JSONObject item = a.optJSONObject(i);

                if (item == null) {
                    continue;
                }

                PlayInfo playInfo = new PlayInfo();
                playInfo.setPlayType(item.optString("playType"));
                playInfo.setResourceName(item.optString("resourceName"));
                playInfo.setResourceId(item.optString("resourceId"));
                playInfo.setCdnType(item.optString("cdnType"));

                JSONObject playUrl = item.optJSONObject("playUrl");
                if (playUrl == null) {
                    continue;
                }

                playInfo.setHttpUrl(playUrl.optString("httpUrl"));
                playInfo.setChannelID(playUrl.optString("channelID"));
                playInfo.setFccs(playUrl.optString("fccs"));

                channels.add(playInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    class PlayInfo {
        private String playType;
        private String resourceName;
        private String resourceId;
        private String cdnType;
        private String httpUrl;
        private String channelID;
        private String fccs;

        public String getPlayType() {
            return playType;
        }

        public void setPlayType(String playType) {
            this.playType = playType;
        }

        public String getResourceName() {
            return resourceName;
        }

        public void setResourceName(String resourceName) {
            this.resourceName = resourceName;
        }

        public String getResourceId() {
            return resourceId;
        }

        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        public String getCdnType() {
            return cdnType;
        }

        public void setCdnType(String cdnType) {
            this.cdnType = cdnType;
        }

        public String getHttpUrl() {
            return httpUrl;
        }

        public void setHttpUrl(String httpUrl) {
            this.httpUrl = httpUrl;
        }

        public String getChannelID() {
            return channelID;
        }

        public void setChannelID(String channelID) {
            this.channelID = channelID;
        }

        public String getFccs() {
            return fccs;
        }

        public void setFccs(String fccs) {
            this.fccs = fccs;
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////

    @Override
    public int getChannelSize() {
        return channels.size();
    }

    @Override
    public String getChannelId(int index) {
        PlayInfo playInfo = channels.get(index);
        if (playInfo != null) {
            return playInfo.getResourceId();
        }
        return null;
    }

    @Override
    public int getIndexByChanenlId(String channelId) {
        int ret = 0;
        if (TextUtils.isEmpty(channelId)) {
            return ret;
        }

        int size = channels.size();
        for (int i = 0; i < size; i++) {
            PlayInfo playInfo = channels.get(i);
            if (channelId.equals(playInfo.getResourceId())) {
                ret = i;
                break;
            }
        }

        return ret;
    }

    @Override
    public String getNextChannelId(String channelId) {
        int index = getIndexByChanenlId(channelId);
        int next = (index + 1) % channels.size();
        return channels.get(next).getResourceId();
    }

    @Override
    public String getPreviousChannelId(String channelId) {
        int index = getIndexByChanenlId(channelId);
        int previous = (index + channels.size() - 1) % channels.size();
        return channels.get(previous).getResourceId();
    }

    @Override
    public String getChannelName(String channelId) {
        for (PlayInfo playInfo : channels) {
            if (channelId.equals(playInfo.getResourceId())) {
                return playInfo.getResourceName();
            }
        }
        return null;
    }

    @Override
    public Type getType(String channelId) {
        for (PlayInfo playInfo : channels) {
            if (channelId.equals(playInfo.getResourceId())) {
                if (CDN_TYPE_HTTP.equals(playInfo.getCdnType())) {
                    return Type.HTTP;
                } else if (CDN_TYPE_P2P.equals(playInfo.getCdnType())) {
                    return Type.P2P;
                }
            }
        }
        return null;
    }

    @Override
    public String getHttpUrl(String channelId) {
        for (PlayInfo playInfo : channels) {
            if (channelId.equals(playInfo.getResourceId())) {
                return playInfo.getHttpUrl();
            }
        }
        return null;
    }

    @Override
    public String getP2pChannelId(String channelId) {
        for (PlayInfo playInfo : channels) {
            if (channelId.equals(playInfo.getResourceId())) {
                return playInfo.getChannelID();
            }
        }
        return null;
    }

    @Override
    public String getP2pFccs(String channelId) {
        for (PlayInfo playInfo : channels) {
            if (channelId.equals(playInfo.getResourceId())) {
                return playInfo.getFccs();
            }
        }
        return null;
    }
}
