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

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.LinearLayoutCompat;

import com.tencent.liteav.demo.superplayer.R;
import com.tencent.liteav.demo.superplayer.SuperPlayerDef;

public class TimeOutPlayer extends AbsPlayer implements View.OnClickListener {
    private ImageView backHome;
    private LinearLayoutCompat resumePlay;
    private TextView returnHome;

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

    public TimeOutPlayer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeOutPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.superplayer_vod_player_timeout, this);

        backHome = findViewById(R.id.timeout_back);
        backHome.setOnClickListener(this);

        resumePlay = findViewById(R.id.timeout_resume);
        resumePlay.setOnClickListener(this);

        returnHome = findViewById(R.id.timeout_return);
        returnHome.setOnClickListener(this);

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimeOutPlayer);
            boolean show = a.getBoolean(R.styleable.TimeOutPlayer_showControl, true);

            if (!show) {
                backHome.setVisibility(GONE);
                resumePlay.setVisibility(GONE);
                returnHome.setVisibility(GONE);
            }

            a.recycle();
        }
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();

        if (id == R.id.timeout_back || id == R.id.timeout_return) {
            if (mControllerCallback != null) {
                mControllerCallback.onBackPressed(SuperPlayerDef.PlayerMode.FULLSCREEN);
            }
        } else if (id == R.id.timeout_resume) {
            if (mControllerCallback != null) {
                mControllerCallback.verify(SuperPlayerDef.VerifyReason.TIMEOUT);
            }
        }
    }

    @Override
    public void onTimeout(boolean timeout) {
        super.onTimeout(timeout);
        this.setOnClickListener(timeout ? this : null);
    }

    @Override
    public void hide() {
        super.hide();
        this.setOnClickListener(null);
    }

    @Override
    public void show() {
        super.show();
        this.setOnClickListener(this);
    }
}
