package com.tenlionsoft.baselib.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.tenlionsoft.baselib.core.widget.base.CustomDialog;

import net.lucode.hackware.magicindicator.MagicIndicator;

import androidx.viewpager2.widget.ViewPager2;

/**
 * 提示工具类
 */
public class UIUtil {
    /**
     * 显示短toast
     */
    public static void sToast(final Context context, final String msg) {
        new Handler(context.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 显示长toast
     */
    public static void lToast(final Context ctx, final String msg) {
        new Handler(ctx.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 圆形进度条dialog
     *
     * @param context
     * @param tips
     * @return
     */
    public static ProgressDialog initDialog(Context context, String tips) {
        CustomDialog dialog = new CustomDialog(context, tips);

        return dialog;
    }


    public interface ViewPageSelectListener {
        void onPageSelect(int pos);
    }

    /**
     * viewPage2与magicIn绑定
     */
    public static void bindViewPager2(MagicIndicator mi, ViewPager2 vp, ViewPageSelectListener listener) {
        vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                mi.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mi.onPageSelected(position);
                if (listener != null) {
                    listener.onPageSelect(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                mi.onPageScrollStateChanged(state);
            }
        });
    }

}
