package com.app.duck;


import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.LayoutInflater;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.app.duck.util.Service2;
import com.app.duck.util.HxUtils;
import com.app.duck.util.PermissionsUtils;
import com.app.http.Http;
import com.romellfudi.ussdlibrary.USSDController;

import org.xutils.x;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * author: hx
 * created on: 2023/10/16 18:00
 * description:
 */
public abstract class BaseActivity<B extends ViewBinding> extends AppCompatActivity {
    protected B b;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Http.clean();
        setLayout();
        x.view().inject(this);
        KeyboardUtils.fixAndroidBug5497(this);
        USSDController.verifyAccesibilityAccess(this);
        USSDController.verifyOverLay(this);
        if (keepScreenOn()) {
            PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (powerManager.isScreenOn()) {
                // 如果当前屏幕亮着，启用常亮模式
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
        }
        initView();
        initData();
    }


    @Override
    public void onBackPressed() {
        Http.clean();
        super.onBackPressed();
    }

    public void setLayout() {
        try {
            Type superclass = getClass().getGenericSuperclass();
            Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            b = (B) method.invoke(null, getLayoutInflater());
            setContentView(b.getRoot());
        } catch (Exception e) {
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        PermissionsUtils.checkPermission(isPermission(), isOtherPermission());

    }

    public boolean isPermission() {
        return true;
    }

    public boolean isOtherPermission() {
        return true;
    }


    protected void initData() {
    }


    public abstract void initView();

    public BaseActivity context() {
        return BaseActivity.this;
    }

    public boolean keepScreenOn() {
        return true;
    }

    public void startForegroundService() {
        Intent intent = new Intent(this, Service2.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intent);
        } else {
            startService(intent);
        }
    }

    public void checkForUpdate() {
        HxUtils.checkForUpdate(this, false);
    }


    public String getPhone() {
        if (StringUtils.isEmpty(HxUtils.getPhone())) {
            return StringUtils.getString(R.string.click_here_to_set_the_number);
        }
        return HxUtils.getPhone();
    }


}
