package com.tenlionsoft.baselib.constant;

import android.Manifest;
import android.Manifest.permission;
import android.annotation.SuppressLint;
import android.content.Context;

import com.tenlionsoft.baselib.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.StringDef;


@SuppressLint("InlinedApi")
public final class PermissionConstants {

    public static final String CALENDAR = Manifest.permission_group.CALENDAR;
    public static final String CAMERA = Manifest.permission_group.CAMERA;
    public static final String CONTACTS = Manifest.permission_group.CONTACTS;
    public static final String LOCATION = Manifest.permission_group.LOCATION;
    public static final String MICROPHONE = Manifest.permission_group.MICROPHONE;
    public static final String PHONE = Manifest.permission_group.PHONE;
    public static final String SENSORS = Manifest.permission_group.SENSORS;
    public static final String SMS = Manifest.permission_group.SMS;
    public static final String STORAGE = Manifest.permission_group.STORAGE;
    public static final String ALERT = Manifest.permission.SYSTEM_ALERT_WINDOW;
    private static final String[] GROUP_CALENDAR = {
        permission.READ_CALENDAR, permission.WRITE_CALENDAR
    };
    private static final String[] GROUP_CAMERA = {
        permission.CAMERA
    };
    private static final String[] GROUP_CONTACTS = {
        permission.READ_CONTACTS, permission.WRITE_CONTACTS, permission.GET_ACCOUNTS
    };
    private static final String[] GROUP_LOCATION = {
        permission.ACCESS_FINE_LOCATION,
        permission.ACCESS_COARSE_LOCATION,
        permission.ACCESS_WIFI_STATE,
        permission.ACCESS_NETWORK_STATE,
        permission.CHANGE_WIFI_STATE,
        permission.ACCESS_COARSE_LOCATION
    };
    private static final String[] GROUP_MICROPHONE = {
        permission.RECORD_AUDIO
    };
    private static final String[] GROUP_PHONE = {
        permission.READ_PHONE_STATE, permission.READ_PHONE_NUMBERS, permission.CALL_PHONE,
        permission.ANSWER_PHONE_CALLS, permission.READ_CALL_LOG, permission.WRITE_CALL_LOG,
        permission.ADD_VOICEMAIL, permission.USE_SIP, permission.PROCESS_OUTGOING_CALLS
    };
    private static final String[] GROUP_SENSORS = {
        permission.BODY_SENSORS
    };
    private static final String[] GROUP_SMS = {
        permission.SEND_SMS, permission.RECEIVE_SMS, permission.READ_SMS,
        permission.RECEIVE_WAP_PUSH, permission.RECEIVE_MMS,
    };
    private static final String[] GROUP_STORAGE = {
        permission.READ_EXTERNAL_STORAGE, permission.WRITE_EXTERNAL_STORAGE
    };

    @StringDef({CALENDAR, CAMERA, CONTACTS, LOCATION, MICROPHONE, PHONE, SENSORS, SMS, STORAGE, ALERT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Permission {
    }

    public static String[] getPermissions(@Permission final String permission) {
        switch (permission) {
            case CALENDAR:
                return GROUP_CALENDAR;
            case CAMERA:
                return GROUP_CAMERA;
            case CONTACTS:
                return GROUP_CONTACTS;
            case LOCATION:
                return GROUP_LOCATION;
            case MICROPHONE:
                return GROUP_MICROPHONE;
            case PHONE:
                return GROUP_PHONE;
            case SENSORS:
                return GROUP_SENSORS;
            case SMS:
                return GROUP_SMS;
            case STORAGE:
                return GROUP_STORAGE;
        }
        return new String[]{permission};
    }

    public static final int REQUEST_CODE_STORAGE = 101;
    public static final int REQUEST_CODE_RECORD_AUDIO = 102;
    public static final int REQUEST_CODE_CAMERA = 103;
    public static final int REQUEST_CODE_PHONE_STATE = 104;
    public static final int REQUEST_CODE_MULTI = 107;
    public static final String WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    public static final String READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String MANAGE_EXTERNAL_STORAGE = permission.MANAGE_EXTERNAL_STORAGE;
    public static final String RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;


    @SuppressWarnings("unused")
    public static List<String> getPermissionRes(Context context, String[] permissions) {
        List<String> res = new ArrayList<>();
        for (String permission : permissions) {
            switch (permission) {
                case WRITE_EXTERNAL_STORAGE:
                case READ_EXTERNAL_STORAGE:
                case MANAGE_EXTERNAL_STORAGE:
                    String storage = context.getString(R.string.cloudLink_permission_storage);
                    if (res.indexOf(storage) < 0) {
                        res.add(storage);
                    }
                    break;
                case RECORD_AUDIO:
                    res.add(context.getString(R.string.cloudLink_permission_recordAudio));
                    break;
                case CAMERA:
                    res.add(context.getString(R.string.cloudLink_permission_camera));
                    break;
                case READ_PHONE_STATE:
                    res.add(context.getString(R.string.cloudLink_permission_phoneState));
                    break;
            }
        }
        return res;
    }

    public static String getPermissionRes(Context context, String permission) {
        switch (permission) {
            case WRITE_EXTERNAL_STORAGE:
            case READ_EXTERNAL_STORAGE:
            case MANAGE_EXTERNAL_STORAGE:
                return context.getString(R.string.cloudLink_permission_storage);
            case RECORD_AUDIO:
                return context.getString(R.string.cloudLink_permission_recordAudio);
            case CAMERA:
                return context.getString(R.string.cloudLink_permission_camera);
            case READ_PHONE_STATE:
                return context.getString(R.string.cloudLink_permission_phoneState);
            default:
                return "";
        }
    }
}
