package com.android.chmo.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.chmo.R;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/30.
 */

public abstract class BaseFragment extends Fragment {
    private View mLayoutView;
    public boolean isVisible;

    /**
     * 初始化布局
     */
    public abstract int getLayoutRes();

    /**
     * 初始化视图
     */
    public abstract void initView();

    @Override
    public Context getContext() {
        return getBaseActivity().getContext();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mLayoutView != null) {
            ViewGroup parent = (ViewGroup) mLayoutView.getParent();
            if (parent != null) {
                parent.removeView(mLayoutView);
            }
        } else {
            mLayoutView = getCreateView(inflater, container);
            ButterKnife.bind(this, mLayoutView);
            initView();     //初始化布局
        }

        return mLayoutView;
    }



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisibleToUser) {
            onVisible();
        } else {
            onInvisible();
            System.gc();
            System.runFinalization();
        }
    }

    public void onVisible() {

    }

    public void  onInvisible(){

    }

    /**
     * 获取Fragment布局文件的View
     *
     * @param inflater
     * @param container
     * @return
     */
    private View getCreateView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(getLayoutRes(), container, false);
    }

    /**
     * 获取当前Fragment状态
     *
     * @return true为正常 false为未加载或正在删除
     */
    private boolean getStatus() {
        return (isAdded() && !isRemoving());
    }

    /**
     * 获取Activity
     *
     * @return
     */
    public BaseActivity getBaseActivity() {
        return (BaseActivity) getActivity();
    }


    public void showToast(String msg) {
        if (getStatus()) {
            getBaseActivity().showToast(msg);
        }
    }

    public void showToast(int res) {
        showToast(getString(res));
    }

    public void showLoading() {
        getBaseActivity().showLoading();
    }

    public void showLoading(String msg) {
        getBaseActivity().showLoading(msg);
    }

    public void hideLoading() {
        getBaseActivity().hideLoading();
    }


    public void openPage(Class clazz) {
        getBaseActivity().openPage(clazz);
    }

    public void openPage(Intent intent) {
        getBaseActivity().openPage(intent);
    }

    public void openPageForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
        getBaseActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
    }

    public void close() {
        getBaseActivity().close();
    }

}
