import {PermissionsAndroid,Platform} from 'react-native';
import { notify } from "./notify";
import Permissions, { PERMISSIONS, RESULTS } from 'react-native-permissions'

export async function CheckAndAskForPermission(Permm){
    if (Platform.OS === 'ios') {
        console.log('CheckAndAskForPermission ios');
        let IOS_hasPermission = await Permissions.check(Permm)
        if (IOS_hasPermission==RESULTS.GRANTED) return true;
        let IOS_status = await Permissions.request(Permm);
        console.log('IOS_status:',IOS_status);
        if (IOS_status === RESULTS.GRANTED) return true;
        if (IOS_status === RESULTS.DENIED) {
            notify('Location permission denied by user.');
        } else if (RESULTS === RESULTS.UNAVAILABLE) {
            notify('Location permission not available.');
        }
        return true;
    }
    else if (Platform.OS === 'android') {
        const hasPermission = await PermissionsAndroid.check(Permm);
        if (hasPermission) return true;
        const status = await PermissionsAndroid.request(Permm);
        if (status === PermissionsAndroid.RESULTS.GRANTED) return true;
        if (status === PermissionsAndroid.RESULTS.DENIED) {
            notify('Location permission denied by user.');
        } else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
            notify('Location permission revoked by user.');
        }
        return false;
    }

    return true;
}

export async function CheckAndAskForMulitplePermissions(Permms=[]){
    let hasPermission = await Permissions.checkMultiple(Permms)
    let k_IOSLoc='ios.permission.LOCATION_WHEN_IN_USE',k_IOSCamera='ios.permission.CAMERA',k_IOSStorage='ios.permission.PHOTO_LIBRARY',k_IOSMic=PERMISSIONS.IOS.MICROPHONE,k_IOSmedia=PERMISSIONS.IOS.MEDIA_LIBRARY;
    let k_Loc='android.permission.ACCESS_FINE_LOCATION',k_Camera='android.permission.CAMERA',k_Storage='android.permission.WRITE_EXTERNAL_STORAGE',k_StorageRead='android.permission.READ_EXTERNAL_STORAGE',k_Mic=PERMISSIONS.ANDROID.RECORD_AUDIO;
    if (Platform.OS === 'ios') {
        if (hasPermission[k_IOSLoc]==RESULTS.GRANTED && hasPermission[k_IOSCamera]==RESULTS.GRANTED && hasPermission[k_IOSStorage]==RESULTS.GRANTED && hasPermission[k_IOSMic]==RESULTS.GRANTED && hasPermission[k_IOSmedia]==RESULTS.GRANTED) return true;  
    }
    else{
        if (hasPermission[k_Loc]==RESULTS.GRANTED && hasPermission[k_Camera]==RESULTS.GRANTED && hasPermission[k_Storage]==RESULTS.GRANTED  &&  hasPermission[k_StorageRead]==RESULTS.GRANTED && hasPermission[k_Mic]==RESULTS.GRANTED) return true;  
    }
        
    let statuses = await Permissions.requestMultiple(Permms);
    let denies=[];
    let notavlbl=[];
    console.log(statuses);
    if (Platform.OS === 'ios') {
        if (statuses[k_IOSLoc]==RESULTS.GRANTED && statuses[k_IOSCamera]==RESULTS.GRANTED && statuses[k_IOSStorage]==RESULTS.GRANTED  && statuses[k_IOSMic]==RESULTS.GRANTED  && statuses[k_IOSmedia]==RESULTS.GRANTED) return true;  
        if (statuses[k_IOSLoc]=== RESULTS.DENIED) {
            denies.push('Location');
        } else if (statuses[k_IOSLoc] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Location');
        }

        if (statuses[k_IOSCamera]=== RESULTS.DENIED) {
            denies.push('Camera');
        } else if (statuses[k_IOSCamera] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Camera');
        }

        if (statuses[k_IOSStorage]=== RESULTS.DENIED) {
            denies.push('Photo Library');
        } else if (statuses[k_IOSStorage] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Photo Library');
        }

        if (statuses[k_IOSMic]=== RESULTS.DENIED) {
            denies.push('Microphone');
        } else if (statuses[k_IOSMic] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Microphone');
        }

        if (statuses[k_IOSmedia]=== RESULTS.DENIED) {
            denies.push('Media Library');
        } else if (statuses[k_IOSmedia] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Media Library');
        }
    }
    else{
        if (statuses[k_Loc]==RESULTS.GRANTED && statuses[k_Camera]==RESULTS.GRANTED && statuses[k_Storage]==RESULTS.GRANTED && statuses[k_StorageRead]==RESULTS.GRANTED && statuses[k_Mic]==RESULTS.GRANTED) return true;  
        if (statuses[k_Loc]=== RESULTS.DENIED) {
            denies.push('Location');
        } else if (statuses[k_Loc] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Location');
        }

        if (statuses[k_Camera]=== RESULTS.DENIED) {
            denies.push('Camera');
        } else if (statuses[k_Camera] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Camera');
        }

        if (statuses[k_StorageRead]=== RESULTS.DENIED) {
            denies.push('Read External Storage');
        } else if (statuses[k_StorageRead] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Read External Storage');
        }

        if (statuses[k_Storage]=== RESULTS.DENIED) {
            denies.push('Photo Library');
        } else if (statuses[k_Storage] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Photo Library');
        }

        if (statuses[k_Mic]=== RESULTS.DENIED) {
            denies.push('Record Audio');
        } else if (statuses[k_Mic] === RESULTS.UNAVAILABLE) {
            notavlbl.push('Record Audio');
        }
    }
       
    let msg='';
    if(denies.length>0){
        msg+='User denied '+denies.join(', ')+(denies.length>1?' permissions':' permission');
    }
    if(notavlbl.length>0){
        msg+=msg!=''?', ':'';
        msg+=notavlbl.join(', ')+(notavlbl.length>1?' permissions':' permission')+' not available';
    }

    if(denies.length>0 || notavlbl.length>0){
        notify(msg);
    }

    return true;
}

export const hasLocationPermission = async () => {
    if (Platform.OS === 'android' && Platform.Version < 21) {
        return true;
    }
    else if(Platform.OS === 'ios'){
        let status=await CheckAndAskForPermission(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
        return status;
    }
    else if(Platform.OS === 'android'){
        let status=await CheckAndAskForPermission(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
        return status;
    }
    return true;
}

export const hasCameraPermission = async () => {
    if (Platform.OS === 'android' && Platform.Version < 21) {
        return true;
    }
    else if(Platform.OS === 'ios'){
        let status=await CheckAndAskForPermission(PERMISSIONS.IOS.CAMERA);
        return status;
    }
    else if(Platform.OS === 'android'){
        let status=await CheckAndAskForPermission(PermissionsAndroid.PERMISSIONS.CAMERA);
        return status;
    }
    return true;
}

export const hasStoragePermission = async () => {
    if (Platform.OS === 'android' && Platform.Version < 21) {
        return true;
    }
    else if(Platform.OS === 'ios'){
        let status=await CheckAndAskForPermission(PERMISSIONS.IOS.PHOTO_LIBRARY);
        return status;
    }
    else if(Platform.OS === 'android'){
        let status=await CheckAndAskForPermission(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
        return status;
    }
    return true;
}

export const requestMultiplePerms = async () => {
    console.log('requestMultiplePerms');
    if (Platform.OS === 'android' && Platform.Version < 21) {
        return true;
    }
    else{
        let OSPer=Platform.OS === 'ios'?PERMISSIONS.IOS:PERMISSIONS.ANDROID;
        let IOSPERMS=[OSPer.LOCATION_WHEN_IN_USE,OSPer.CAMERA,OSPer.PHOTO_LIBRARY,OSPer.MICROPHONE,OSPer.MEDIA_LIBRARY];
        let ANDROIDPERMS=[OSPer.ACCESS_FINE_LOCATION,OSPer.CAMERA,OSPer.READ_EXTERNAL_STORAGE,OSPer.WRITE_EXTERNAL_STORAGE,OSPer.RECORD_AUDIO];
        let allperms=Platform.OS === 'ios'?IOSPERMS:ANDROIDPERMS;
        let status=await CheckAndAskForMulitplePermissions(allperms);
        return status;
    }
}

  

export const _checkLocationPermission = async () => {
    if(Platform.OS === 'android' && Platform.Version < 21){
        return true;
    }
    else if (Platform.OS === 'ios' ){
        let IOS_hasPermission = await Permissions.check(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
        let IOS_P=IOS_hasPermission==RESULTS.GRANTED?true:false;
        return IOS_P;
    }
  else if (Platform.OS === 'android') {
  const hasPermission = await PermissionsAndroid.check(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
  );

  if (hasPermission) return true;

  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
  );

  return granted === PermissionsAndroid.RESULTS.GRANTED;
}
    else{return true;}
}

export const _checkCameraPermission = async () => {
    if(Platform.OS === 'android' && Platform.Version < 21){
        return true;
    }
    else if (Platform.OS === 'ios' ){
        let IOS_hasPermission = await Permissions.check(PERMISSIONS.IOS.CAMERA);
        let IOS_P=IOS_hasPermission==RESULTS.GRANTED?true:false;
        return IOS_P;
    }
    else if(Platform.OS === 'android' ){
        const hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
        return hasPermission;    
    }
    else{return true;}
}

export const _checkStoragePermission = async () => {
    if(Platform.OS === 'android' && Platform.Version < 21){
        return true;
    }
    else if (Platform.OS === 'ios' ){
        let IOS_hasPermission = await Permissions.check(PERMISSIONS.IOS.PHOTO_LIBRARY);
        let IOS_P=IOS_hasPermission==RESULTS.GRANTED?true:false;
        return IOS_P;
    }
    else if(Platform.OS === 'android' ){
        const hasPermission = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
        return hasPermission;    
    }
    else{return true;}
}

export async function checkPermissions(){
    await hasLocationPermission();
    await hasCameraPermission();
    await hasStoragePermission();
}