package com.wasu.cs.model;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.wasu.cs.model.LiveDemandProgram.Data.Channel;
import com.wasu.cs.model.LiveDemandProgram.Data.Channel.PlayUrl;
import com.wasu.module.datafetch.DataFetchException;
import com.wasu.module.datafetch.ObjectBase;
import com.wasu.module.log.WLog;

/**
 * 直播轮播数据
 *
 * @author zhangrm
 */
public class LiveDemandProgram extends ObjectBase implements ILiveAssets {

    private static final long serialVersionUID = 1L;

    private final static String TAG = "LiveDemandProgram";

    private int code;

    private String message;

    private Map<String, Data> data = new HashMap<String, Data>();

    public static final String PLAY_TYPE_HTTP = "1";
    public static final String PLAY_TYPE_P2P = "2";

    public class Data {

        private String type;
        private String name;
        private List<Channel> channelList;

        public class Channel {
            private int channelId;
            private String channelKey;
            private String channelName;
            private String picUrl;
            private String playType;
            private PlayUrl playUrl;
            private String playBackUrl;
            private String playBackBill;

            public class PlayUrl {
                private String httpUrl;
                private String p2pid;
                private String p2pserver;

                public String getHttpUrl() {
                    return httpUrl;
                }

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

                public String getP2pid() {
                    return p2pid;
                }

                public void setP2pid(String p2pid) {
                    this.p2pid = p2pid;
                }

                public String getP2pserver() {
                    return p2pserver;
                }

                public void setP2pserver(String p2pserver) {
                    this.p2pserver = p2pserver;
                }

            }

            public int getChannelId() {
                return channelId;
            }

            public void setChannelId(int channelId) {
                this.channelId = channelId;
            }

            public String getChannelKey() {
                return channelKey;
            }

            public void setChannelKey(String channelKey) {
                this.channelKey = channelKey;
            }

            public String getChannelName() {
                return channelName;
            }

            public void setChannelName(String channelName) {
                this.channelName = channelName;
            }

            public String getPicUrl() {
                return picUrl;
            }

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

            public String getPlayType() {
                return playType;
            }

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

            public PlayUrl getPlayUrl() {
                return playUrl;
            }

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

            public String getPlayBackUrl() {
                return playBackUrl;
            }

            public void setPlayBackUrl(String playBackUrl) {
                this.playBackUrl = playBackUrl;
            }

            public String getPlayBackBill() {
                return playBackBill;
            }

            public void setPlayBackBill(String playBackBill) {
                this.playBackBill = playBackBill;
            }

        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

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

        public List<Channel> getChannelList() {
            return channelList;
        }

        public void setChannelList(List<Channel> channelList) {
            this.channelList = channelList;
        }

    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Data> getData() {
        return data;
    }

    public void setData(Map<String, Data> data) {
        this.data = data;
    }

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

            code = o.optInt("code");
            message = o.optString("message");
            if (code != 200) {
                WLog.e(TAG, "解析出错," + message);
                return;
            }

            JSONObject data = o.optJSONObject("data");
            if (data == null) {
                WLog.e(TAG, "解析出错");
                return;
            }

            JSONObject data1 = data.optJSONObject("data1");
            JSONObject data2 = data.optJSONObject("data2");
            JSONObject data3 = data.optJSONObject("data3");

            this.data.put("data1", parseData(data1));
            this.data.put("data2", parseData(data2));
            this.data.put("data3", parseData(data3));

            integrateData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Data parseData(JSONObject data) {
        if (data == null) {
            return null;
        }

        Data ret = new Data();

        ret.setType(data.optString("type"));
        ret.setName(data.optString("name"));

        List<Channel> channelList = new ArrayList<Channel>();

        JSONArray a = data.optJSONArray("channelList");
        int size = a.length();
        for (int i = 0; i < size; i++) {
            JSONObject o = a.optJSONObject(i);
            if (o == null) {
                continue;
            }

            Channel channel = ret.new Channel();
            channel.setChannelId(o.optInt("channelId"));
            channel.setChannelKey(o.optString("channelKey"));
            channel.setChannelName(o.optString("channelName"));
            channel.setPicUrl(o.optString("picUrl"));
            channel.setPlayType(o.optString("playType"));

            JSONObject o1 = o.optJSONObject("playUrl");

            if (o1 != null) {
                PlayUrl playUrl = channel.new PlayUrl();
                playUrl.setHttpUrl(o1.optString("httpUrl"));
                playUrl.setP2pid(o1.optString("p2pid"));
                playUrl.setP2pserver(o1.optString("p2pserver"));

                channel.setPlayUrl(playUrl);
            }

            channel.setPlayBackUrl(o.optString("playBackUrl"));
            channel.setPlayBackBill(o.optString("playBackBill"));

            channelList.add(channel);
        }

        ret.setChannelList(channelList);

        return ret;
    }

    private List<Channel> liveChannels = new ArrayList<Channel>();
    private List<Channel> roundChannels = new ArrayList<Channel>();

    private void integrateData() {
        if (this.data.size() == 0) {
            return;
        }

        Data data1 = this.data.get("data1");
        Data data2 = this.data.get("data2");
        Data data3 = this.data.get("data3");

        if (data1 != null) {
            liveChannels.addAll(data1.getChannelList());
        }

        if (data2 != null) {
            liveChannels.addAll(data2.getChannelList());
        }

        if (data3 != null) {
            roundChannels.addAll(data3.getChannelList());
        }
    }

    private List<Channel> getChannelList() {
        if (liveChannels.isEmpty()) {
            return roundChannels;
        } else {
            return liveChannels;
        }
    }

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

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

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

        List<Channel> list = getChannelList();

        for (int i = 0; i < list.size(); i++) {
            Channel channel = list.get(i);
            if (channelId.equals(String.valueOf(channel.getChannelId()))) {
                ret = i;
                break;
            }
        }

        return ret;
    }

    @Override
    public String getChannelId(int index) {
        Channel channel = getChannelList().get(index);
        if (channel != null) {
            return String.valueOf(channel.getChannelId());
        }
        return null;
    }

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

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

    @Override
    public String getChannelName(String channelId) {
        if (TextUtils.isEmpty(channelId)) {
            return null;
        }

        List<Channel> channels = getChannelList();
        for (Channel channel : channels) {
            if (channelId.equals(String.valueOf(channel.getChannelId()))) {
                return channel.getChannelName();
            }
        }
        return null;
    }

    @Override
    public Type getType(String channelId) {
        if (TextUtils.isEmpty(channelId)) {
            return null;
        }

        List<Channel> channels = getChannelList();
        for (Channel channel : channels) {
            if (channelId.equals(String.valueOf(channel.getChannelId()))) {
                if (PLAY_TYPE_HTTP.equals(channel.getPlayType())) {
                    return Type.HTTP;
                } else if (PLAY_TYPE_P2P.equals(channel.getPlayType())) {
                    return Type.P2P;
                }
            }
        }
        return null;
    }

    @Override
    public String getHttpUrl(String channelId) {
        if (TextUtils.isEmpty(channelId)) {
            return null;
        }

        List<Channel> channels = getChannelList();
        for (Channel channel : channels) {
            if (channelId.equals(String.valueOf(channel.getChannelId()))) {
                if (channel.getPlayUrl() == null) {
                    break;
                }

                return channel.getPlayUrl().getHttpUrl();
            }
        }
        return null;
    }

    @Override
    public String getP2pChannelId(String channelId) {
        if (TextUtils.isEmpty(channelId)) {
            return null;
        }

        List<Channel> channels = getChannelList();
        for (Channel channel : channels) {
            if (channelId.equals(String.valueOf(channel.getChannelId()))) {
                if (channel.getPlayUrl() == null) {
                    break;
                }

                return channel.getPlayUrl().getP2pid();
            }
        }
        return null;
    }

    @Override
    public String getP2pFccs(String channelId) {
        if (TextUtils.isEmpty(channelId)) {
            return null;
        }

        List<Channel> channels = getChannelList();
        for (Channel channel : channels) {
            if (channelId.equals(String.valueOf(channel.getChannelId()))) {
                if (channel.getPlayUrl() == null) {
                    break;
                }

                return channel.getPlayUrl().getP2pserver();
            }
        }
        return null;
    }
}
