package com.gh.gamecenter.common.viewholder;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.gh.gamecenter.common.R;
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder;
import com.gh.gamecenter.common.base.activity.BaseActivity;
import com.gh.gamecenter.common.baselist.ListViewModel;
import com.gh.gamecenter.common.baselist.LoadType;
import com.gh.gamecenter.common.callback.OnListClickListener;

import java.util.ArrayList;

public class FooterViewHolder extends BaseRecyclerViewHolder {

    private ProgressBar loading;
    private TextView hint;

    public FooterViewHolder(View itemView) {
        super(itemView);
        loading = itemView.findViewById(R.id.footerview_loading);
        hint = itemView.findViewById(R.id.footerview_hint);
    }

    public FooterViewHolder(View itemView, OnListClickListener listClickListener) {
        super(itemView, listClickListener);
        loading = itemView.findViewById(R.id.footerview_loading);
        hint = itemView.findViewById(R.id.footerview_hint);
        itemView.setOnClickListener(this);
    }

    // 减去recyclerView item间隔（个别列表间隔很大）
    public void initItemPadding() {
        itemView.setPadding(0, 0, 0, 0);
    }

    public void initFooterViewHolder(boolean isNetworkError, boolean isOver, @StringRes int loadOverHint) {
        if (isNetworkError) {
            loading.setVisibility(View.GONE);
            hint.setText(R.string.loading_failed_retry);
        } else if (isOver) {
            loading.setVisibility(View.GONE);
            hint.setText(loadOverHint);
        } else {
            loading.setVisibility(View.VISIBLE);
            hint.setText(R.string.loading);
        }
    }

    public void initFooterViewHolder(ListViewModel viewModel, boolean isLoading, boolean isNetworkError, boolean isOver) {
        initFooterViewHolder(isLoading, isNetworkError, isOver, R.string.load_over_hint, v -> {
            if (isNetworkError) viewModel.load(LoadType.RETRY);
        });
    }

    public void bindFooterDefaultEmpty(ListViewModel viewModel, boolean isLoading, boolean isNetworkError, boolean isOver) {
        if (isNetworkError) {
            loading.setVisibility(View.GONE);
            hint.setVisibility(View.VISIBLE);
            hint.setText(R.string.loading_failed_retry);
        } else if (isOver) {
            loading.setVisibility(View.GONE);
            hint.setVisibility(View.VISIBLE);
            hint.setText(R.string.load_over_hint);
        } else if (isLoading) {
            loading.setVisibility(View.VISIBLE);
            hint.setVisibility(View.VISIBLE);
            hint.setText(R.string.loading);
        } else {
            loading.setVisibility(View.GONE);
            hint.setVisibility(View.GONE);
        }
        itemView.setOnClickListener(v -> {
            if (isNetworkError) viewModel.load(LoadType.RETRY);
        });
    }

    public void initFooterViewHolder(boolean isLoading, boolean isNetworkError, boolean isOver) {
        initFooterViewHolder(isLoading, isNetworkError, isOver, R.string.load_over_hint);
    }

    public void initFooterViewHolder(boolean isLoading, boolean isNetworkError, boolean isOver, @StringRes int loadOverHint) {
        BaseActivity.updateStaticView(itemView, new ArrayList<>());
        if (isNetworkError) {
            loading.setVisibility(View.GONE);
            hint.setText(R.string.loading_failed_retry);
        } else if (isOver) {
            loading.setVisibility(View.GONE);
            hint.setText(loadOverHint);
        } else if (isLoading) {
            loading.setVisibility(View.VISIBLE);
            hint.setText(R.string.loading);
        } else {
            loading.setVisibility(View.GONE);
            hint.setText(R.string.loading_more_hint);
        }
    }

    public void initFooterViewHolder(boolean isLoading, boolean mIsNetworkError, boolean mIsOver, OnClickListener onClickListener) {
        initFooterViewHolder(isLoading, mIsNetworkError, mIsOver, R.string.load_over_hint, onClickListener);
    }

    public void initFooterViewHolder(boolean isLoading, boolean mIsNetworkError, boolean mIsOver, @StringRes int loadOverHint, OnClickListener onClickListener) {
        if (mIsNetworkError) {
            loading.setVisibility(View.GONE);
            hint.setText(R.string.loading_failed_retry);
            itemView.setClickable(true);
            itemView.setOnClickListener(onClickListener);
        } else if (mIsOver) {
            loading.setVisibility(View.GONE);
            hint.setText(loadOverHint);
            itemView.setClickable(true);
            itemView.setOnClickListener(onClickListener);
        } else if (isLoading) {
            loading.setVisibility(View.VISIBLE);
            hint.setText(R.string.loading);
            itemView.setClickable(false);
        } else {
            loading.setVisibility(View.GONE);
            hint.setText(R.string.loading_more_hint);
            itemView.setClickable(false);
        }
    }

    public ProgressBar getLoading() {
        return loading;
    }

    public TextView getHint() {
        return hint;
    }
}