package com.stu.assistant.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.stu.assistant.R;
import com.stu.assistant.uitls.Config;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedNoClearUtil;
import com.stu.assistant.uitls.SharedUtil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import cn.bmob.v3.util.V;

public abstract class BaseActivity extends FragmentActivity {
    public static Context mContext;
    private Config config;
    private ProgressDialog progressDialog;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        String string = SharedNoClearUtil.create(BaseApplion.application).getString(SharedConfig.isTheme, SharedConfig.isThemeSys);
        if (string.equals(SharedConfig.isThemeSys)) {
            setTheme(R.style.AppTheme);
        } else {
            setTheme(R.style.MyAppTheme);
        }
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置activity只能竖屏
        mContext = this;
        //沉浸式状态栏
        initImmersionBar();
        //加入Activity管理器l
        BaseApplion.application.getActivityManage().addActivity(this);
        //加载当前页面
        setContentView(R.layout.base_activity_layout);
        ((ViewGroup) findViewById(R.id.fl_content)).addView(getLayoutInflater().inflate(getLayoutId(), null));
        Html.fromHtml("&yen");
    }

    public void showProDialog(String msg) {
        progressDialog = ProgressDialog.show(this, null, msg, true, true);
        progressDialog.show();
    }

    public void dismissProDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        initView();
        initData();
        initListener();
    }

    public Config getConfig() {
        if (config == null) {
            config = new Config();
        }
        return config;
    }


    protected void setTextMsg(int id, String msg) {
        TextView textView = (TextView) findViewById(id);
        textView.setVisibility(View.VISIBLE);
        textView.setText(msg);
    }

    protected void setTitleLayView(int id) {
        LinearLayout backLay = (LinearLayout) findViewById(id);
        backLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    protected void setTitleLay(int tvId, int layId, String msg) {
        setTextMsg(tvId, msg);
        setTitleLayView(layId);
    }


    /**
     * 沉浸栏颜色
     */
    protected void initImmersionBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()  //透明状态栏，不写默认透明色
                .fitsSystemWindows(false)    //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .init();  //必须
    }


    @Override
    protected void onStop() {
        super.onStop();
        dismissProDialog();
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();
}
