package com.ym.zxhcsdk.ads;

import android.app.Activity;
import android.graphics.Bitmap;
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.HttpClientUtils;
import com.ym.zxhcsdk.R;
import com.ym.zxhcsdk.listener.RewardVideoAdUpdateViewListener;

public class RewardvideoPortraitADActivity extends Activity implements SurfaceHolder.Callback, RewardVideoAdUpdateViewListener {

    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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rewardvideo_portrait_a_d);
        bindViews();
        RewardVideoMediaPlayer.getInstance().setRewardVideoUpdateListener(this);//设置播放回调监听，更新Activity View
    }

    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);


//        设置底部信息
        bottomTitle.setText(RewardVideoMediaPlayer.getInstance().getHcAdBean().getVideo_title());
        bottomDesc.setText(RewardVideoMediaPlayer.getInstance().getHcAdBean().getVideo_desc());
        bottomBtnTv.setText(RewardVideoMediaPlayer.getInstance().getHcAdBean().getVideo_iconbuttontext());

        HttpClientUtils.LoadImg(RewardVideoMediaPlayer.getInstance().getHcAdBean().getVideo_icon_url(), new HttpClientUtils.OnRequestImgCallBack() {
            @Override
            public void onSuccess(Bitmap bitmap) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bottomIcon.setImageBitmap(bitmap);
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {

            }
        });






        findViewById(R.id.ll_bottom_download).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RewardVideoMediaPlayer.getInstance().clickVideoAd();
            }
        });
        //初始化SurfaceHolder类，SurfaceView的控制器
        surfaceHolder = sfv_reward_show.getHolder();
        surfaceHolder.addCallback(this);
        SetAdVideo();

        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");
        RewardVideoMediaPlayer.getInstance().getMediaPlayer().setDisplay(surfaceHolder);
        RewardVideoMediaPlayer.getInstance().startVideo();
    }

    /**
     * 播放激励视频完成回调
     */
    @Override
    public void OnCompletionListener() {
        TopClose.setVisibility(View.VISIBLE);
        TopTimeTv.setVisibility(View.GONE);
    }

    /**
     * 播放激励视频错误回调
     * @param error
     */
    @Override
    public void OnErrorListener(String error) {
        TopClose.setVisibility(View.VISIBLE);
        TopTimeTv.setVisibility(View.GONE);
    }

    @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
    protected void onDestroy() {
        super.onDestroy();
        RewardVideoMediaPlayer.getInstance().closeVideoAdPage();
    }

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