package com.gh.gamecenter.common.view;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.gh.gamecenter.common.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.ArrowDrawable;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;
import com.scwang.smartrefresh.layout.util.DensityUtil;

@SuppressLint("RestrictedApi")
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class SwipeRefreshHeader extends AbstractSwipeRefreshHeader<SwipeRefreshHeader> implements RefreshHeader {

    public static final byte ID_TEXT_UPDATE = 4;

    public static String REFRESH_HEADER_PULLING = "下拉可以刷新";//"下拉可以刷新";
    public static String REFRESH_HEADER_REFRESHING = "正在刷新...";//"正在刷新...";
    public static String REFRESH_HEADER_LOADING = "刷新中...";//"正在加载...";
    public static String REFRESH_HEADER_RELEASE = "释放立即刷新";//"释放立即刷新";
    public static String REFRESH_HEADER_FINISH = "刷新完成";//"刷新完成";
    public static String REFRESH_HEADER_FAILED = "获取失败，请检查网络设置";//"刷新失败";

    private TextView successView;

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

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

    public SwipeRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final View arrowView = mArrowView;
        final View progressView = mProgressView;
        final DensityUtil density = new DensityUtil();

        successView = (TextView) LayoutInflater.from(context).inflate(R.layout.piece_refresh_hint, null);
        LinearLayout.LayoutParams successViewLp = new LinearLayout.LayoutParams(MATCH_PARENT, density.dip2px(45));
        addView(successView, successViewLp);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ClassicsHeader);

        LayoutParams lpArrow = (LayoutParams) arrowView.getLayoutParams();
        LayoutParams lpProgress = (LayoutParams) progressView.getLayoutParams();
        LinearLayout.LayoutParams lpUpdateText = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        lpUpdateText.topMargin = ta.getDimensionPixelSize(R.styleable.ClassicsHeader_srlTextTimeMarginTop, density.dip2px(0));
        lpProgress.rightMargin = ta.getDimensionPixelSize(R.styleable.ClassicsFooter_srlDrawableMarginRight, density.dip2px(10));
        lpArrow.rightMargin = lpProgress.rightMargin;

        lpArrow.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableArrowSize, lpArrow.width);
        lpArrow.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableArrowSize, lpArrow.height);
        lpProgress.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableProgressSize, lpProgress.width);
        lpProgress.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableProgressSize, lpProgress.height);

        lpArrow.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpArrow.width);
        lpArrow.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpArrow.height);
        lpProgress.width = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpProgress.width);
        lpProgress.height = ta.getLayoutDimension(R.styleable.ClassicsHeader_srlDrawableSize, lpProgress.height);

        mFinishDuration = ta.getInt(R.styleable.ClassicsHeader_srlFinishDuration, 1000);
        mSpinnerStyle = SpinnerStyle.values()[ta.getInt(R.styleable.ClassicsHeader_srlClassicsSpinnerStyle, mSpinnerStyle.ordinal())];

        if (ta.hasValue(R.styleable.ClassicsHeader_srlDrawableArrow)) {
            mArrowView.setImageDrawable(ta.getDrawable(R.styleable.ClassicsHeader_srlDrawableArrow));
        } else {
            mArrowDrawable = new ArrowDrawable();
            mArrowDrawable.setColor(0xff666666);
            mArrowView.setImageResource(R.drawable.refresh_icon);
        }

        if (ta.hasValue(R.styleable.ClassicsHeader_srlDrawableProgress)) {
            mProgressView.setImageDrawable(ta.getDrawable(R.styleable.ClassicsHeader_srlDrawableProgress));
        } else {
            mProgressDrawable = new ProgressDrawable();
            mProgressDrawable.setColor(0xff666666);
            mProgressView.setImageResource(R.drawable.refresh_anim);
        }

        if (ta.hasValue(R.styleable.ClassicsHeader_srlTextSizeTitle)) {
            mTitleText.setTextSize(TypedValue.COMPLEX_UNIT_PX, ta.getDimensionPixelSize(R.styleable.ClassicsHeader_srlTextSizeTitle, DensityUtil.dp2px(12)));
        } else {
            mTitleText.setTextSize(12);
        }

        if (ta.hasValue(R.styleable.ClassicsHeader_srlPrimaryColor)) {
            setPrimaryColor(ta.getColor(R.styleable.ClassicsHeader_srlPrimaryColor, 0));
        }
        if (ta.hasValue(R.styleable.ClassicsHeader_srlAccentColor)) {
            setAccentColor(ta.getColor(R.styleable.ClassicsHeader_srlAccentColor, 0));
        }

        ta.recycle();

        mTitleText.setText(this.isInEditMode() ? REFRESH_HEADER_REFRESHING : REFRESH_HEADER_PULLING);
    }

    public void setFinishText(String text) {
        REFRESH_HEADER_FINISH = text;
    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {
        mCenterLayout.setVisibility(GONE);
        mArrowView.setVisibility(GONE);
        mProgressView.setVisibility(GONE);
        if (success) {
            successView.setVisibility(VISIBLE);
            successView.setText(REFRESH_HEADER_FINISH);
            return super.onFinish(layout, success);
        } else {
            successView.setVisibility(GONE);
            super.onFinish(layout, success);
            return 0;
        }
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        switch (newState) {
            case PullDownToRefresh:
                successView.setVisibility(GONE);
                mCenterLayout.setVisibility(VISIBLE);
                mTitleText.setText(REFRESH_HEADER_PULLING);
                mArrowView.setVisibility(VISIBLE);
//                mArrowView.animate().rotation(0);
                break;
            case Refreshing:
            case RefreshReleased:
                successView.setVisibility(GONE);
                mCenterLayout.setVisibility(VISIBLE);
                mTitleText.setText(REFRESH_HEADER_REFRESHING);
                mArrowView.setVisibility(GONE);
                break;
            case ReleaseToRefresh:
                successView.setVisibility(GONE);
                mCenterLayout.setVisibility(VISIBLE);
                mTitleText.setText(REFRESH_HEADER_RELEASE);
//                mArrowView.animate().rotation(180);
                break;
            case Loading:
                successView.setVisibility(GONE);
                mCenterLayout.setVisibility(VISIBLE);
                mArrowView.setVisibility(GONE);
                mTitleText.setText(REFRESH_HEADER_LOADING);
                break;
            case None:
                successView.setVisibility(GONE);
                REFRESH_HEADER_FINISH = "";
                break;
        }
    }

    public SwipeRefreshHeader setAccentColor(@ColorInt int accentColor) {
        return super.setAccentColor(accentColor);
    }

}

