package com.tencent.liteav.demo.superplayer.ui.player;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.cast.dlna.dmc.control.ICastInterface;
import com.tencent.liteav.demo.superplayer.R;
import com.tencent.liteav.demo.superplayer.SuperPlayerDef;
import com.tencent.liteav.demo.superplayer.ui.view.PointProgressBar;

public class CastingPlayer extends AbsPlayer implements View.OnClickListener,
        PointProgressBar.OnProgressBarChangeListener {

    private LinearLayout mLayoutBottom;                          // 底部进度条所在布局
    private ImageView mIvPause;                               // 暂停播放按钮
    private ImageView mIvPlayNext;                            // 播放下一个按钮
    private ImageView mIvFullScreen;                          // 全屏按钮

    private TextView mTvCurrent;                             // 当前进度文本
    private TextView mTvDuration;                            // 总时长文本
    private PointProgressBar mBarProgress;                       // 播放进度条

    private long mLastClickTime;                         // 上次点击事件的时间
    private TextView mStopCast;                            // 停止投屏

    private boolean mIsChangingSeekBarProgress;             // 进度条是否正在拖动，避免SeekBar由于视频播放的update而跳动

    private SuperPlayerDef.PlayerState mCurrentPlayState = SuperPlayerDef.PlayerState.END;                 // 当前播放状态
    private long mDuration;                              // 视频总时长
    private long mProgress;                              // 当前播放进度

    public CastingPlayer(Context context) {
        super(context);
        init(context);
    }

    public CastingPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CastingPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.superplayer_vod_player_casting, this);
        mLayoutBottom = (LinearLayout) findViewById(R.id.superplayer_ll_bottom);
        mLayoutBottom.setOnClickListener(this);
        mIvPause = (ImageView) findViewById(R.id.superplayer_iv_pause);
        mTvCurrent = (TextView) findViewById(R.id.superplayer_tv_current);
        mTvDuration = (TextView) findViewById(R.id.superplayer_tv_duration);

        mStopCast = (TextView) findViewById(R.id.stop_cast);
        mStopCast.setOnClickListener(this);

        mBarProgress = (PointProgressBar) findViewById(R.id.superplayer_bar_progress);
        mBarProgress.setProgress(0);
        mBarProgress.setMax(100);
        mBarProgress.setListener(this);

        mIvFullScreen = (ImageView) findViewById(R.id.superplayer_iv_fullscreen);
        mIvPlayNext = (ImageView) findViewById(R.id.superplayer_iv_play_next);

//        mTimeoutView = (TimeOutPlayer) findViewById(R.id.superplayer_timeout_view);

        mIvPause.setOnClickListener(this);
        mIvPlayNext.setOnClickListener(this);
        mIvFullScreen.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (System.currentTimeMillis() - mLastClickTime < 300) { //限制点击频率
            return;
        }
        mLastClickTime = System.currentTimeMillis();
        int id = view.getId();

        if (id == R.id.superplayer_iv_pause) { //暂停\播放按钮
            togglePlayState();
        } else if (id == R.id.superplayer_iv_play_next) {
            mControllerCallback.playNext();
        }else if(id == R.id.stop_cast){
            mControllerCallback.stopCast();
        }
    }

    @Override
    public void onProgressChanged(PointProgressBar progressBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(PointProgressBar progressBar) {

    }

    @Override
    public void onStopTrackingTouch(PointProgressBar progressBar) {
        int curProgress = progressBar.getProgress();
        int maxProgress = progressBar.getMax();

        if (curProgress >= 0 && curProgress <= maxProgress) {
            float percentage = ((float) curProgress) / maxProgress;
            int position = (int) (mDuration * percentage);

            if (mControllerCallback != null) {
                mControllerCallback.onSeekTo(position);
            }
        }
    }

    @Override
    public void updatePlayState(SuperPlayerDef.PlayerState playState) {
        switch (playState) {
            case INIT:
            case PAUSE:
            case END:
                mIvPause.setImageResource(R.mipmap.play_state);
                break;
            case PLAYING:
            case LOADING:
                mBarProgress.setEnabled(true);
                mIvPause.setImageResource(R.mipmap.pause_state);
                break;
        }
        mCurrentPlayState = playState;
    }

    @Override
    public void updateVideoProgress(long current, long duration) {
        mProgress = current < 0 ? 0 : current;
        mDuration = duration < 0 ? 0 : duration;
        mTvCurrent.setText(formattedTime(mProgress));

        float percentage = mDuration > 0 ? ((float) mProgress / (float) mDuration) : 1.0f;
        if (mProgress == 0) {
            percentage = 0;
        }

        if (percentage >= 0 && percentage <= 1) {
            int progress = Math.round(percentage * mBarProgress.getMax());
            if (!mIsChangingSeekBarProgress) {
                mBarProgress.setProgress(progress);
            }
            mTvDuration.setText(formattedTime(mDuration));
        }
    }

    private void togglePlayState() {
        switch (mCurrentPlayState) {
            case INIT:
            case PAUSE:
            case END:
                if (mControllerCallback != null) {
                    mControllerCallback.onResume();
                }
                break;
            case PLAYING:
            case LOADING:
                if (mControllerCallback != null) {
                    mControllerCallback.onPause();
                }
                break;
        }
//        show();
    }

}
