/* 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 catTypes: {[key: number]: {cat: string}} = {
    1: {cat: "temporary"},
    2: {cat: "permanent"},
    3: {cat: "difficult access"},
    4: {cat: "accessible"},
    5: {cat: "unpleasant"},
    6: {cat: "not in list"}
} // works for now, but better solution would be to take categories from lmTypes and remove duplicates

export const lmTypes: {[key: number]: {image: ImageRequireSource, label: string, cat: string}} = {
    1: {image: require('../../assets/pothole.png'), label: "pothole", cat: "temporary"}, //done
    2: {image: require('../../assets/stairs.png'), label: "stairs", cat: "permanent"}, //done
    3: {image: require('../../assets/barrier.png'), label: "small construction", cat: "temporary"}, //done
    4: {image: require('../../assets/uneven.png'), label: "rough terrain", cat: "difficult access"}, //done
    5: {image: require('../../assets/information.png'), label: "information kiosk", cat: "accessible"}, //done
    6: {image: require('../../assets/washroom.png'), label: "accessible washroom", cat: "accessible"}, //done
    7: {image: require('../../assets/power.png'), label: "out-of-service", cat: "temporary"}, //done
    8: {image: require('../../assets/crosswalk.png'), label: "crosswalk issue", cat: "permanent"}, //done
    9: {image: require('../../assets/ice.png'), label: "ice", cat: "temporary"}, // done
    10: {image: require('../../assets/barrier.png'), label: "large construction", cat: "temporary"}, //done, check icon
    11: {image: require('../../assets/tripping.png'), label: "tripping hazard", cat: "temporary"}, //done
    12: {image: require('../../assets/stairs.png'), label: "non-power door", cat: "permanent"}, //done
    13: {image: require('../../assets/tripping.png'), label: "tripping hazard", cat: "difficult access"}, //done
    14: {image: require('../../assets/ramp.png'), label: "ramp", cat: "accessible"}, //done
    15: {image: require('../../assets/bump.png'), label: "steep incline", cat: "difficult access"}, //done
    16: {image: require('../../assets/childfriendly.png'), label: "child friendly area", cat: "accessible"}, //done
    17: {image: require('../../assets/uneven.png'), label: "narrow path", cat: "difficult access"}, //done
    18: {image: require('../../assets/barrier.png'), label: "height issue", cat: "difficult access"}, //done
    19: {image: require('../../assets/bump.png'), label: "signage issue", cat: "difficult access"}, //done
    20: {image: require('../../assets/information.png'), label: 'mobility aid provided', cat: "accessible"}, //done
    21: {image: require('../../assets/accessibleEntrance.png'), label: 'accessible entrance', cat: "accessible"}, //done
    22: {image: require('../../assets/misc.png'), label: 'not in list', cat: "not in list"}, //done
    23: {image: require('../../assets/elevator.png'), label: 'elevator', cat: "accessible"}, //done
    24: {image: require('../../assets/accessibleEntrance.png'), label: 'power door', cat: "accessible"}, //done
    25: {image: require('../../assets/desk.png'), label: 'accessible desk', cat: "accessible"}, //done
    26: {image: require('../../assets/ramp.png'), label: 'curb cut', cat: "accessible"}, //done
    27: {image: require('../../assets/water.png'), label: 'water fountain', cat: "accessible"}, //done
    28: {image: require('../../assets/loudnoise.png'), label: 'loud', cat: "unpleasant"}, //done
    29: {image: require('../../assets/garbage.png'), label: 'stinky', cat: "unpleasant"}, //done
}

export const lmTypesIndoor: {[key: number]: {image: ImageRequireSource, label: string, cat: string}} = {
    1: {image: require('../../assets/pothole.png'), label: "pothole", cat: "temporary"}, //done
    2: {image: require('../../assets/stairs.png'), label: "stairs", cat: "permanent"}, //done
    3: {image: require('../../assets/barrier.png'), label: "small construction", cat: "temporary"}, //done
    4: {image: require('../../assets/uneven.png'), label: "rough terrain", cat: "difficult access"}, //done
    5: {image: require('../../assets/information.png'), label: "information kiosk", cat: "accessible"}, //done
    6: {image: require('../../assets/washroom.png'), label: "accessible washroom", cat: "accessible"}, //done
    7: {image: require('../../assets/power.png'), label: "out-of-service", cat: "temporary"}, //done
    8: {image: require('../../assets/crosswalk.png'), label: "crosswalk issue", cat: "permanent"}, //done
    9: {image: require('../../assets/ice.png'), label: "ice", cat: "temporary"}, // done
    10: {image: require('../../assets/barrier.png'), label: "large construction", cat: "temporary"}, //done, check icon
    11: {image: require('../../assets/tripping.png'), label: "tripping hazard", cat: "temporary"}, //done
    12: {image: require('../../assets/stairs.png'), label: "non-power door", cat: "permanent"}, //done
    13: {image: require('../../assets/tripping.png'), label: "tripping hazard", cat: "difficult access"}, //done
    14: {image: require('../../assets/ramp.png'), label: "ramp", cat: "accessible"}, //done
    15: {image: require('../../assets/bump.png'), label: "steep incline", cat: "difficult access"}, //done
    16: {image: require('../../assets/childfriendly.png'), label: "child friendly area", cat: "accessible"}, //done
    17: {image: require('../../assets/uneven.png'), label: "narrow path", cat: "difficult access"}, //done
    18: {image: require('../../assets/barrier.png'), label: "height issue", cat: "difficult access"}, //done
    19: {image: require('../../assets/bump.png'), label: "signage issue", cat: "difficult access"}, //done
    20: {image: require('../../assets/information.png'), label: 'mobility aid provided', cat: "accessible"}, //done
    21: {image: require('../../assets/accessibleEntrance.png'), label: 'accessible entrance', cat: "accessible"}, //done
    22: {image: require('../../assets/misc.png'), label: 'not in list', cat: "not in list"}, //done
    23: {image: require('../../assets/elevator.png'), label: 'elevator', cat: "accessible"}, //done
    24: {image: require('../../assets/accessibleEntrance.png'), label: 'power door', cat: "accessible"}, //done
    25: {image: require('../../assets/desk.png'), label: 'accessible desk', cat: "accessible"}, //done
    26: {image: require('../../assets/ramp.png'), label: 'curb cut', cat: "accessible"}, //done
    27: {image: require('../../assets/water.png'), label: 'water fountain', cat: "accessible"}, //done
    28: {image: require('../../assets/loudnoise.png'), label: 'loud', cat: "unpleasant"}, //done
    29: {image: require('../../assets/garbage.png'), label: 'stinky', cat: "unpleasant"}, //done
}

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)
    }
}