package com.azhon.basic.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.azhon.basic.view.LoadingDialog;

/**
 * 项目名:    TODO-MVVM
 * 包名       com.azhon.basic.base
 * 文件名:    BaseNoModelFragment
 * 创建时间:  2019-03-28 on 17:06
 * 描述:     TODO 不需要ViewModel的页面基类
 *
 * @author xuhuixiang
 */

public abstract class BaseNoModelFragment<DB extends ViewDataBinding> extends Fragment {

    protected DB dataBinding;
    protected Context context;
    protected FragmentActivity activity;
    private LoadingDialog loadingDialog;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = initDataBinding(inflater, setLayoutResourceID(), container);
        return dataBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = getActivity();
        setUpView();
        setUpData();
    }

    /**
     * 初始化要加载的布局资源ID
     */
    protected abstract int setLayoutResourceID();


    /**
     * 初始化DataBinding
     */
    protected DB initDataBinding(LayoutInflater inflater, @LayoutRes int layoutId, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, layoutId, container, false);
    }

    /**
     * 初始化视图
     */
    protected abstract void setUpView();

    /**
     * 初始化数据
     */
    protected abstract void setUpData();

    /**
     * 显示用户等待框
     *
     * @param msg 提示信息
     */
    protected void showDialog(String msg) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.setLoadingMsg(msg);
        } else {
            loadingDialog = new LoadingDialog(context);
            loadingDialog.setLoadingMsg(msg);
            loadingDialog.show();
        }
    }
    protected void showToast(String message){
        try {
//            SnackbarUtils.Short(dataBinding.getRoot(),message).show();
            Toast.makeText(dataBinding.getRoot().getContext(),message,Toast.LENGTH_SHORT).show();


        }catch (Exception e){e.printStackTrace();}

    }

    protected void showToast(View view,String message){
        try {
//            SnackbarUtils.Short(view,message).show();
            Toast.makeText(dataBinding.getRoot().getContext(),message,Toast.LENGTH_SHORT).show();

        }catch (Exception e){e.printStackTrace();}


    }

    protected void showToast(View view,String message,int color){
        try {
//            SnackbarUtils.Short(view,message,color).show();
            Toast.makeText(dataBinding.getRoot().getContext(),message,Toast.LENGTH_SHORT).show();

        }catch (Exception e){e.printStackTrace();}


    }

    protected void showToast(String message,int color){
        try {
//            SnackbarUtils.Short(dataBinding.getRoot(),message,color).show();
            Toast.makeText(dataBinding.getRoot().getContext(),message,Toast.LENGTH_SHORT).show();


        }catch (Exception e){e.printStackTrace();}

    }

    /**
     * 隐藏等待框
     */
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dataBinding != null) {
            dataBinding.unbind();
        }
    }


    public View getContentView() {
        return dataBinding.getRoot();
    }

    public Context getMContext() {
        return context;
    }



    /**
     * 隐藏软键盘
     */
    public void hideSoftInput() {
        View v = getActivity().getCurrentFocus();
        if (v != null && v.getWindowToken() != null) {
            InputMethodManager manager = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            boolean isOpen = manager.isActive();
            if (isOpen) {
                manager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
}
