package com.duben.microtribe.ui.fragment.base;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.duben.microtribe.MintsApplication;
import com.duben.microtribe.R;
import com.duben.microtribe.common.AppConfig;
import com.duben.microtribe.ui.widgets.LoadingDialog;
import com.duben.microtribe.utils.ToastUtil;
import com.duben.library.base.BaseAppFragment;


/**
 * 作者：孟崔广
 * 时间：2018/9/13 14:37
 * 邮箱：mengcg@xiaojinqb.cn
 */
public abstract class BaseFragment extends BaseAppFragment {

    private LoadingDialog progressDialog;

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

    public MintsApplication getBaseApplication() {
        return (MintsApplication) getActivity().getApplication();
    }

    public Context getContext() {
        return getActivity();
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    /**
     * 显示加载进度条（自定义message）
     *
     * @param message
     */
    public void showLoading(String message) {
        if (getActivity().getWindow() != null && !getActivity().isFinishing()) {
            if (progressDialog == null) {
                progressDialog = new LoadingDialog(getActivity());
                progressDialog.setLoadText(message);
            }
            progressDialog.show();
            setProgressOnTouchOutside(false);
            setProgressNoDismiss();
        }
    }

    /**
     * 设置Progress是否手触消失
     *
     * @param onTouchOutside
     */
    public void setProgressOnTouchOutside(boolean onTouchOutside) {
        if (progressDialog != null)
            progressDialog.setCanceledOnTouchOutside(onTouchOutside);
    }

    /**
     * 设置点击返回是否消失加载进度条
     */
    public final void setProgressNoDismiss() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
    }

    /**
     * 消失加载进度条
     */
    public final void hideLoading() {
        try {
            if (getActivity().getWindow() != null && !getActivity().isFinishing()) {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            progressDialog = null;
        }
    }

    /**
     * 弹出系统的Toast
     */
    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg))
            ToastUtil.show(MintsApplication.getContext(), msg);
    }

    /**
     * 显示提示信息
     *
     * @since 2.5.0
     */
    protected void showMissingPermissionDialog(String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.notifyTitle);
        //当前应用缺少必要权限。\n\n请点击\"设置\"-\"权限\"-打开所需权限。
        builder.setMessage(getString(R.string.dialog_pos_text1) + msg + getString(R.string.dialog_pos_text2));

        // 拒绝, 退出应用
        builder.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        builder.setPositiveButton(R.string.setting,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + getContext().getPackageName()));
                        startActivity(intent);
                    }
                });

        builder.setCancelable(false);

        builder.show();
    }
}
