package com.naz.sdkdemo.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.lzy.okgo.OkGo;
import com.naz.sdkdemo.R;
import com.naz.sdkdemo.weight.ActivityManager;
import com.naz.sdkdemo.weight.TitleBar;
import com.naz.sdkdemo.weight.XToast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;

/**
 * Description:ydd
 * Author: ydd
 * Date:2020/10/22
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    protected TitleBar titleBar;
    private KProgressHUD hud;
   /* public static RFIDWithUHFUART mReader;*/
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);
        ActivityManager.getActivityManager().add(this);
        if (initBundle(getIntent().getExtras())) {
            setContentView(getLayoutResId());
            initTitleBar();
            initWindow();
            ButterKnife.bind(this);
            onRestartInstance(savedInstanceState);
           /* if(mReader==null){
                initUHF();
            }*/
            initView();
            initData();
        } else {
            finish();
        }
    }
   /* private void initUHF() {
        try {
            mReader = RFIDWithUHFUART.getInstance();
        } catch (Exception ex) {
            XToast.showToast(ex.getMessage());
            return;
        }

        if (mReader != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mReader.init();
                }
            }).start();
        }
    }*/

    protected void initTitleBar() {
        try {
            titleBar = findViewById(R.id.title_view);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != titleBar) {
            titleBar.setBackListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            setupTitleBar(titleBar);
        }
    }

    public void showLoading() {
        if (hud == null) {
            hud = KProgressHUD.create(this).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
        }
        hud.show();
    }

    public void hideLoading() {
        if (hud != null) {
            hud.dismiss();
        }
    }
    protected boolean initBundle(Bundle bundle) {
        return true;
    }

    protected abstract int getLayoutResId();

    protected abstract void initView();

    protected void initData() {

    }

    protected void setupTitleBar(TitleBar titleBar) {

    }

    protected void initWindow() {
        if (getSupportActionBar() != null) getSupportActionBar().hide();//去掉继承AppCompatActivity的标题栏
    }

    protected void onRestartInstance(Bundle bundle) {
    }

    @Override
    public void onClick(View v) {
    }

    public void GoPath(Class c) {
        Intent intent = new Intent(this, c);
        startActivity(intent);
    }

    public void GoPath(Class c, Bundle bundle) {
        Intent intent = new Intent(this, c);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void GoPathAndFinish(Class c) {
        GoPath(c);
        finish();
    }
    public void GoPathAndFinish(Class c, Bundle bundle) {
        GoPath(c,bundle);
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        ActivityManager.getActivityManager().remove(this);
    }

    public void initTitleView(String title) {
        if (null == titleBar) {
            try {
                titleBar = findViewById(R.id.title_view);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != titleBar) {
            titleBar.getCenterView().setText(title);
        }
    }

    public void hiddenBackIcon() {
        titleBar.hiddenBack();
    }

    public void setBackIcon(int resId) {
        titleBar.setNavigationIcon(resId);
    }

    public void showToast(String m){
        XToast.showToast(m);
    }
    /**
     * 设置 app 字体不随系统字体设置改变
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }
}
