package com.navinfo.outdoor.base;

import android.app.Activity;
import android.app.Application;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.navinfo.outdoor.R;
import com.navinfo.outdoor.api.UserApplication;
import com.gyf.immersionbar.ImmersionBar;
import com.navinfo.outdoor.util.NetWorkUtils;

import java.util.Timer;
import java.util.TimerTask;


public abstract class BaseActivity extends AppCompatActivity {

    private AlertDialog alertDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setScreenRoate(true);
        //初始化，默认透明状态栏和黑色导航栏
        ImmersionBar.with(this).init();
        Collector.addActivity(this);
        setContentView(getLayout());
        initMVP();
        initView();
        initData();
        initListener();
    }
    private static void setCustomDensity(Application application, Activity activity) {
        final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
        // 以设计DP为360为例，获得目标Density
        final float targetDensity = (float) (appDisplayMetrics.widthPixels / 360.0);
        final float targetScaledDensity = targetDensity * (appDisplayMetrics.scaledDensity / appDisplayMetrics.density);
        final int targetDensityDpi = (int)(targetDensity * 160);
        //设置application的Density
        appDisplayMetrics.density = targetDensity;
        appDisplayMetrics.scaledDensity = targetScaledDensity;
        appDisplayMetrics.densityDpi = targetDensityDpi;
        //设置activity的Density
        final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
        activityDisplayMetrics.density = targetDensity;
        activityDisplayMetrics.scaledDensity = targetScaledDensity;
        activityDisplayMetrics.densityDpi = targetDensityDpi;
    }

    protected void toast(String mag){
        Toast.makeText(UserApplication.getUserApplication(), mag, Toast.LENGTH_SHORT).show();
    }

    protected void initMVP() {
    }
    public void showLoadingDialog() {
        if (NetWorkUtils.iConnected(this)) { // 当前网络可用
            alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
            alertDialog.setCancelable(false);
            alertDialog.setOnKeyListener((dialog, keyCode, event) -> {
                if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_BACK)
                    return true;
                return false;

            });
            //loading样式
            View view = LayoutInflater.from(this).inflate(R.layout.loading, null);
            alertDialog.setView(view);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
           // initTimer();
        } else {
            Toast.makeText(this, "网络不可用", Toast.LENGTH_SHORT).show();
        }
    }
    public void showFileLoadingDialog() {
        if (NetWorkUtils.iConnected(this)) { // 当前网络可用
            alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
            alertDialog.setCancelable(false);
            alertDialog.setOnKeyListener((dialog, keyCode, event) -> {
                if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_BACK)
                    return true;
                return false;

            });
            //loading样式
            View view = LayoutInflater.from(this).inflate(R.layout.loading, null);
            alertDialog.setView(view);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        } else {
            Toast.makeText(this, "网络不可用", Toast.LENGTH_SHORT).show();
        }
    }

    private void initTimer() {
        Timer timer = new Timer(true);
        TimerTask timerTask = new TimerTask() {
            int countTime = 20;

            @Override
            public void run() {
                if (countTime > 0) {
                    countTime--;
                }
                if (countTime == 1) {
                    dismissLoadingDialog();
                }

            }
        };
        timer.schedule(timerTask, 1000, 1000);
    }

    public void setLoadingDialogText(String s) {
        //给loading 添加文字
        TextView view = alertDialog.findViewById(R.id.progressBar_tx);
        view.setText(s);
    }

    public void dismissLoadingDialog() {
        if (null != alertDialog && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }
    protected void initListener() {

    }

    protected abstract int getLayout();

    protected void initView() {

    }

    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Collector.removeActivity(this);
    }
    /**
     * 设置屏幕横竖屏切换
     * @param screenRoate true  竖屏     false  横屏
     */
    private void setScreenRoate(Boolean screenRoate) {
        if (screenRoate) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏模式
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }


}
