package com.mints.goodmoney.ui.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.mints.goodmoney.R;


public class CountDownVedioView extends LinearLayout {

    private CountDownVedioListener countDownVedioListener;

    private CountDownView cdCountVedioView;
    private ImageView ivCountVedioRedbox;

    private boolean isPlaying = false;

    public CountDownVedioView(Context context) {
        this(context, null);
    }

    public CountDownVedioView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountDownVedioView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);
        initListener();
    }

    private void initView(Context context) {
        View view = View.inflate(context, R.layout.view_countdown_vedio, this);

        ivCountVedioRedbox = view.findViewById(R.id.iv_count_redbox);
        cdCountVedioView = view.findViewById(R.id.cd_count_vedio);
    }

    private void initListener() {
        cdCountVedioView.setOnLoadingFinishListener(new CountDownView.OnLoadingFinishListener() {
            @Override
            public void finish() {
                if (countDownVedioListener != null) {
                    countDownVedioListener.finish();
                }
            }
        });
    }

    public void start() {
        cdCountVedioView.start();
        isPlaying = true;
    }

    public void pause() {
        cdCountVedioView.pause();
    }

    public void resume() {
        cdCountVedioView.resume();
    }

    public void reset() {
        cdCountVedioView.reset();
        isPlaying = true;
    }

    public void stop() {
        stopStatus();
        cdCountVedioView.reset();
        cdCountVedioView.pause();
    }

    public void stopStatus() {
        isPlaying = false;
    }

    public boolean isPlaying() {
        return isPlaying;
    }

    public void setTime(int time) {
        cdCountVedioView.setTime(time);
    }

    public void setCountDownVedioListener(CountDownVedioListener countDownVedioListener) {
        this.countDownVedioListener = countDownVedioListener;
    }

    public interface CountDownVedioListener {
        void finish();
    }

    /**
     * 显示红包动画
     */
    public void showRedbox() {
        if (ivCountVedioRedbox != null) {
//            ivCountVedioRedbox.setVisibility(VISIBLE);
            YoYo.with(Techniques.Tada).duration(2000)
                    .repeat(2)
                    .playOn(ivCountVedioRedbox);
        }
    }
}
