package com.guadou.cs_cptservices.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.guadou.cs_cptservices.R;
import com.guadou.lib_baselib.utils.log.YYLogUtils;

/**
 * Loading + Retry 展示的TextView
 */
public class LoadRetryTextView extends FrameLayout {

    private TextView mTvContent;
    private View mPbLoading;
    private View mIvRetry;

    public LoadRetryTextView(@NonNull Context context) {
        this(context, null, 0);
    }

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

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

        inflate(context, R.layout.view_load_retry_textview, this);

        mTvContent = findViewById(R.id.tv_content);
        mPbLoading = findViewById(R.id.pb_loading);
        mIvRetry = findViewById(R.id.iv_retry);

        mIvRetry.setOnClickListener(v -> {
            if (mListener != null) mListener.onRetry();
        });
    }

    public void setCustomStyle(Typeface typeface, int textSize, int textColor) {
        this.setCustomStyle(typeface, textSize, textColor, 0, 0);
    }

    public void setCustomStyle(Typeface typeface, int textSize, int textColor, int gravity, int iconSize) {
        if (typeface != null) {
            mTvContent.setTypeface(typeface);
        }
        if (textSize != 0) {
            mTvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        }
        if (textColor != 0) {
            mTvContent.setTextColor(textColor);
        }
        if (gravity != 0) {
            LayoutParams layoutParams1 = (LayoutParams) mTvContent.getLayoutParams();
            layoutParams1.gravity = gravity;
            mTvContent.setLayoutParams(layoutParams1);

            LayoutParams layoutParams2 = (LayoutParams) mPbLoading.getLayoutParams();
            layoutParams2.gravity = gravity;
            if (iconSize != 0) {
                layoutParams2.width = iconSize;
                layoutParams2.height = iconSize;
            }
            mPbLoading.setLayoutParams(layoutParams2);

            LayoutParams layoutParams3 = (LayoutParams) mIvRetry.getLayoutParams();
            layoutParams3.gravity = gravity;
            if (iconSize != 0) {
                layoutParams3.width = iconSize;
                layoutParams3.height = iconSize;
            }
            mIvRetry.setLayoutParams(layoutParams3);
        }
    }

    public TextView getTextView() {
        return mTvContent;
    }

    public void showLoadingState() {
        mIvRetry.setVisibility(GONE);
        mTvContent.setVisibility(GONE);
        mPbLoading.setVisibility(VISIBLE);
    }

    public void showTextState(String text) {
        mIvRetry.setVisibility(GONE);
        mTvContent.setVisibility(VISIBLE);
        mPbLoading.setVisibility(GONE);
        YYLogUtils.w("赋值："+text);
        mTvContent.setText(text);


    }

    public void showRetryState() {
        mIvRetry.setVisibility(VISIBLE);
        mTvContent.setVisibility(GONE);
        mPbLoading.setVisibility(GONE);
    }

    public String getText() {
        return mTvContent.getText().toString();
    }

    private OnRetryListener mListener;

    public void setOnRetryListener(OnRetryListener listener) {
        mListener = listener;
    }


    public interface OnRetryListener {
        void onRetry();
    }
}
