package com.bpa.scalage.pagefragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.bpa.scalage.HBL.Dialog.AlertDialogButton;
import com.bpa.scalage.HBL.Dialog.DialogManager;
import com.bpa.scalage.HBL.Update.AfterPermissionGranted;
import com.bpa.scalage.HBL.Update.IConstants;
import com.bpa.scalage.HBL.Update.UpdateAppUtils;
import com.bpa.scalage.R;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * 其他页面
 */
public class OverPageFragment extends Fragment {

    Button btn_checkUpdate;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.over_page, container, false);
        btn_checkUpdate = view.findViewById(R.id.btn_checkUpdate);
        return view;
    }

    private void CheckUpdate(){

    }

    @AfterPermissionGranted(IConstants.UPDATE_APP)
    public void checkPermission() {
        // 检查文件读写权限
        String[] params = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(getContext(), params)) {

            //Android 8.0后，安装应用需要检查打开未知来源应用权限
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                checkInstallPermission();
            } else {
                UpdateAppUtils.updateApp(getContext());
            }
        } else {
            //未获取权限
            EasyPermissions.requestPermissions(this, "更新版本需要读写本地权限！", IConstants.UPDATE_APP, params);
        }
    }

    /**
     * Android 8.0后，安装应用需要检查打开未知来源应用权限
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void checkInstallPermission() {
        // 判断是否已打开未知来源应用权限
        boolean haveInstallPermission = getContext().getPackageManager().canRequestPackageInstalls();

        if (haveInstallPermission) {
            //已经打开权限，直接启动版本更新
            UpdateAppUtils.updateApp(getContext());
        } else {
            DialogManager.showWarn(getContext(),"为了正常升级APP，请点击设置-高级设置-允许安装未知来源应用，本功能只限用于APP版本升级，\r\n请问是否继续？", AlertDialogButton.YesNo,(s)->{
                if(s){
                    // 跳转到系统打开未知来源应用权限，在onActivityResult中启动更新
                    toInstallPermissionSettingIntent(getContext());
                }
            });
        }
    }

    /**
     * 开启安装未知来源权限
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void toInstallPermissionSettingIntent(Context context) {
        Uri packageURI = Uri.parse("package:" + context.getPackageName());
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
        startActivityForResult(intent, IConstants.INSTALL_PERMISSION_CODE);
    }
}