/* Copyright (C) Click & Push Accessibility, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written and maintained by the Click & Push Development team 
 * <dev@clicknpush.ca>, January 2022
 */

import { Alert, ImageRequireSource, ImageURISource, Permission, Platform, StyleSheet, View } from "react-native";
import { checkMultiple, PERMISSIONS, requestMultiple, RESULTS, } from "react-native-permissions";

// global constants
export const colors = {
    red: '#df3f3f',
}

export const SECURESTORE_ACCESSTOKEN = "access"
export const SECURESTORE_REFRESHTOKEN = "refresh"
export const SECURESTORE_NOTIFTOKEN = 'notif'
export const SECURESTORE_IDTOKEN = 'id'

export const lmTypes: {[key: number]: {image: ImageRequireSource, label: string}} = {
    // 1: {image: require('../../assets/uneven.png'), label: "rough terrain"}, not currently in use
    2: {image: require('../../assets/stairs.png'), label: "stairs"},
    3: {image: require('../../assets/barrier.png'), label: "barrier"},
    4: {image: require('../../assets/uneven.png'), label: "rough terrain"},
    5: {image: require('../../assets/information.png'), label: "information"},
    6: {image: require('../../assets/washroom.png'), label: "accessible washroom"},
    7: {image: require('../../assets/power.png'), label: "power issue"},
    8: {image: require('../../assets/crosswalk.png'), label: "crosswalk issue"},
    9: {image: require('../../assets/ice.png'), label: "ice"},
    14: {image: require('../../assets/ramp.png'), label: "ramp"},
    16: {image: require('../../assets/childfriendly.png'), label: "child friendly area"},
    21: {image: require('../../assets/accessibleEntrance.png'), label: 'accessible entrance'},
    22: {image: require('../../assets/misc.png'), label: 'misc'}
}

export const lmTypesIndoor: {[key: number]: {image: ImageRequireSource, label: string}} = {
    2: {image: require('../../assets/stairs.png'), label: "stairs"},
    5: {image: require('../../assets/information.png'), label: "information"},
    6: {image: require('../../assets/washroom.png'), label: "accessible washroom"},
    10: {image: require('../../assets/desk.png'), label: "desk"},
    11: {image: require('../../assets/elevator.png'), label: "elevator"},
    12: {image: require('../../assets/kiosk.png'), label: "kiosk"},
    13: {image: require('../../assets/monitor.png'), label: "monitor"},
    14: {image: require('../../assets/ramp.png'), label: "ramp"},
    15: {image: require('../../assets/water.png'), label: "water fountain"},
    16: {image: require('../../assets/childfriendly.png'), label: "child friendly area"},
    17: {image: require('../../assets/garbage.png'), label: "garbage cans"},
    18: {image: require('../../assets/loudnoise.png'), label: "loud area"},
    19: {image: require('../../assets/tripping.png'), label: "tripping hazard"},
    20: {image: require('../../assets/locator.png'), label: 'locator'},
    22: {image: require('../../assets/misc.png'), label: 'misc'}
}

// 2: {image: require('../../assets/stairs.png'), label: "stairs"},
// 3: {image: require('../../assets/barrier.png'), label: "barrier"},
// 4: {image: require('../../assets/uneven.png'), label: "rough terrain"},
// 5: {image: require('../../assets/information.png'), label: "information"},
// 6: {image: require('../../assets/washroom.png'), label: "accessible washroom"},
// 7: {image: require('../../assets/power.png'), label: "power issue"},
// 8: {image: require('../../assets/crosswalk.png'), label: "crosswalk issue"},
// 9: {image: require('../../assets/ice.png'), label: "ice"},
// 10: {image: require('../../assets/desk.png'), label: "desk"},
// 11: {image: require('../../assets/elevator.png'), label: "elevator"},
// 12: {image: require('../../assets/kiosk.png'), label: "kiosk"},
// 13: {image: require('../../assets/monitor.png'), label: "monitor"},
// 14: {image: require('../../assets/ramp.png'), label: "ramp"},
// 15: {image: require('../../assets/water.png'), label: "water fountain"},
// 16: {image: require('../../assets/childfriendly.png'), label: "child friendly area"},


export const GlobalStyles = StyleSheet.create({
    itemRowContainer: {
        flexDirection: 'row', 
        justifyContent: 'space-between'
    },
})

export const checkVoicePermissions = async (permissions: string[]): Promise<boolean> => {
    try {
        if (Platform.OS == 'android') {
            permissions = [PERMISSIONS.ANDROID.RECORD_AUDIO]
        }
        else if (Platform.OS == 'ios') {
            permissions = [PERMISSIONS.IOS.SPEECH_RECOGNITION, PERMISSIONS.IOS.MICROPHONE]
        }

        const result = await checkMultiple(permissions as Permission[])
        let granted = false
        permissions.forEach(permission => {
            granted = result[permission] ==  RESULTS.GRANTED
        })

        return granted
    } catch (error) {
        console.log('[Permissions]: An error occured when getting voice permissions: ' + error)
    }
}

export const getMediaPermissions = async () => {
    let permissions = []
    if (Platform.OS == "android") {  
        permissions = [
            PERMISSIONS.ANDROID.CAMERA,
            PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE,
            PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE,
        ]
    }

    else if (Platform.OS == 'ios') {
        permissions = [
            PERMISSIONS.IOS.CAMERA,
            PERMISSIONS.IOS.PHOTO_LIBRARY,
        ]
    }
    
    try {
        const permissionsResult = await checkMultiple(permissions)
        const ungrantedPermissions = permissions.filter(permission => permissionsResult[permission] != RESULTS.GRANTED);
        if (ungrantedPermissions.length > 0) {
            const result = await requestMultiple(ungrantedPermissions)   
            const deniedPermissions = ungrantedPermissions.filter(permission => result[permission] != RESULTS.GRANTED)
            if (deniedPermissions.length > 0) {
                return false
            }
        }

        return true
    }
    catch (error) { 
        console.log('[Permissions]: An error occured when getting media permissions: ' + error)
    }
}