/**
 * 加载状态管理 Helper
 * 从 EZRealPlayActivity 拆分出来，负责播放器加载区域的 UI 状态切换
 */
package com.videogo.ui.realplay.helper;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.videogo.ui.realplay.EZRealPlayActivity;
import com.videogo.widget.loading.LoadingTextView;

import java.util.Random;

import ezviz.ezopensdk.R;

/**
 * 加载状态管理 Helper
 * 管理播放器加载区域的 Loading/成功/失败/停止 四种状态 UI 切换
 *
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class LoadingStateHelper {

    private static final String TAG = "LoadingStateHelper";

    private RelativeLayout mRealPlayLoadingRl;// 预览加载父视图
    private TextView mRealPlayTipTv;// 错误信息提示文本
    private ImageView mRealPlayPlayIv;// 播放视图正中间播放按钮
    private LoadingTextView mRealPlayPlayLoading;// 播放加载进程UI

    private final EZRealPlayActivity mActivity;// Activity 引用

    public LoadingStateHelper(EZRealPlayActivity activity) {
        this.mActivity = activity;
        initView();
    }

    /**
     * 初始化 Loading 区域的 View
     */
    private void initView() {
        mRealPlayLoadingRl = (RelativeLayout) mActivity.findViewById(R.id.realplay_loading_rl);
        mRealPlayTipTv = (TextView) mActivity.findViewById(R.id.realplay_tip_tv);
        mRealPlayPlayIv = (ImageView) mActivity.findViewById(R.id.realplay_play_iv);
        mRealPlayPlayLoading = (LoadingTextView) mActivity.findViewById(R.id.realplay_loading);
        mRealPlayPlayIv.setOnClickListener(v -> mActivity.playerStartRealPlay());
    }

    // region --- 公开方法 ---

    /**
     * 更新加载进度
     * @param progress 当前进度值
     */
    public void updateLoadingProgress(final int progress) {
        mRealPlayPlayLoading.setTag(Integer.valueOf(progress));
        mRealPlayPlayLoading.setText(progress + "%");
        if (mActivity.mHandler != null) {
            mActivity.mHandler.postDelayed(() -> {
                if (mRealPlayPlayLoading != null) {
                    Integer tag = (Integer) mRealPlayPlayLoading.getTag();
                    if (tag != null && tag.intValue() == progress) {
                        Random r = new Random();
                        mRealPlayPlayLoading.setText((progress + r.nextInt(20)) + "%");
                    }
                }
            }, 500);
        }
    }

    /**
     * 设置为加载中状态
     */
    public void setStartLoading() {
        mRealPlayLoadingRl.setVisibility(View.VISIBLE);
        mRealPlayTipTv.setVisibility(View.GONE);
        mRealPlayPlayLoading.setVisibility(View.VISIBLE);
        mRealPlayPlayIv.setVisibility(View.GONE);
    }

    /**
     * 设置为停止加载状态（显示播放按钮）
     */
    public void setStopLoading() {
        mRealPlayLoadingRl.setVisibility(View.VISIBLE);
        mRealPlayTipTv.setVisibility(View.GONE);
        mRealPlayPlayLoading.setVisibility(View.GONE);
        mRealPlayPlayIv.setVisibility(View.VISIBLE);
    }

    /**
     * 设置为加载失败状态
     * @param errorStr 错误提示文本
     */
    public void setLoadingFail(String errorStr) {
        mRealPlayLoadingRl.setVisibility(View.VISIBLE);
        mRealPlayTipTv.setVisibility(View.VISIBLE);
        mRealPlayTipTv.setText(errorStr);
        mRealPlayPlayLoading.setVisibility(View.GONE);
        mRealPlayPlayIv.setVisibility(View.GONE);
    }

    /**
     * 设置为加载成功状态（隐藏整个 Loading 区域）
     */
    public void setLoadingSuccess() {
        mRealPlayLoadingRl.setVisibility(View.INVISIBLE);
        mRealPlayTipTv.setVisibility(View.GONE);
        mRealPlayPlayLoading.setVisibility(View.GONE);
        mRealPlayPlayIv.setVisibility(View.GONE);
    }

    /**
     * 设置网络不可用提示
     * @param networkTip 网络不可用提示文本
     */
    public void setNetworkUnavailableTip(String networkTip) {
        mRealPlayTipTv.setText(networkTip);
    }

    // endregion
}
