/**
 * 加载状态管理 Helper
 * 从 EZPlayBackListActivity 拆分出来，负责播放区域的 Loading/成功/失败/完成 四种状态 UI 切换
 */
package com.videogo.ui.playback.helper;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.videogo.ui.playback.EZPlayBackListActivity;
import com.videogo.widget.loading.LoadingView;

import ezviz.ezopensdk.R;

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

    private static final String TAG = "LoadingStateHelper";

    private LoadingView mPlaybackPlayLoading;// 播放加载进程UI
    private ViewGroup errorTipsVg;// 错误信息显示父视图
    private ImageView playbackFinishedIv;// 播放完成图标
    private TextView errorInfoTV;// 错误信息
    private Button retryBtn;// 重试按钮

    private final EZPlayBackListActivity mActivity;// Activity 引用

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

    /**
     * 初始化 Loading 区域的 View
     */
    private void initView() {
        mPlaybackPlayLoading = (LoadingView) mActivity.findViewById(R.id.playback_loading);
        errorTipsVg = (ViewGroup) mActivity.findViewById(R.id.vg_error_tips);
        playbackFinishedIv = (ImageView) mActivity.findViewById(R.id.playback_finished_iv);
        errorInfoTV = (TextView) mActivity.findViewById(R.id.error_info_tv);
        retryBtn = (Button) mActivity.findViewById(R.id.playback_retry_btn);
        retryBtn.setOnClickListener(v -> mActivity.onRetryPlayback());
    }

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

    /**
     * 设置为加载中状态（开始播放前调用）
     */
    public void setStartLoading() {
        mPlaybackPlayLoading.setVisibility(View.VISIBLE);
        errorTipsVg.setVisibility(View.GONE);
        playbackFinishedIv.setVisibility(View.GONE);
        retryBtn.setVisibility(View.GONE);
    }

    /**
     * 设置为播放成功状态（隐藏 Loading 和错误区域）
     */
    public void setLoadingSuccess() {
        mPlaybackPlayLoading.setVisibility(View.GONE);
        errorTipsVg.setVisibility(View.GONE);
        playbackFinishedIv.setVisibility(View.GONE);
        retryBtn.setVisibility(View.GONE);
    }

    /**
     * 设置为加载失败状态（显示错误信息 + 重试按钮）
     * @param errorStr 错误提示文本
     */
    public void setLoadingFail(final String errorStr) {
        mActivity.runOnUiThread(() -> {
            errorTipsVg.setVisibility(View.VISIBLE);
            mPlaybackPlayLoading.setVisibility(View.GONE);
            playbackFinishedIv.setVisibility(View.GONE);
            retryBtn.setVisibility(View.VISIBLE);
            errorInfoTV.setText(errorStr);
        });
    }

    /**
     * 设置为播放完成状态（显示完成图标 + 提示信息，不显示重试按钮）
     * @param tip 完成提示文本
     */
    public void setPlaybackFinish(final String tip) {
        mActivity.runOnUiThread(() -> {
            mPlaybackPlayLoading.setVisibility(View.GONE);
            errorTipsVg.setVisibility(View.VISIBLE);
            playbackFinishedIv.setVisibility(View.VISIBLE);
            retryBtn.setVisibility(View.GONE);
            errorInfoTV.setText(tip);
        });
    }

    /**
     * 回放进度条拖动后，重置 Loading 区域可见性
     */
    public void resetSeekPlayUI() {
        errorTipsVg.setVisibility(View.GONE);
        mPlaybackPlayLoading.setVisibility(View.VISIBLE);
        playbackFinishedIv.setVisibility(View.GONE);
        retryBtn.setVisibility(View.GONE);
    }

    // endregion
}
