import { getLocalPush } from '@/features/trackTimeDuration/actions/TrackTimeActions';
import { clientInfo } from '@/services/common';
import Taro from '@tarojs/taro';
import JPush from 'jpush-react-native';
import { Alert, Linking } from 'react-native';
import { check, PERMISSIONS, RESULTS, checkMultiple } from 'react-native-permissions';
// import {OneSignal} from 'react-native-onesignal';

export const checkLocation = () => {

    // JPush.isNotificationEnabled((result) => {
    //     Taro.showModal({
    //         title: '提示',
    //         content: JSON.stringify(result)
    //     })
    // });

    check(PERMISSIONS.IOS.LOCATION_ALWAYS)
        .then((result) => {
            switch (result) {
                case RESULTS.UNAVAILABLE:
                    // Alert.alert('This feature is not available (on this device / in this context)');
                    break;
                case RESULTS.DENIED:
                    console.log('The permission has not been requested / is denied but requestable');
                    break;
                case RESULTS.LIMITED:
                    console.log('The permission is limited: some actions are possible');
                    break;
                case RESULTS.GRANTED:
                    console.log('The permission is granted');
                    break;
                case RESULTS.BLOCKED:
                    // console.log('The permission is denied and not requestable anymore');
                    break;
            }
        })
        .catch((error) => {
            // …
        });
}


export const uploadPermissions = () => {
    if (Taro.getSystemInfoSync().platform == 'android') {
        checkMultiple([PERMISSIONS.ANDROID.CAMERA,
        PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
        PERMISSIONS.ANDROID.POST_NOTIFICATIONS]).then(statuses => {
            const OneSignal = require('react-native-onesignal').OneSignal
            OneSignal.initialize("2bbf5339-7ab8-4c5c-9a66-c3a8eaea188e");
            OneSignal.Notifications.getPermissionAsync().then((res) => {
                if (res) {
                    global.notification = 'authorized'
                }
                else {
                    global.notification = 'denied'
                }

                clientInfo({
                    perm: {
                        camera: statuses[PERMISSIONS.ANDROID.CAMERA],
                        location: statuses[PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION],
                        notification: global.notification
                    }
                })
            })
        });
        return
    }
    checkMultiple([PERMISSIONS.IOS.CAMERA,
    PERMISSIONS.IOS.PHOTO_LIBRARY,
    PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
    PERMISSIONS.IOS.LOCATION_ALWAYS]).then(statuses => {
        if (global.notification == 'not_determined') {
            clientInfo({
                perm: {
                    album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
                    camera: statuses[PERMISSIONS.IOS.CAMERA],
                    location_always: statuses[PERMISSIONS.IOS.LOCATION_ALWAYS],
                    location_when_in_use: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
                    notification: 'not_determined'
                }
            })
        }
        else {
            JPush.isNotificationEnabled((res) => {
                console.log(res)
                if (res) {
                    getLocalPush()
                    clientInfo({
                        perm: {
                            album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
                            camera: statuses[PERMISSIONS.IOS.CAMERA],
                            location_always: statuses[PERMISSIONS.IOS.LOCATION_ALWAYS],
                            location_when_in_use: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
                            notification: 'authorized'
                        }
                    })
                }
                else {
                    clientInfo({
                        perm: {
                            album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
                            camera: statuses[PERMISSIONS.IOS.CAMERA],
                            location_always: statuses[PERMISSIONS.IOS.LOCATION_ALWAYS],
                            location_when_in_use: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
                            notification: 'denied'
                        }
                    })
                }
            })
        }

    })
}

export const checkNotification = () => {
    if (Taro.getSystemInfoSync().platform == 'android') {
        const OneSignal = require('react-native-onesignal').OneSignal
        OneSignal.Notifications.canRequestPermission().then((res) => {
            if (res) {
                OneSignal.Notifications.requestPermission(true);
            }
            else {
                Linking.openSettings()
            }
        })
        return;
    }



    if (global.notification == 'not_determined') {
        if (Taro.getSystemInfoSync().platform == 'ios') {
            var Jto = require('react-native').NativeModules.NativeBridge;
            Jto.authNotification()
        }
        else {
            // Linking.openSettings()
            const OneSignal = require('react-native-onesignal').OneSignal
            OneSignal.Notifications.requestPermission(true).then((response) => {
                if (response) {
                    global.notification = 'authorized'
                    uploadPermissions()
                } else {
                    global.notification = 'denied'
                    uploadPermissions()
                }
            });
        }
        // const test = require('@/utils/push').default
        // test()
    }
    else {
        if (Taro.getSystemInfoSync().platform == 'ios') {
            Linking.openURL('app-settings:notifications')
        }
        else {
            Linking.openSettings()
        }
    }
}