package com.android.chmo.base;


import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.android.chmo.R;
import com.android.chmo.ui.dialog.LoadingDialog;
import com.netease.nim.avchatkit.common.activity.ActivityManager;
import com.netease.nim.uikit.common.util.StatusBarUtil;

import butterknife.ButterKnife;


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

public abstract class BaseActivity extends AppCompatActivity {
    private Toast toast;
    private LoadingDialog loadingDialog;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentResId());
        ActivityManager.getInstance().pushActivity(this);
        StatusBarUtil.setStatusBarDrakText(this);
        ButterKnife.bind(this);
        initView();

    }

    /**
     * 布局资源文件
     */
    protected abstract int getContentResId();

    /**
     * 初始化控件
     */
    public abstract void initView();

    public Context getContext() {
        return this;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
        ActivityManager.getInstance().popActivity(this);
    }

    public void openPage(Class clazz) {
        openPage(new Intent(this, clazz));
    }

    public void openPage(Intent intent) {
        startActivity(intent);
        overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
    }

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

    public void close() {
        finish();
    }


    public void showToast(String msg) {
        if (!isFinishing()) {
            if (toast == null) {
                toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }

            toast.show();
        }
    }

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

    public void showLoading() {
        this.showLoading(null);
    }

    public void showLoading(String loading) {
        if(loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        loadingDialog.setLoading(loading);
        if(!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }
    public void hideLoading() {
        if(loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration newConfig = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (resources != null && newConfig.fontScale != 1) {
            newConfig.fontScale = 1;
            if (Build.VERSION.SDK_INT >= 17) {
                Context configurationContext = createConfigurationContext(newConfig);
                resources = configurationContext.getResources();
                displayMetrics.scaledDensity = displayMetrics.density * newConfig.fontScale;
            } else {
//                resources.updateConfiguration(newConfig, displayMetrics);
            }
        }
        return resources;
    }
}
