package com.wisewoods.base.base;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.Utils;
import com.wisewoods.base.utils.DialogUtils;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;

import retrofit2.Call;

public class BaseLogic<VB extends ViewBinding> {

    // 两次点击按钮之间的点击间隔不能少于300毫秒
    private static final int MIN_CLICK_DELAY_TIME = 300;
    private static long lastClickTime;

    public boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    private Object object;
    private VB binding;
    private ArrayList<Call> httpList = new ArrayList<>();
    private Dialog dialog;

    public BaseLogic(Object obj) {
        this.object = obj;
    }

    public VB getBinding() {
        return binding;
    }

    public void init() {
        if (object instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) object;
            if (activity.hasEvent()) {
                EventBus.getDefault().register(object);
            }
        } else if (object instanceof BaseFragment) {
            BaseFragment fragment = (BaseFragment) object;
            if (fragment.hasEvent()) {
                EventBus.getDefault().register(object);
            }
        }
    }

    public View initData(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = null;
        if (object instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) object;
            Type superclass = activity.getClass().getGenericSuperclass();
            if (superclass instanceof ParameterizedType) {
                Type[] types = ((ParameterizedType) superclass).getActualTypeArguments();
                Class<VB> aClass = (Class<VB>) types[0];
                try {
                    Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
                    binding = (VB) method.invoke(null, activity.getLayoutInflater());
                    activity.setContentView(binding.getRoot());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                if (getLayout() instanceof Integer) {
                    activity.setContentView((Integer) getLayout());
                } else if (getLayout() instanceof View) {
                    activity.setContentView((View) getLayout());
                }
            }
        } else if (object instanceof BaseFragment) {
            BaseFragment fragment = (BaseFragment) object;
            Type superclass = fragment.getClass().getGenericSuperclass();

            if (superclass instanceof ParameterizedType) {
                Type[] types = ((ParameterizedType) superclass).getActualTypeArguments();
                Class<VB> aClass = (Class<VB>) types[0];
                try {
                    Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
                    binding = (VB) method.invoke(null, fragment.getLayoutInflater(), container, false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                view = binding.getRoot();
            } else {
                if (getLayout() instanceof Integer) {
                    view = inflater.inflate((Integer) getLayout(), container, false);
                } else if (getLayout() instanceof View) {
                    view = (View) getLayout();
                }
            }
        }
        return view;

    }

    protected <T> Call<T> addCall(Call<T> call) {
        httpList.add(call);
        return call;
    }

    public void onD() {
        for (Call call : httpList) {
            if (call != null && !call.isExecuted()) {
                call.cancel();
            }
        }
        if (object instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) object;
            if (activity.hasEvent()) {
                EventBus.getDefault().unregister(object);
            }
        } else if (object instanceof BaseFragment) {
            BaseFragment fragment = (BaseFragment) object;
            if (fragment.hasEvent()) {
                EventBus.getDefault().unregister(object);
            }
        }
    }

    protected Object getLayout() {
        TextView tv = new TextView(Utils.getApp());
        tv.setText("测试");
        return tv;
    }

    protected void showLoading() {
        dialog = DialogUtils.showMD();
    }

    protected void hideLoading() {
        DialogUtils.dissmiss(dialog);
    }
}
