package com.azhon.basic.base;

import android.annotation.SuppressLint;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;

import com.azhon.basic.bean.DialogBean;
import com.azhon.basic.lifecycle.BaseViewModel;


/**
 * 项目名:    TODO-MVVM
 * 包名       com.azhon.basic.base
 * 文件名:    BaseFragment
 * 创建时间:  2019-03-28 on 17:35
 * 描述:     TODO ViewModel、ViewDataBinding都需要的基类
 *
 * @author xuhuixiang
 */

public abstract class BaseFragment<VM extends BaseViewModel, DB extends ViewDataBinding>
        extends BaseNoModelFragment<DB> {

    protected VM viewModel;
    private static Toast toast;

    @Override
    protected DB initDataBinding(LayoutInflater inflater, int layoutId, ViewGroup container) {
        DB db = super.initDataBinding(inflater, layoutId, container);
        /**
         * 将这两个初始化函数插在{@link BaseFragment#initDataBinding}
         */
        viewModel = initViewModel();
        initObserve();
        return db;
    }

    /**
     * 初始化ViewModel
     */
    protected abstract VM initViewModel();

    /**
     * 监听当前ViewModel中 showDialog和error的值
     */
    private void initObserve() {
        if (viewModel == null) return;
        viewModel.getShowDialog(this, new Observer<DialogBean>() {
            @Override
            public void onChanged(DialogBean bean) {
                if (bean.isShow()) {
                    showDialog(bean.getMsg());
                } else {
                    dismissDialog();
                }
            }
        });
        viewModel.getError(this, new Observer<Object>() {
            @Override
            public void onChanged(Object obj) {
                showError(obj);
            }
        });
    }

    /**
     * ViewModel层发生了错误
     */
    protected abstract void showError(Object obj);


//    /**
//     * 显示提示  toast
//     *
//     * @param msg 提示信息
//     */
//    @SuppressLint("ShowToast")
//    public void showToast(String msg) {
//        try {
//            if (null == toast) {
//                toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
//            } else {
//                toast.setText(msg);
//            }
//            getActivity().runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    toast.show();
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//            //解决在子线程中调用Toast的异常情况处理
//            Looper.prepare();
//            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
//            Looper.loop();
//        }
//    }

}
