package com.naz.sdkdemo.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerView;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.base.BaseActivity;
import com.naz.sdkdemo.base.CallbackNew;
import com.naz.sdkdemo.bean.AdBean;
import com.naz.sdkdemo.http.ApiHelper;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * AD
 */
public class AdActivity extends BaseActivity {

    @BindView(R.id.video)
    PlayerView videoView;
    @BindView(R.id.picture)
    ImageView pictureView;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    private SimpleExoPlayer mPlayer;

    private int imgIndex = 0;
    private int imgSum = 0;

    @Override
    public int getLayoutResId() {
        return R.layout.ad_activity;
    }

    @Override
    public void initView() {
        mPlayer = new SimpleExoPlayer.Builder(this).build();
        mPlayer.setRepeatMode(Player.REPEAT_MODE_ALL);
        mPlayer.setPlayWhenReady(true);
        videoView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
        videoView.setKeepScreenOn(true);
        videoView.setPlayer(mPlayer);
        topLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPlayer.isPlaying()){
                    mPlayer.release();
                }
                if (timer!=null){
                    timer.cancel();
                    timerTask.cancel();
                }
                finish();
            }
        });
        ApiHelper.getVideoUrl(this, new CallbackNew<AdBean>() {
            @Override
            public void onResult(AdBean adBean) {
                if (adBean!=null){
                    if (adBean.video.size()==1){
                        MediaItem mediaItem = MediaItem.fromUri(adBean.video.get(0));
                        mPlayer.addMediaItem(mediaItem);
                        mPlayer.prepare();
                    }else if (adBean.video.size() >1){
                        MediaItem mediaItem = MediaItem.fromUri(adBean.video.get(0));
                        MediaItem mediaItem2 = MediaItem.fromUri(adBean.video.get(1));
                        mPlayer.addMediaItem(mediaItem);
                        mPlayer.addMediaItem(mediaItem2);
                        mPlayer.prepare();
                    }
                    if (adBean.img.size()>0){
                        imgIndex = 0;
                        imgSum = adBean.img.size();
                        startImage(adBean.limit_time, adBean.img);
                    }
                }
            }
        });
    }

    private Timer timer;
    private TimerTask timerTask;
    private void startImage(int time, List<String> imgs) {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                Log.e("--------->","--------->run");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(AdActivity.this).load(imgs.get(imgIndex)).into(pictureView);
                        imgIndex = (++imgIndex)%imgSum;
                    }
                });
            }
        };
        timer.schedule(timerTask, 0, (long) time*60 *1000);
    }

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