package com.ym.zxhcsdk.ads;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.ym.zxhcsdk.R;
import com.ym.zxhcsdk.view.ZXHCRewardVideoAD;

public class RewardvideoPortraitADActivity extends Activity implements SurfaceHolder.Callback {

    private MediaPlayer mPlayer = null;
    private SurfaceView sfv_reward_show;
    private SurfaceHolder surfaceHolder;
    private FrameLayout frameView;

    private TextView bottomTitle;
    private TextView bottomDesc;
    private ImageView bottomIcon;
    private TextView bottomBtnTv;
    private TextView TopTimeTv;
    private ImageView TopClose;

    public ZXHCRewardVideoAD mZxhcRewardVideoAD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rewardvideo_portrait_a_d);

        mZxhcRewardVideoAD = RewardVideoPlayerUtils.getInstance().getVideo();
        if (null != mZxhcRewardVideoAD) {
            mPlayer = RewardVideoMediaPlayer.getInstance();
        }

        bindViews();
    }



    private void bindViews() {
        sfv_reward_show = findViewById(R.id.sfv_reward_video_ad_show);
        frameView = findViewById(R.id.frame_content);
        bottomTitle = findViewById(R.id.reward_video_ad_title);
        bottomDesc = findViewById(R.id.reward_video_ad_desc);
        bottomIcon = findViewById(R.id.reward_ad_app_icon);
        bottomBtnTv = findViewById(R.id.reward_video_ad_btn);
        TopTimeTv = findViewById(R.id.reward_ad_time);
        TopClose = findViewById(R.id.reward_ad_close);
        //初始化SurfaceHolder类，SurfaceView的控制器
        surfaceHolder = sfv_reward_show.getHolder();
        surfaceHolder.addCallback(this);
        SetAdVideo();

        //            mPlayer = new MediaPlayer();
//            mPlayer.setDataSource("https://zx-cdn.zhangxinhulian.com/file/2020/5/8447fa5488ee19a54d2adbe8d127693045540211.mp4");
          //  mPlayer.setDisplay(surfaceHolder);
//            mPlayer.prepareAsync();

        TopClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void SetAdVideo(){
            TopClose.setVisibility(View.GONE);
            TopTimeTv.setVisibility(View.VISIBLE);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        Log.d("wxw", "surfaceCreated");
        if (mPlayer!=null){
            mPlayer.setDisplay(surfaceHolder);
//            return;
        }


            //网络流媒体播放结束监听
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d("wxw", "MediaPlayer onCompletion");
                    TopClose.setVisibility(View.VISIBLE);
                    TopTimeTv.setVisibility(View.GONE);
                }
            });
            //设置错误信息监听
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.d("wxw", "MediaPlayer error " + what + " extra " + extra);
                    TopClose.setVisibility(View.VISIBLE);
                    TopTimeTv.setVisibility(View.GONE);
                    return false;
                }
            });
            //视频尺寸监听
            mPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
//                    int phoneWidth = PhoneUtils.getInstance().getScreenWidth(RewardvideoPortraitADActivity.this);
//                    float pro = height / (float) width;
//                    ViewGroup.LayoutParams layoutParams = frameView.getLayoutParams();
//                    layoutParams.width = phoneWidth;
//                    layoutParams.height = (int) (phoneWidth * pro);
//                    frameView.setLayoutParams(layoutParams);
                    Log.d("wxw", "mp.getCurrentPosition() " + mp.getCurrentPosition());
                }
            });

            //mPlayer.start();

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        Log.d("wxw", "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        Log.d("wxw", "surfaceDestroyed");
    }

    private int delayTime;
    CountDownTimer countDownTimer = null;

    private void countDown(int time, TextView textView) {
        long times = time * 1000;
        //时间  倒计时   入参long类型
        countDownTimer = new CountDownTimer(times, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                delayTime = (int) millisUntilFinished / 1000;
                textView.setText(delayTime + "");
                if (delayTime == 0) {
                    textView.setText("跳过");
                }
            }

            @Override
            public void onFinish() {
                countDownTimer.cancel();
                countDownTimer = null;
            }
        };
        countDownTimer.start();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPlayer!=null){
            if (!mPlayer.isPlaying()){
                mPlayer.start();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mPlayer!=null){
            if (mPlayer.isPlaying()){
                mPlayer.pause();
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (mPlayer!=null){
            mPlayer.release();
            mPlayer=null;
        }
        super.onDestroy();
    }
}