package com.wasu.cs.ui;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.ViewGroup;

import com.sohutv.tv.player.partner.SohuTvPlayer;
import com.wasu.authsdk.AuthListener;
import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.entity.PriceInfo;
import com.wasu.authsdk.help.Tools;
import com.wasu.common.WError;
import com.wasu.comp.userlogin.DialogLogin;
import com.wasu.comp.videoview.IMediaControl;
import com.wasu.comp.videoview.IMediaListener;
import com.wasu.comp.videoview.UrlProperty;
import com.wasu.compfactory.WasuCompFactory;
import com.wasu.cs.model.DemandList;
import com.wasu.cs.model.DemandPlayinfo;
import com.wasu.cs.model.DemandProgram;
import com.wasu.cs.model.DemandSeries;
import com.wasu.cs.model.IAssetList;
import com.wasu.cs.widget.mediacontrol.DialogPlanBuy;
import com.wasu.cs.widget.mediacontrol.MediaController;
import com.wasu.cs.widget.videoview.ExtPlayer;
import com.wasu.cs.widget.videoview.PlayerParams;
import com.wasu.cs.widget.videoview.WasuPlayerView;
import com.wasu.module.datafetch.DataFetchListener.ObjectListener;
import com.wasu.module.datafetch.DataFetchModule;
import com.wasu.module.datafetch.ObjectBase;
import com.wasu.module.db.DBManage;
import com.wasu.module.log.WLog;
import com.wasu.statistics.PlayInfo;
import com.wasu.statistics.PlayerStatistics;
import com.wasu.statistics.WasuStatistics;
import com.wasu.util.NetUtils;
import com.wasu.util.VersionUtils;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import basic.BuilderTypeManager.BuildType;
import basic.db.model.DBProgramHistory;
import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.Common;
import cn.com.wasu.main.IntentConstant;

public class ActivityPlayer extends ActivityBase implements IMediaListener {
    public static final String ACTION = "com.wasu.action.play";
    /**
     * 资产信息key
     */
    public static final String ASSET_INFO = IntentConstant.PROGRAM_INFO.value();
    /**
     * 联播数据
     */
    public static final String ASSET_LIST = "asset_list";
    /**
     * 获取资产地址key
     */
    public static final String ASSET_URL = IntentConstant.DATAURI.value();
    /**
     * 指定当前播放的码率
     */
    public static final String ASSET_BITRATE = "bitrate";
    /**
     * 播放器类型, 0-正常，1-列表联播
     */
    public static final String PLAY_TYPE = IntentConstant.PLAY_TYPE.value();
    /**
     * 列表index
     */
    public static final String PLAY_INDEX = IntentConstant.PLAY_INDEX.value();
    /**
     * 大数据资产标识
     */
    public static final String TRACEID = "traceid";

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

    public static final long PLAY_RATE_NORMAL_H = 2500000;
    public static final long PLAY_RATE_NORMAL_L = 1300000;
    public static final long PLAY_RATE_BD = 3800000;
    public static final long PLAY_RATE_4K = 6000000;

    /**
     * 默认码率1.3m
     */
    public static long DEFAULT_PLAY_RATE = PLAY_RATE_NORMAL_L;

    public static final int PLAY_TYPE_NORMAL = 0;
    /**
     * 连播
     */
    public static final int PLAY_TYPE_LOOP = 1;
    /**
     * 连播分页条数
     */
    private static final int PSIZE = 50;

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

    private static final String TAG = "ActivityPlayer";

    private static final int STATUS_INIT = 0;
    private static final int STATUS_PARSE_INTENT = 1;
    private static final int STATUS_FETCH_ASSET_INFO = 2;
    private static final int STATUS_QUERY_PRICE = 3;
    private static final int STATUS_ADJUST_PLAY_BITRATE = 4;
    private static final int STATUS_QUERY_HISTORY = 5;
    private static final int STATUS_PLAY_AUTH = 6;
    private static final int STATUS_DONE = 7;


    //
    private static final int MAX_LOOP_CNT = 10;

    /**
     * 当前状态
     */
    private int currentStatus;

    /**
     * 下一个流程
     */
    private static final int PROCESS_NEXT = 0;
    /**
     * 上一个流程
     */
    private static final int PROCESS_PREVIOUS = 1;

    private int series = -1;
    private long currentPosition = 0;
    private boolean isFullPlay = false;

    private Handler processHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            if (msg.what == PROCESS_NEXT) {
                switch (currentStatus) {

                    case STATUS_INIT:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        parseIntent();
                        break;
                    case STATUS_PARSE_INTENT:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        if (assetInfo == null) {
                            try {
                                fetchAssetInfo();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            addDataToStatistics(assetInfo);
                            queryPrice();
                        }
                        break;
                    case STATUS_FETCH_ASSET_INFO:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        queryPrice();
                        break;
                    case STATUS_QUERY_PRICE:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        adjustPlayBitrate();
                        break;
                    case STATUS_ADJUST_PLAY_BITRATE:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        queryHistory();
                        break;
                    case STATUS_QUERY_HISTORY:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        playAuth();
                        break;
                    case STATUS_PLAY_AUTH:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);
                        }
                        play();
                        break;
                    case STATUS_DONE:
                        if (mediaController != null) {
                            mediaController.showBufferView();
                            mediaController.setReady(false);

                            mediaController.setAssetInfo(assetInfo);
                            mediaController.setAssetUrl(assetUrl);
                            mediaController.setPlayType(playType);
                            mediaController.setPlayIndex(playIndex);
                            mediaController.setAssetList(assetList);
                            mediaController.setTraceid(traceid);
                        }
                        break;

                }
            } else if (msg.what == PROCESS_PREVIOUS) {
                // TODO
            }

        }

    };

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

    /**
     * 播放器
     */
    private WasuPlayerView player;
    /**
     * 播控组件
     */
    private MediaController mediaController;

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

    /**
     * 资产信息
     */
    private DemandProgram assetInfo;
    /**
     * 获取资产地址
     */
    private String assetUrl;
    /**
     * 播放类型
     */
    private int playType;
    /**
     * 联播list
     */
    private IAssetList assetList;
    /**
     * 列表index
     */
    private int playIndex;
    /**
     * 接收外部传进来的traceid，如果为空，则是非大数据推荐的影片
     */
    private String traceid;

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

    /**
     * UrlProperty
     */
    private UrlProperty urlProperty;
    /**
     * 当前播放资产原始价格
     */
    private double mOriginPrice = 0;
    /**
     * 资产播放历史
     */
    private DBProgramHistory assetHistory;
    /**
     * 错误重试次数
     */
    private int errorRetryCount = 3;

    /**
     * 播放器准备好时，是否显示续播的消息
     */
    private boolean isShowSeekMsg;

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

    @Override
    protected void doCreate(Bundle savedInstanceState) {
        WLog.i(TAG,"doCreate()");

        Intent intent = getIntent();
        currentPosition = intent.getLongExtra(IntentConstant.LAST_PLAY_TIME.value(), 0);
        series = intent.getIntExtra(IntentConstant.LAST_SERIES.value(), 0);
        isFullPlay = intent.getBooleanExtra(IntentConstant.Full_Play.value(), false);

        configurePlayer();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {

        /**
         * 1.先检查intent中数据是否合法
         * 2.如果assetInfo为空，根据assetUrl获取assetInfo
         * 3.从数据库读取播放历史
         * 4.播放鉴权，免费资产异步鉴权，收费同步鉴权，失败退出播放
         */
        currentStatus = STATUS_INIT;
        processHandler.sendEmptyMessage(PROCESS_NEXT);

        super.onPostCreate(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {

        if (currentStatus != STATUS_DONE) {
            return;
        }

        /**
         * 1.保存历史记录，结束当前播放
         * 2.将media controller清除状态
         * 2.先检查intent中数据是否合法
         * 3.如果assetInfo为空，根据assetUrl获取assetInfo
         * 4.从数据库读取播放历史
         * 5.播放鉴权，免费资产异步鉴权，收费同步鉴权，失败退出播放
         */
        setIntent(intent);

        if (mediaController != null) {
            mediaController.clear();
        }

        currentStatus = STATUS_INIT;
        processHandler.sendEmptyMessage(PROCESS_NEXT);

        super.onNewIntent(intent);
    }

    @Override
    public void finish() {

        processHandler.removeCallbacksAndMessages(null);

        if (player != null && player.isPlaying()) {
            saveHistory(player.getCurrentPosition(), player.getDuration());
        }

        if (mediaController != null) {
            mediaController.clear();
            mediaController.removeAllViews();
            mediaController = null;
        }

        if (player != null) {
            player = null;
        }

        super.finish();
    }

    @Override
    public void onStateChanged(int state) {
        if (AppUtil.ETHERNET_DISCONNECTED == state || AppUtil.WIFI_DISCONNECTED == state) {
            showErrorExitDlg("网络出错,请检查您的网络设置");
        } else {
            hideErrorExitDlg();
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mediaController != null) {
            return mediaController.dispatchKeyEvent(event);
        }
        return true;
    }

    PlayerStatistics playerStatistics;

    /**
     * 配置播放器
     */
    private void configurePlayer() {
        canOffScreen = getIntent().getBooleanExtra("canOffScreen", false);
        if (player != null) {
            return;
        }

        if (assetInfo == null) {
            if (canOffScreen) {
                player = new WasuPlayerView(this, BuildType.payTypeUrl, MediaController.DISPLAY_CANT_OFF_SCREEN);
            }else {
                player = new WasuPlayerView(this, BuildType.payTypeUrl);

            }
        } else {
            switch (assetInfo.getAssetFrom()) {
                case 91:
                    player = new WasuPlayerView(this, BuildType.payTypeUrl, ExtPlayer.Sohu);
                    break;
                default:
                    if (canOffScreen) {
                        player = new WasuPlayerView(this, BuildType.payTypeUrl, MediaController.DISPLAY_CANT_OFF_SCREEN);
                    }else {
                        player = new WasuPlayerView(this, BuildType.payTypeUrl);

                    }
                    break;
            }
        }

        player.addObserver(this);
        player.setFullScreen(true);
        addContentView(player, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        mediaController = player.getMediaController();

        playerStatistics = new PlayerStatistics(assetInfo);
        playerStatistics.setPlayer(player);
        WLog.i(TAG, "playerStatistics=" + playerStatistics);
        player.addObserver(playerStatistics);
        player.addObserver(mediaController);
        player.setFullPlay(isFullPlay);
        if(isFullPlay && !player.isFullScreen()) {
            player.toggleFullScreen();
        }
    }

    private boolean canOffScreen = false;//是否支持暂停息屏功能

    /**
     * 解析传入数据
     *
     * @return
     */
    public void parseIntent() {
        if (player == null) {
            return;
        }
        currentStatus = STATUS_PARSE_INTENT;
        assetInfo = (DemandProgram) getIntent().getSerializableExtra(ASSET_INFO);
        assetUrl = getIntent().getStringExtra(ASSET_URL);
        playType = getIntent().getIntExtra(PLAY_TYPE, 0);
        playIndex = getIntent().getIntExtra(PLAY_INDEX, 0);
        assetList = (IAssetList) getIntent().getSerializableExtra(ASSET_LIST);
        traceid = getIntent().getStringExtra(TRACEID);

        if (PLAY_TYPE_NORMAL == playType && assetInfo == null && (assetUrl == null || (assetUrl != null && "".equals(assetUrl.trim())))) {
            showErrorExitDlg("传入数据为空");
        } else if (PLAY_TYPE_LOOP == playType && assetList == null && (assetUrl == null || (assetUrl != null && "".equals(assetUrl.trim())))) {
            showErrorExitDlg("传入数据为空");
        } else {
            processHandler.sendEmptyMessage(PROCESS_NEXT);
        }
    }

    protected void addDataToStatistics(DemandProgram mDemandInfo2) {
        if (mDemandInfo2 == null) {
            WLog.i(TAG, "addDataToStatistics mDemandInfo2=" + mDemandInfo2);
            return;
        }
        WasuStatistics.getInstance().addPageElem(new PlayInfo(mDemandInfo2.getId(), mDemandInfo2.getTitle(), "", "",
                mDemandInfo2.getPpv(), BuildType.SITE_ID, "", mDemandInfo2.getPriceInfo().getPrice() + "", mDemandInfo2.getAssetTypeText(), mDemandInfo2.getCurPlayIndex() + 1 + "", isAdFree == true ? "1" : "0", "0",
                mDemandInfo2.getAssetFrom() + "", "viewpage", mDemandInfo2.getPriceInfo().getOriginalPrice() + "", checkIsVip()));
    }

    private String checkIsVip() {
        String isvip = AuthSDK.getInstance().getValue("vipState");
        return isvip.isEmpty() ? "0" : isvip;
    }

    /**
     * 根据assetUrl获取assetInfo
     */
    public void fetchAssetInfo() {
        if (player == null) {
            return;
        }

        currentStatus = STATUS_FETCH_ASSET_INFO;

        switch (playType) {
            case PLAY_TYPE_NORMAL: {
                ObjectListener objectListener = new ObjectListener() {

                    @Override
                    public void onObjectGet(int retcode, String extraMsg, ObjectBase data) {

                        if (retcode != WError.ERROR_NONE && errorRetryCount > 0) {
                            DataFetchModule.getInstance().fetchObjectGet(assetUrl, DemandProgram.class, this);
                            errorRetryCount--;
                            return;
                        }

                        errorRetryCount = 3;
                        if (retcode != 0) {
                            showErrorExitDlg("获取资产数据失败");
                            return;
                        } else {
                            assetInfo = (DemandProgram) data;
                            if (assetInfo == null) {
                                showErrorExitDlg("获取资产数据失败");
                                return;
                            }

                            assetInfo.setDetailUrl(assetUrl);
                            addDataToStatistics(assetInfo);
                            processHandler.sendEmptyMessage(PROCESS_NEXT);
                        }

                    }
                };

                DataFetchModule.getInstance().fetchObjectGet(assetUrl, DemandProgram.class, objectListener);
            }
            break;
            case PLAY_TYPE_LOOP: {

                final ObjectListener demandProgramListener = new ObjectListener() {

                    @Override
                    public void onObjectGet(int retcode, String extraMsg, ObjectBase data) {

                        if (retcode != WError.ERROR_NONE) {
                            showErrorExitDlg("获取资产数据失败");
                            return;
                        } else {
                            assetInfo = (DemandProgram) data;
                            if (assetInfo == null) {
                                showErrorExitDlg("获取资产数据失败");
                                return;
                            }

                            assetInfo.setDetailUrl(assetList.getJsonUrl(playIndex));
                            addDataToStatistics(assetInfo);
                            processHandler.sendEmptyMessage(PROCESS_NEXT);
                        }

                    }
                };

                final ObjectListener demandListListener = new ObjectListener() {

                    @Override
                    public void onObjectGet(int retcode, String extraMsg, ObjectBase data) {

                        if (retcode != WError.ERROR_NONE) {
                            showErrorExitDlg("获取连播数据失败");
                            return;
                        } else {
                            DemandList demandList = (DemandList) data;
                            if (demandList == null || demandList.getDatum() == null) {
                                showErrorExitDlg("获取连播数据失败");
                                return;
                            }

                            if (assetList == null) {
                                assetList = demandList;

                                String url = assetList.getJsonUrl(playIndex % PSIZE);
                                DataFetchModule.getInstance().fetchObjectGet(url, DemandProgram.class, demandProgramListener);
                            } else {
                                int index = ((DemandList) assetList).getDatum().getAssets().size();
                                assetList.append(demandList);

                                String url = assetList.getJsonUrl(index);
                                DataFetchModule.getInstance().fetchObjectGet(url, DemandProgram.class, demandProgramListener);
                            }
                        }

                    }
                };

                if (assetList != null) {
                    String url = assetList.getJsonUrl(playIndex);
                    if (TextUtils.isEmpty(url) && assetUrl != null) {
                        String pageUrl = Uri.parse(assetUrl.replaceAll("&page=[\\d]*", "").replaceAll("&psize=[\\d]*", "")).buildUpon().appendQueryParameter("page", String.valueOf(playIndex / PSIZE + 1)).appendQueryParameter("psize", String.valueOf(PSIZE)).build().toString();
                        DataFetchModule.getInstance().fetchObjectGet(pageUrl, DemandList.class, demandListListener);
                    } else {
                        DataFetchModule.getInstance().fetchObjectGet(url, DemandProgram.class, demandProgramListener);
                    }
                } else if (assetUrl != null) {
                    String pageUrl = Uri.parse(assetUrl.replaceAll("&page=[\\d]*", "").replaceAll("&psize=[\\d]*", "")).buildUpon().appendQueryParameter("page", String.valueOf(playIndex / PSIZE + 1)).appendQueryParameter("psize", String.valueOf(PSIZE)).build().toString();
                    DataFetchModule.getInstance().fetchObjectGet(pageUrl, DemandList.class, demandListListener);
                }
            }
            break;
        }
    }

    /**
     * 询价
     */
    private void queryPrice() {
        if (player == null) {
            return;
        }

        currentStatus = STATUS_QUERY_PRICE;

        if (assetInfo == null) {
            processHandler.sendEmptyMessage(PROCESS_NEXT);
            return;
        }

        urlProperty = new UrlProperty();
        urlProperty.setPreferPlayer(IMediaControl.PreferPlayer.DEFAULT);
        urlProperty.setResourceId(assetInfo.getId());
        urlProperty.setResourceName(assetInfo.getTitle());
        urlProperty.setNodeId(assetInfo.getCatId());
        urlProperty.setPpvPath(assetInfo.getPpv());
        urlProperty.setIsFree(assetInfo.getIsFree() == 1);
        urlProperty.setPriceinfo(assetInfo.getPriceInfo());

        final AuthListener queryPriceListener = new AuthListener() {

            @Override
            public void result(int ret, String extra, Object retData) {
                if (player != null) {
                    player.priceChanged(ret, extra, retData);
                }

                if (ret == WError.ERROR_NONE) {
                    PriceInfo price = (PriceInfo) retData;
                    urlProperty.setPrice(price.mPrice);
                    mOriginPrice = price.mOriginalPrice;
                } else {
                    urlProperty.setPrice(0);
                    mOriginPrice = 0;
                }

                processHandler.sendEmptyMessage(PROCESS_NEXT);
            }
        };

        if (TextUtils.isEmpty(urlProperty.getResourceId())) {
            urlProperty.setIsFree(true);
            processHandler.sendEmptyMessage(PROCESS_NEXT);
        } else {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("resourceId", urlProperty.getResourceId());
            params.put("resourceName", urlProperty.getResourceName());
            params.put("orderType", 0);

            AuthSDK.getInstance().queryPrice(params, queryPriceListener);
        }
    }

    boolean isAdFree = false;

    /**
     * 调整默认播放码率
     * 一.普通栏目资产
     * a.会员登录情况下,查询是否VIP会员(调用是否免广告接口，免广告的为VIP会员)：
     * <p/>
     * （1）VIP会员：默认播放高清码率（1.3M），如果没有高清，则默认播放码率取低一级；如果高清及高清码率以下都没有，则取高一级码率（比如，只有超清，4K，则默认取超清播放）
     * （2）普通会员：默认播放清晰码率（0.8M）,如果没有清晰，默认取低一级码率；如果清晰以下都没有，则默认顺序取高一级码率
     * <p/>
     * b.会员未登陆情况下:
     * (1.)单点收费资产（只判断原价price>0）的情况，默认播放高清码率，默认取低一级码率；若高清及以下码率都为空，默认取高一级码率
     * (2)免费资产（原价price=0）,默认播放0.8M码率
     * <p/>
     * 二、特殊栏目资产（如4K和蓝光等高码率资产，播放相对应码流3.8M/6M）
     */
    private void adjustPlayBitrate() {
        if (player == null) {
            return;
        }

        currentStatus = STATUS_ADJUST_PLAY_BITRATE;

        if (assetInfo == null) {
            processHandler.sendEmptyMessage(PROCESS_NEXT);
            return;
        }

        switch (assetInfo.getCatProp()) {
            case DemandProgram.CAT_PROP_BD:
                DEFAULT_PLAY_RATE = PLAY_RATE_BD;
                urlProperty.setBitrate((int) getFitRate());
                processHandler.sendEmptyMessage(PROCESS_NEXT);
                break;
            case DemandProgram.CAT_PROP_4K:
                DEFAULT_PLAY_RATE = PLAY_RATE_4K;
                urlProperty.setBitrate((int) getFitRate());
                processHandler.sendEmptyMessage(PROCESS_NEXT);
                break;
            default: {
                String userKey = AuthSDK.getInstance().getValue(AuthSDK.KEY_USERKEY);
                if (TextUtils.isEmpty(userKey)) {
                    if (mOriginPrice > 0) {
                        DEFAULT_PLAY_RATE = PLAY_RATE_NORMAL_H;
                    } else if (mOriginPrice == 0) {
                        DEFAULT_PLAY_RATE = PLAY_RATE_NORMAL_L;
                    } else {
                        //
                    }

                    urlProperty.setBitrate((int) getFitRate());
                    processHandler.sendEmptyMessage(PROCESS_NEXT);
                } else {
                    AuthSDK.getInstance().verifyUserIsAdFree(new AuthListener() {
                        @Override
                        public void result(int ret, String extra, Object retData) {
                            if (ret == WError.ERROR_NONE) {
                                try {
                                    JSONObject root = new JSONObject(new String((byte[]) retData, "utf-8"));
                                    isAdFree = root.optInt("isFree") == 1;

                                    if (isAdFree) {
                                        DEFAULT_PLAY_RATE = PLAY_RATE_NORMAL_H;
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                            urlProperty.setBitrate((int) getFitRate());
                            processHandler.sendEmptyMessage(PROCESS_NEXT);
                        }
                    });
                }
            }
            break;
        }
    }

    /**
     * 查询播放历史记录
     */
    public void queryHistory() {
        if (player == null) {
            return;
        }

        currentStatus = STATUS_QUERY_HISTORY;

//        getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).edit().putLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, DEFAULT_PLAY_RATE).commit();

        if (assetInfo == null) {
            assetHistory = new DBProgramHistory();
            assetHistory.domain = BuildType.HTTP_DOMAIN;

            processHandler.sendEmptyMessage(PROCESS_NEXT);
            return;
        } else {
            if (assetInfo.getAssetFrom() == 91) {
                player.switchPlayer(ExtPlayer.Sohu);
            } else {
                player.switchPlayer(ExtPlayer.Default);
            }
        }

        try {
            assetHistory = DBManage.queryBy(DBProgramHistory.class, "programId", assetInfo.getId());
            assetHistory.domain = BuildType.HTTP_DOMAIN;
        } catch (Exception e) {
            WLog.e(TAG, "查询历史记录出错");
            e.printStackTrace();

            assetHistory = new DBProgramHistory();
            assetHistory.domain = BuildType.HTTP_DOMAIN;
        }

        processHandler.sendEmptyMessage(PROCESS_NEXT);
    }

    /**
     * 播放鉴权
     */
    public void playAuth() {
        if (player == null) {
            return;
        }

        currentStatus = STATUS_PLAY_AUTH;

        if (assetInfo == null) {
            processHandler.sendEmptyMessage(PROCESS_NEXT);
            return;
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("resourceId", assetInfo.getId());
        params.put("ip", Tools.getLocalIpAddress());
        params.put("offSet", assetHistory.lastPlayTime);
        params.put("price", assetInfo.getPriceInfo().getPrice());
        params.put("resourceName", assetInfo.getTitle());
        if (DemandProgram.SHOWTYPE_MOVIE == assetInfo.getAssetType()) {
            params.put("type", 0);
        } else if (DemandProgram.SHOWTYPE_TV_SERIES == assetInfo.getAssetType()) {
            params.put("type", 1);
        } else {
            params.put("type", 2);
        }
        params.put("categoryId", assetInfo.getCatId());

        AuthSDK.getInstance().playVideoCount(params, new AuthListener() {

            @Override
            public void result(int arg0, String arg1, Object arg2) {
                WLog.e(TAG, "video play count:" + arg0 + "," + arg1 + "," + arg2);
            }

        });

        /**
         * 搜狐资产需要搜狐播放器鉴权
         *
         * 1.判断资产是否免费
         * 2.如果是免费，不需要鉴权，直接播放
         * 3.如果是收费，判断用户是否登录
         * 4.用户未登录则引导用户登录
         * 5.用户登录成功后询价并鉴权,如果免费影片直接播放，收费影片引导订包，订包成功后播放，否则停止播放
         * 6.用户登录失败，停止播放
         */
        if (assetInfo.getAssetFrom() == 91) {
            if (urlProperty.getPrice() == 0 && mOriginPrice != 99999) {
                processHandler.sendEmptyMessage(PROCESS_NEXT);
            } else {
                String userKey = AuthSDK.getInstance().getValue(AuthSDK.KEY_USERKEY);
                if (TextUtils.isEmpty(userKey)) {
                    DialogLogin dialogLogin = new WasuCompFactory().createUniLogin(this, BuildType.loginurl);
                    dialogLogin.setLoginStatusListener(new DialogLogin.LoginStatusListener() {
                        @Override
                        public void onLogStatus(boolean b) {
                            if (b) {
                                Map<String, Object> params = new HashMap<String, Object>();
                                params.put("resourceId", urlProperty.getResourceId());
                                params.put("resourceName", urlProperty.getResourceName());
                                params.put("orderType", 0);

                                AuthSDK.getInstance().queryPrice(params, new AuthListener() {
                                    @Override
                                    public void result(int ret, String extra, Object retData) {
                                        if (ret == WError.ERROR_NONE) {
                                            PriceInfo price = (PriceInfo) retData;
                                            urlProperty.setPrice(price.mPrice);
                                            mOriginPrice = price.mOriginalPrice;
                                        }

                                        if (urlProperty.getPrice() == 0) {
                                            processHandler.sendEmptyMessage(PROCESS_NEXT);
                                        } else {
                                            SohuTvPlayer sohuTvPlayer = (SohuTvPlayer) player.getMediaControl();
                                            sohuTvPlayer.setProviderAppKey(Common.sohuAppKey);
                                            sohuTvPlayer.setUid(AuthSDK.getInstance().getValue(AuthSDK.KEY_USERKEY));
                                            sohuTvPlayer.setMobile("");

                                            DialogPlanBuy dialogPlanBuy = new DialogPlanBuy(ActivityPlayer.this, Common.OtherOrderUrl, assetInfo.getAssetFromLabel(), new DialogPlanBuy.PayStatusListener() {
                                                @Override
                                                public void onPay(int status, int code) {
                                                    if (status == 0) {
                                                        processHandler.sendEmptyMessage(PROCESS_NEXT);
                                                    } else {
                                                        showErrorExitDlg("支付失败");
                                                    }
                                                }
                                            });
                                            dialogPlanBuy.show();
                                        }
                                    }
                                });
                            } else {
                                showErrorExitDlg("登录失败,无法观看本片");
                            }
                        }
                    });
                    dialogLogin.show();
                } else {
                    SohuTvPlayer sohuTvPlayer = (SohuTvPlayer) player.getMediaControl();
                    sohuTvPlayer.setProviderAppKey(Common.sohuAppKey);
                    sohuTvPlayer.setUid(userKey);
                    sohuTvPlayer.setMobile("");

                    if (urlProperty.getPrice() != 0) {
                        DialogPlanBuy dialogPlanBuy = new DialogPlanBuy(this, Common.OtherOrderUrl, assetInfo.getAssetFromLabel(), new DialogPlanBuy.PayStatusListener() {
                            @Override
                            public void onPay(int status, int code) {
                                if (status == 0) {
                                    processHandler.sendEmptyMessage(PROCESS_NEXT);
                                } else {
                                    showErrorExitDlg("支付失败");
                                }
                            }
                        });
                        dialogPlanBuy.show();
                    } else {
                        processHandler.sendEmptyMessage(PROCESS_NEXT);
                    }
                }
            }
        } else {
            processHandler.sendEmptyMessage(PROCESS_NEXT);
        }
    }

    /**
     * 开始播放
     */
    public void play() {
        if (player == null) {
            return;
        }

        currentStatus = STATUS_DONE;

        getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).edit().putLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, DEFAULT_PLAY_RATE).commit();

        String url = getFitPlayUrl();
        if (TextUtils.isEmpty(url)) {
            showErrorExitDlg("没有获取到播放串,可能该片源已下线");
            return;
        }

        if (player != null) {
            player.updateParams(getUpdateParams());
        }

        if (assetInfo == null) {
            showErrorExitDlg("资产获取失败");
            return;
        }

        try {
            /*WasuStatistics.getInstance().playBegin(new PlayInfo(urlProperty.getResourceId(), urlProperty.getResourceName(), url,
                    urlProperty.getBitrate() + "", urlProperty.getPpvPath(),
                    BuildType.SITE_ID, traceid, urlProperty.getPrice() + "", assetInfo.getAssetTypeText(), assetInfo.getCurPlayIndex() + 1 + ""), AppUtil.playEnter);*/
            //player.setVideoPath(url, urlProperty);

            PlayerParams params = new PlayerParams();
            params.setAssetInfo(assetInfo);

            if(isFullPlay && series >= 0) {
                assetInfo.setCurPlayIndex(series);
            }
            if(isFullPlay) {
                player.setPosition((int) currentPosition * 1000);
            }

            // 传入有码率时强制指定码率
            if (getIntent().hasExtra(ASSET_BITRATE)) {
                long bitrate = getIntent().getLongExtra(ASSET_BITRATE, 0);
                if (assetInfo.getPlayinfoList().get(bitrate) != null) {
                    params.setBitrate(bitrate);
                }
            }

            player.play(params);

            if (isFullPlay && !player.isFullScreen()) {
                player.toggleFullScreen();
            }
        } catch (Exception e) {
            e.printStackTrace();
            showErrorExitDlg("播放地址无效,可能该片源已下线");
            return;
        }

        // 免费资产/已付费资产，上次观看时间如果距离结束10s以上，将历史记录传给播放器，收费资产等待询价结果再决定是否seek，询价免费seek，不免费不seek
        if (assetInfo.getIsFree() == 1 && assetHistory.lastSeries == assetInfo.getCurPlayIndex() && assetHistory.duration - assetHistory.lastPlayTime > 10000) {
            player.seekTo((int) assetHistory.lastPlayTime);
            isShowSeekMsg = true;
        }

        processHandler.sendEmptyMessage(PROCESS_NEXT);
    }

    /**
     * 根据默认码率和集数获取加密播放串
     *
     * @return
     */
    public String getFitPlayUrl() {
        if (assetInfo == null || assetInfo.getPlayinfoList().size() == 0) {
            return null;
        }
        int playIndex = assetInfo.getCurPlayIndex();

        long fitRate = getFitRate();
        DemandPlayinfo playInfo = assetInfo.getPlayinfoList().get(fitRate);

        if (playInfo == null) {//向下寻找
            long minRate = getMinRate();
            long closedRate = getClosedFitRate(fitRate, false);
            int loopcnt = 0;
            while (closedRate >= minRate && loopcnt < MAX_LOOP_CNT) {
                playInfo = assetInfo.getPlayinfoList().get(closedRate);
                if (playInfo != null) {
                    fitRate = closedRate;
                    break;
                } else {
                    closedRate = getClosedFitRate(closedRate, false);
                }
                loopcnt++;
            }
        }

        if (playInfo == null) {//向上寻找
            long maxRate = getMaxRate();
            long closedRate = getClosedFitRate(fitRate, true);
            int loopcnt = 0;
            while (closedRate <= maxRate && loopcnt < MAX_LOOP_CNT) {
                playInfo = assetInfo.getPlayinfoList().get(closedRate);
                if (playInfo != null) {
                    fitRate = closedRate;
                    break;
                } else {
                    closedRate = getClosedFitRate(closedRate, true);
                }
                loopcnt++;
            }
        }

        if (playInfo == null) {
            return null;
        }

        DemandSeries seriesInfo = null;
        List<DemandSeries> series = playInfo.getSeriesList();
        for (DemandSeries o : series) {
            if (o.getEpisode() == playIndex) {
                seriesInfo = o;
                break;
            }
        }

        if (seriesInfo == null) {//向下寻找
            long minRate = getMinRate();
            long closedRate = getClosedFitRate(fitRate, false);
            int loopcnt = 0;
            while (closedRate >= minRate && loopcnt < MAX_LOOP_CNT) {
                playInfo = assetInfo.getPlayinfoList().get(closedRate);
                if (playInfo != null) {
                    List<DemandSeries> l = playInfo.getSeriesList();
                    for (DemandSeries o : l) {
                        if (o.getEpisode() == playIndex) {
                            seriesInfo = o;
                            break;
                        }
                    }

                    if (seriesInfo != null) {
                        fitRate = closedRate;
                        break;
                    } else {
                        if (closedRate == minRate) {
                            break;
                        }
                        closedRate = getClosedFitRate(closedRate, false);
                    }
                } else {
                    if (closedRate == minRate) {
                        break;
                    }
                    closedRate = getClosedFitRate(closedRate, false);
                }
                loopcnt++;
            }
        }

        if (seriesInfo == null) {//向上寻找
            long maxRate = getMaxRate();
            long closedRate = getClosedFitRate(fitRate, true);
            int loopcnt = 0;
            while (closedRate <= maxRate && loopcnt < MAX_LOOP_CNT) {
                playInfo = assetInfo.getPlayinfoList().get(closedRate);
                if (playInfo != null) {
                    List<DemandSeries> l = playInfo.getSeriesList();
                    for (DemandSeries o : l) {
                        if (o.getEpisode() == playIndex) {
                            seriesInfo = o;
                            break;
                        }
                    }

                    if (seriesInfo != null) {
                        fitRate = closedRate;
                        break;
                    } else {
                        if (closedRate == maxRate) {
                            break;
                        }
                        closedRate = getClosedFitRate(closedRate, true);
                    }
                } else {
                    if (closedRate == maxRate) {
                        break;
                    }
                    closedRate = getClosedFitRate(closedRate, true);
                }
                loopcnt++;
            }
        }

        if (seriesInfo == null && series.size() != 0) {
            seriesInfo = series.get(0);
            if (seriesInfo != null) {
                assetInfo.setCurPlayIndex(seriesInfo.getEpisode() - 1);
            }
        }

        player.setAssetSeries(seriesInfo);

        if (seriesInfo == null) {
            return null;
        }

        getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
                .edit()
                .putLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, playInfo.getRate())
                .commit();

        return seriesInfo.getEncryUrl();
    }

    /**
     * 根据默认码率/用户选择的码率，选择当前最接近的码率
     *
     * @return
     */
    public long getFitRate() {
        if (assetInfo == null) {
            return DEFAULT_PLAY_RATE;
        }

        if (getIntent().hasExtra(ASSET_BITRATE)) {
            long bitrate = getIntent().getLongExtra(ASSET_BITRATE, 0);
            if (assetInfo.getPlayinfoList().get(bitrate) != null) {
                return bitrate;
            }
        }

        long defaultRate = this.getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, DEFAULT_PLAY_RATE);
        if (assetInfo.getPlayinfoList().get(defaultRate) != null) {
            return defaultRate;
        }

        long fitRate = defaultRate;
        long absRate = Long.MAX_VALUE;
        Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
        for (Entry<Long, DemandPlayinfo> entry : entrySet) {
            long tmpRate = entry.getKey();

            if (Math.abs(defaultRate - tmpRate) <= absRate) {
                fitRate = tmpRate;
                absRate = Math.abs(defaultRate - tmpRate);
            }
        }
        return fitRate;
    }

    /**
     * 寻找比当前码率最接近的码率，若没有找到，返回原来的码率
     *
     * @param fitRate
     * @param direction true，向上寻找，false，向下寻找
     * @return
     */
    private long getClosedFitRate(long fitRate, boolean direction) {
        long closedFitRate = fitRate;
        long absRate = Long.MAX_VALUE;
        Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
        for (Entry<Long, DemandPlayinfo> entry : entrySet) {
            long tmpRate = entry.getKey();

            if (direction) {
                if (tmpRate <= fitRate) {
                    continue;
                }
            } else {
                if (tmpRate >= fitRate) {
                    continue;
                }
            }

            if (Math.abs(fitRate - tmpRate) <= absRate) {
                closedFitRate = tmpRate;
                absRate = Math.abs(fitRate - tmpRate);
            }
        }

        return closedFitRate;
    }

    /**
     * 寻找最大码率
     *
     * @return
     */
    private long getMaxRate() {
        long ret = -1L;

        if (assetInfo == null || assetInfo.getPlayinfoList() == null) {
            return ret;
        }

        Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
        for (Entry<Long, DemandPlayinfo> entry : entrySet) {
            long tmpRate = entry.getKey();

            if (ret == -1L) {
                ret = tmpRate;
                continue;
            }

            if (ret > tmpRate) {
                continue;
            }

            ret = tmpRate;
        }

        return ret;
    }

    /**
     * 寻找最小码率
     *
     * @return
     */
    private long getMinRate() {
        long ret = -1L;

        if (assetInfo == null || assetInfo.getPlayinfoList() == null || assetInfo.getPlayinfoList().entrySet() == null) {
            return ret;
        }

        Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
        for (Entry<Long, DemandPlayinfo> entry : entrySet) {
            long tmpRate = entry.getKey();

            if (ret == -1L) {
                ret = tmpRate;
                continue;
            }

            if (ret < tmpRate) {
                continue;
            }

            ret = tmpRate;
        }

        return ret;
    }

    /**
     * 保存播放历史记录
     */
    public void saveHistory(int currDuration, int duration) {
        if (mediaController == null || currDuration == 0 || duration == 0 || duration == -1) {
            return;
        }

        if (assetInfo == null) {
            return;
        }

        try {
            assetHistory.programId = Integer.parseInt(assetInfo.getId());
            assetHistory.programPic = assetInfo.getPicUrl();
            assetHistory.programName = assetInfo.getTitle();
            assetHistory.domain = BuildType.HTTP_DOMAIN;
            assetHistory.showType = assetInfo.getAssetType();
            assetHistory.lastPlayTime = currDuration;
            assetHistory.duration = duration;
            assetHistory.lastSeries = assetInfo.getCurPlayIndex();
            assetHistory.totalSeries = assetInfo.getTotal();
            assetHistory.updateSeries = assetInfo.getFirstPlayinfo().getSeriesList().size();
            assetHistory.preUpdateSeries = assetHistory.updateSeries;
            if (TextUtils.isEmpty(assetInfo.getDetailUrl())) {//优先保存单个资产的url
                assetHistory.detailUrl = assetUrl;
            } else {
                assetHistory.detailUrl = assetInfo.getDetailUrl();
            }
            assetHistory.playUrl = getFitPlayUrl();
            assetHistory.savehistorytime = System.currentTimeMillis();

            assetHistory.delete();
        } catch (Exception e) {
            WLog.e(TAG, "保存历史记录失败");
            e.printStackTrace();
        }
    }

    /**
     * 退出播放
     *
     * @param msg
     */
    public void exit(String msg) {
        postMessage(msg);

        saveHistory(player.getCurrentPosition(), player.getDuration());

        if (mediaController != null) {
            mediaController.clear();
            mediaController.removeAllViews();
            mediaController = null;
        }
//		if(playerStatistics!=null){
//			player.removeObserver(playerStatistics);
//			playerStatistics=null;
//		}
        finish();
    }

    @Override
    public void onAdStatusChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        if (player == null) {
            return;
        }
        WasuStatistics.getInstance().playEnd(player.getCurrentPosition(), player.getDuration());
        // 调起播放结束的统计
        WLog.e("BigDataStatistics", "调起播放结束的统计actp");
        WasuStatistics.getInstance().use(assetInfo.getId(), player.getCurrentPosition() + "");

        if (playType == PLAY_TYPE_NORMAL) {
            if (assetInfo.getAssetType() == DemandProgram.SHOWTYPE_MOVIE ||
                    assetInfo.getAssetType() == DemandProgram.SHOWTYPE_UNKNOWN ||
                    assetInfo.getAssetType() == DemandProgram.SHOWTYPE_NEITHER_MOVIE_NOR_TV) {

                player.StopTimer();
                exit("播放完毕");
                return;
            }

            int playIndex = assetInfo.getCurPlayIndex();
            if (playIndex == assetInfo.getPlayInfo(getFitRate()).getSeriesList().size()) {
                player.StopTimer();
                exit("播放完毕");
                return;
            }

            playIndex++;
            Intent intent = new Intent(ActivityPlayer.ACTION);
            assetInfo.setCurPlayIndex(playIndex);
            intent.putExtra(ActivityPlayer.ASSET_INFO, assetInfo);
            startActivity(intent);
        } else if (playType == PLAY_TYPE_LOOP) {
            if (playIndex == assetList.getSize() - 1) {
                player.StopTimer();
                exit("播放完毕");
                return;
            }

            saveHistory(player.getCurrentPosition(), player.getDuration());

            playIndex++;
            Intent intent = new Intent(ActivityPlayer.ACTION);
            intent.putExtra(PLAY_TYPE, PLAY_TYPE_LOOP);
            intent.putExtra(PLAY_INDEX, playIndex);
            intent.putExtra(ASSET_LIST, (Serializable) assetList);
            intent.putExtra(ASSET_URL, assetUrl);
            startActivity(intent);
        } else {

        }
    }

    @Override
    public void onError(MediaPlayer arg0, int arg1, int arg2) {
        showErrorExitDlg("播放器错误:" + arg1 + "," + arg2);
    }

    @Override
    public void onInfo(MediaPlayer arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPause(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPrepareComplete(MediaPlayer arg0) {
        if (assetHistory == null || assetInfo == null || mediaController == null) {
            return;
        }

        // 提示用户历史记录时间点
        if (isShowSeekMsg && assetHistory.lastSeries == assetInfo.getCurPlayIndex() && assetHistory.duration - assetHistory.lastPlayTime > 10000) {
            int totalSeconds = (int) ((int) assetHistory.lastPlayTime / 1000.f);
            int seconds = (int) (totalSeconds % 60.f);
            int minutes = (int) ((totalSeconds / 60.f) % 60.f);
            int hours = (int) (totalSeconds / 3600.f);
            StringBuilder sb = new StringBuilder();
            Formatter ft = new Formatter(sb, Locale.getDefault());

            String timeStr = "";
            sb.setLength(0);
            if (hours > 0) {
                timeStr = ft.format("%d:%02d:%02d", hours, minutes, seconds).toString();
            } else {
                timeStr = ft.format("%02d:%02d", minutes, seconds).toString();
            }
            postMessage("已从上次退出的时间点" + timeStr + "开始播放");
            isShowSeekMsg = false;

            ft.close();
        }

        mediaController.setReady(true);
    }

    @Override
    public void onPreparing(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProgress(int arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onResume(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSeekComplete(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSeeking(MediaPlayer arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStart(MediaPlayer arg0) {
        if (mediaController != null) {
            mediaController.handleFullScreen(true);
        }
        if(urlProperty == null ||  assetInfo == null || player == null){
            WLog.e(TAG, "传入的播放数据有误或播放器为空！");
            return;
        }
        WasuStatistics.getInstance().playBegin(new PlayInfo(urlProperty.getResourceId(), urlProperty.getResourceName(), player.getRealUrl().isEmpty() ? "" : player.getRealUrl(),
                urlProperty.getBitrate() + "", urlProperty.getPpvPath(),
                BuildType.SITE_ID, traceid, urlProperty.getPrice() + "",
                assetInfo.getAssetTypeText(), assetInfo.getCurPlayIndex() + 1 + "",
                "1", "0", assetInfo.getContentChannel(), "begin", urlProperty.getPriceInfo().getOriginalPrice() + "", checkIsVip()), AppUtil.playEnter);
        player.StartTimer();
    }

    @Override
    public void onStatusChanged(MediaPlayer arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStop(MediaPlayer arg0) {
        if (player == null) {
            return;
        }

        saveHistory(player.getCurrentPosition(), player.getDuration());
        WasuStatistics.getInstance().playEnd(player.getCurrentPosition(), player.getDuration());
        player.StopTimer();
        // 调起播放结束的统计
        WLog.e("BigDataStatistics", "调起播放结束的统计atp2");
        if (assetInfo != null)
            WasuStatistics.getInstance().use(assetInfo.getId(), player.getCurrentPosition() + "");
    }

    /**
     * @param arg0 WASU_ERROR_AUTH			 	= 设备授权注册失败
     *             WASU_ERROR_QUERY_PRICE 		= 资产询价失败
     *             WASU_ERROR_PAY				= 资产支付失败
     *             WASU_ERROR_PLAYER			= 播放器异常
     *             WASU_ERROR_UPM_TOKEN_EXPIRE	= token严重超期
     * @param arg1
     */
    @Override
    public void onWasuError(int arg0, String arg1) {
        if (NetUtils.isNetConnected(this)) {
            switch (arg0) {
                case IMediaListener.WASU_ERROR_AUTH:
                    showErrorExitDlg("设备授权注册失败");
                    break;
                case IMediaListener.WASU_ERROR_QUERY_PRICE:
                    showErrorExitDlg("资产询价失败");
                    break;
                case IMediaListener.WASU_ERROR_PAY:
                    exit("资产支付失败");
                    break;
                case IMediaListener.WASU_ERROR_PLAYER:
                    showErrorExitDlg("播放器异常:" + arg1);
                    break;
                case IMediaListener.WASU_ERROR_UPM_TOKEN_EXPIRE:
                    showErrorExitDlg("token严重超期");
                    break;
                default:
                    showErrorExitDlg(arg1);
                    break;
            }
        } else {
            showErrorExitDlg("网络出错,请检查您的网络设置");
        }
    }

    @Override
    public void onWasuPlayLimit(int arg0, String arg1) {
        if (arg0 == IMediaListener.WASU_PLAY_IN_PAY) {
            saveHistory(player.getCurrentPosition(), player.getDuration());
        } else if (arg0 == IMediaListener.WASU_PLAY_IN_PREVIEW) {

        } else if (arg0 == IMediaListener.WASU_PLAY_FREE) { //收费影片，已付费继续历史播放
            if (assetInfo.getIsFree() == 0 && assetHistory.lastSeries == assetInfo.getCurPlayIndex() && assetHistory.duration - assetHistory.lastPlayTime > 10000) {
                player.seekTo((int) assetHistory.lastPlayTime);
                isShowSeekMsg = true;
            }
        }
    }

    private Map<String, String> getUpdateParams() {
        Map<String, String> params = new HashMap<String, String>();
        if (assetInfo != null) {
            params.put("prol", Common.APP_NAME + "_" + VersionUtils.getVersionName(this));
            params.put("ccid", assetInfo.getCatId());
            params.put("cid", assetInfo.getId());
            params.put("pay", assetInfo.getFee());
            params.put("strrate", getPlayTag());
            String duration = "";
            try {
                if (player.getAssetSeries() != null)
                    duration = Integer.parseInt(player.getAssetSeries().getDuration()) / 1000 + "";
            } catch (Exception e) {
                e.printStackTrace();
            }
            params.put("vd", duration);
            params.put("ccName", assetInfo.getCatName());
            params.put("cName", assetInfo.getTitle());

            params.put("userid", AuthSDK.getInstance().getValue(AuthSDK.KEY_TVID));
            params.put("columnid", assetInfo.getCatId());
            params.put("programid", assetInfo.getId());
            params.put("featureid", assetInfo.getIsFree() == 1 ? "true" : "false");
        }
        return params;
    }

    private String getPlayTag() {
        if (assetInfo == null) {
            return null;
        }

        Set<Entry<Long, DemandPlayinfo>> entrySet = assetInfo.getPlayinfoList().entrySet();
        long fitRate = getSharedPreferences(MediaController.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE).getLong(MediaController.SHARED_PREFERENCES_KEY_CURRENT_RATE, DEFAULT_PLAY_RATE);
        for (Entry<Long, DemandPlayinfo> entry : entrySet) {
            long rate = entry.getKey();
            String tag = entry.getValue().getTag();

            if (rate == fitRate) {
                return tag;
            }
        }

        return null;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.StartTimer();

            if (isFullPlay && !player.isFullScreen()) {
                player.toggleFullScreen();
            }
        }
    }

    /**
     * 边看边买生命周期
     */

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.StopTimer();
        }
    }


}
