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

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

export const lmTypes: {[key: number]: {image: ImageURISource, label: string}} = {
    1: {image: require('../../assets/uneven.png'), label: "rough terrain"},
    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"}
} 

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

export const checkGivenPermissions = 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('An error occured when getting 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);
        console.log(permissionsResult)
        console.log(ungrantedPermissions)
        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('An error occured when getting permissions: ' + error)
    }
}

export const getLocationPermissions = async () => {
    try {
        console.log('App state changed, getting permissions')
        let permissions = []
        if (Platform.OS == "android") {  
        permissions = [
            PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
            PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
            PERMISSIONS.ANDROID.RECORD_AUDIO,
        ]
        }
    
        else if (Platform.OS == 'ios') {
        permissions = [
            PERMISSIONS.IOS.LOCATION_ALWAYS,
            PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
            PERMISSIONS.IOS.SPEECH_RECOGNITION,
            PERMISSIONS.IOS.MICROPHONE,
        ]
        }
        
        try {
        const permissionsResult = await checkMultiple(permissions)
        const ungrantedPermissions = permissions.filter(permission => permissionsResult[permission] != RESULTS.GRANTED);
        requestMultiple(ungrantedPermissions)
        }
        catch (error) { 
        console.log('An error occured when getting permissions: ' + error)
        }
    }
    catch (error) {
        console.log('An error occured when getting permissions: ' + error)
    }
}