package com.wasu.cs.widget;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.FrameLayout;

import com.wasu.comp.videoview.IMediaListener;
import com.wasu.cs.ui.ActivityBase;
import com.wasu.cs.widget.mediacontrol.MediaController;
import com.wasu.cs.widget.videoview.WasuLivePlayerView;
import com.wasu.module.log.WLog;

import cn.com.wasu.main.IntentConstant;

/**
 * @Description: 首页推荐屏的视频小窗口,对直播播放器再封装！
 *               跟{@link MainPageLiveVideoView}的区别是当前这个是在Activity中,
 *               {@link MainPageLiveVideoView}是在ViewGroup中
 * @Author: Danxingxi
 * @CreateDate: 2016/12/5 17:15
 */
public class ActivityLiveVideoView extends ActivityBase implements IMediaListener {
    private String TAG = "ActivityLiveVideoView";
    private WasuLivePlayerView wasuLivePlayerView;
    private String jsonUrl;
    private FrameLayout liveVideoViewContent;

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

        Intent intent = getIntent();
        jsonUrl = intent.getStringExtra(IntentConstant.DATAURI.value());
        if(TextUtils.isEmpty(jsonUrl)){
            postMessage("直播数据获取失败，请稍后再试!");
        }
//        liveVideoViewContent = (FrameLayout) findViewById(R.id.liveVideoViewContent);
        wasuLivePlayerView = new WasuLivePlayerView(ActivityLiveVideoView.this);
        wasuLivePlayerView.setExcludeOption(MediaController.DISPLAY_PROMPT_EXIT_AD);
        wasuLivePlayerView.addObserver(this);
        wasuLivePlayerView.handleFullScreen(true);

        setContentView(wasuLivePlayerView);

//        wasuLivePlayerView.setAnchorView(liveVideoViewContent, this);
//        if(!wasuLivePlayerView.isFullScreen()){
//            wasuLivePlayerView.toggleFullScreen();
//        }
        initData(jsonUrl);
    }

    /**
     * 设置播放器数据
     * @param liveUrl
     */
    private void initData(String liveUrl) {
        jsonUrl = liveUrl;
        if(jsonUrl!=null && !TextUtils.isEmpty(jsonUrl) && wasuLivePlayerView!=null){
            wasuLivePlayerView.play(jsonUrl, 0);
            wasuLivePlayerView.start();
        }
    }

    private long lastTime;
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(!wasuLivePlayerView.dispatchKeyEvent(event)){
            if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
                long currentTime = System.currentTimeMillis();
                if(currentTime - lastTime > 2500){
                    postMessage("再按一次退回到首页哦！");
                    lastTime = currentTime;
                    return true;
                }else {
                    finish();
                }

            }
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(wasuLivePlayerView!=null){
            wasuLivePlayerView.stopPlayback();
            wasuLivePlayerView.removeObserver(ActivityLiveVideoView.this);
            wasuLivePlayerView.removeAllViews();
            wasuLivePlayerView = null;
        }
    }

    @Override
    public void onStart(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onPreparing(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onPrepareComplete(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onSeeking(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onSeekComplete(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onProgress(int i, int i1, int i2) {

    }

    @Override
    public void onPause(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onResume(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onStop(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onError(MediaPlayer mediaPlayer, int i, int i1) {

    }

    @Override
    public void onInfo(MediaPlayer mediaPlayer, int i, int i1) {

    }

    @Override
    public void onStatusChanged(MediaPlayer mediaPlayer, int i) {

    }

    @Override
    public void onAdStatusChanged(int i, int i1) {

    }

    @Override
    public void onWasuError(int i, String s) {

    }

    @Override
    public void onWasuPlayLimit(int i, String s) {

    }
}
