package com.tofly.ghrq_route.common.ui.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

/**
 * Created by lihualong on 2017/6/22.
 */

public abstract class BaseBottomSheetFragment extends BottomSheetDialogFragment {

    public Context mContext;
    private View rootView;
    public BottomSheetDialog mDialog;
    public BottomSheetBehavior<View> behavior;

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

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null){
            parent.removeView(rootView);
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = LayoutInflater.from(this.mContext).inflate(getResourceId(), null);
            initView(rootView);
        }
        resetView(rootView);
        mDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        mDialog.setContentView(rootView);
        behavior = BottomSheetBehavior.from((View) rootView.getParent());
        behavior.setHideable(true);
//        ((View)rootView.getParent()).setBackgroundColor(Color.TRANSPARENT);
        rootView.post(new Runnable() {
            @Override
            public void run() {
                behavior.setPeekHeight(rootView.getHeight());
            }
        });
        return mDialog;
    }

    public void resetView(View rootView) {

    }

    /**
     * 初始化 views
     * @param rootView
     */
    protected abstract void initView(View rootView);
/**
 * 设置布局文件 id
 */
    protected abstract int getResourceId();

    public boolean isShowing(){
        if (mDialog != null && mDialog.isShowing()){
            return true;
        }else {
            return false;
        }
    }


}
