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


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'
                }
                var Jto = require('react-native').NativeModules.NativeBridge;
                Jto.checkSystemLocationService().then(enableLocation => {
                    clientInfo({
                        perm: {
                            android: {
                                camera: statuses[PERMISSIONS.ANDROID.CAMERA],
                                location: {
                                    location_services_enabled: enableLocation,
                                    authorization_status: statuses[PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION]
                                },
                                notification: {
                                    authorization_status: global.notification
                                }
                            }

                        }
                    })
                })


            })
        });
        return
    }
    checkMultiple([PERMISSIONS.IOS.CAMERA,
    PERMISSIONS.IOS.PHOTO_LIBRARY,
    PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
    PERMISSIONS.IOS.LOCATION_ALWAYS]).then(statuses => {
        //         getLocalPush()

        var Jto = require('react-native').NativeModules.NativeBridge;
        Jto.getLocationAuthStatus((res) => {
            clientInfo({
                perm: {
                    ios: {
                        album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
                        camera: statuses[PERMISSIONS.IOS.CAMERA],
                        location: res,
                        notification: {
                            authorization_status: global.notification
                        }
                    }
                }
            })
        })


    })
}

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 (kIsIOS) {
            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 (kIsIOS) {
            Linking.openURL('app-settings:notifications')
        }
        else {
            Linking.openSettings()
        }
    }
}