package com.zxhl.cms.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.google.android.material.snackbar.Snackbar;
import com.zxhl.cms.common.Constant;
import com.zxhl.cms.utils.Utils;
import com.trello.rxlifecycle2.components.support.RxFragment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


/**
 * Created by gaoleichao on 2018/5/2.
 */

public abstract class BaseFragment extends RxFragment {
    public Activity mActivity;
    public View fragmentView;
    protected boolean isInitial = false;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        before();
        if (fragmentView == null && layoutID() > 0) {
            fragmentView = inflater.inflate(layoutID(), container, false);
        }
        after(fragmentView);

        return fragmentView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isInitial = true;
        init(fragmentView);
        data();
    }

    protected void before() {
    }

    protected void after(View view) {
        mActivity = getActivity();
    }

    public abstract int layoutID();

    public abstract void init(View view);

    public void data() {

    }


    public void destoryView() {
        Utils.destory(fragmentView);
    }

    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
//        overridePendingTransition(R.anim.slide_in_right,
//                R.anim.activity_close_enter);
    }


    /**
     * 启动Activity
     */
    protected void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    /**
     * 启动Activity
     */
    protected void openActivity(String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
//        overridePendingTransition(R.anim.slide_in_right,
//                R.anim.activity_close_enter);
    }

    /**
     * 短时间显示Toast
     *
     * @param info 显示的内容
     */
    public void showToast(String info) {
        Context context = getActivity();
        // 防止未Attach到界面上时，getActivity为空
        if (context != null) {
            Utils.showToast(context, info);
        }
    }

    public void showSnake(View view, String info) {
        Snackbar.make(view, info, Snackbar.LENGTH_LONG).show();
    }

    public void showSnake(View view, String info, String actionInfo, View.OnClickListener listener) {
        Snackbar.make(view, info, Snackbar.LENGTH_LONG).setAction(actionInfo, listener).show();
    }

    /**
     * 短时间显示Toast
     *
     * @param resId 显示的内容
     */
    public void showToast(int resId) {
        showToast(getString(resId));
    }

    public void showSnackbar(@NonNull String text) {
        showSnackbar(text, "", null);
    }

    public void showSnackbar(@NonNull String text, @NonNull String actionStr, View.OnClickListener listener) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            showSnackbar(new View(getContext()), text, actionStr, listener);
        }
    }

    public void showSnackbar(@NonNull View view, @NonNull String text, @NonNull String actionStr, View.OnClickListener listener) {
        Snackbar.make(view, text, Snackbar.LENGTH_LONG).setAction(actionStr, listener).show();
    }

    protected boolean hasLiveProcess() {
        android.app.ActivityManager appAM = (android.app.ActivityManager) getActivity().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (android.app.ActivityManager.RunningAppProcessInfo appProcess : appAM.getRunningAppProcesses()) {
            if (appProcess.processName.contains(":live")) {
                return true;
            }
        }
        return false;
    }

    // 检测MIUI
    private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
    private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
    private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";

    public boolean isNeedAdapterPhone() {
        return Build.VERSION.SDK_INT < 21;

//        if (SettingPreference.isSaveNeedAdapterPhone(getContext())) {
//            return SettingPreference.isNeedAdapterPhone(getContext());
//        }
//        boolean b = isMIUI() || isFuckPhone();
//        SettingPreference.setIsSaveNeedAdapterPhone(getContext(), true);
//        SettingPreference.setIsNeedAdapterPhone(getContext(), b);
//
//        return b;
    }

    private boolean isFuckPhone() {
        if (Build.VERSION.SDK_INT < 21) {// && !str.contains("nexus")
            return true;
        } else {
            return false;
        }
    }

    //是否小米手机
    private boolean isMIUI() {
        Properties prop = new Properties();
        boolean isMIUI;
        try {
            prop.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        isMIUI = prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
                || prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
                || prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null;

        return isMIUI;
    }

    protected boolean isVisible = false;


    /**
     * 设置状态栏背景色
     *
     * @param color
     */
    protected void setStatusBarBackground(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //注意要清除 FLAG_TRANSLUCENT_STATUS flag
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getActivity().getWindow().setStatusBarColor(color);
        }
    }

    protected void onVisible() {
        lazyLoad();
    }

    protected void lazyLoad() {

    }

    protected void onInvisible() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (getActivity() != null) {
            Glide.get(getActivity()).clearMemory();
        }
    }

    /**
     * 在这里实现Fragment数据的缓加载.
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisible) {
            onVisible();
        } else {
            onInvisible();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);{
            isVisible = !hidden;
            if (isVisible) {
                onVisible();
            } else {
                onInvisible();
            }
        }
    }
}