import React, { PureComponent, } from 'react'
import { NavigationContainer } from '@react-navigation/native'
import * as RootNavigation from './src/navigations/RootNavigation'
import { navigationRef } from './src/navigations/RootNavigation'
import analytics from '@react-native-firebase/analytics'
import { checkMultiple, requestMultiple, requestNotifications, PERMISSIONS } from 'react-native-permissions'
import { ModalPortal } from 'react-native-modals'
import { Provider } from 'react-redux'
import { store } from './src/redux/store'
import GlobalModals from './src/components/globalModals/GlobalModals'
import t, { languageConfig, } from './src/translator/translator'

export let appStartInstance: AppStart = null

export default class AppStart extends PureComponent
{
    navigationContainer: NavigationContainer = {  }

    routeName: String = ''
    defaultCoreUri = null

    constructor(props)
    {
        super(props)
        this.initialMethodsForMainView = this.initialMethodsForMainView.bind(this)

        this.state = {
            content: null,
        }

        appStartInstance = this
    }


    async componentDidMount()
    {
        await this.initialMethodsForMainView()
    }

    async initialMethodsForMainView()
    {
        await languageConfig.init()

        await this.askAllPermissions()

        // if(__DEV__)
        // {
            // globalData.setCoreIdentityApiUri('https://identity-test.novbem.az')
            // globalData.setCoreApiUri('https://business-api-test.novbem.az')
        // }
        // else
        // {
            // globalData.setCoreIdentityApiUri('https://identity.novbem.az')
            // globalData.setCoreApiUri('https://business-api.novbem.az/v2.2.0')
        // }    

        // let isAuthorized = await storageService.getIsAuthorized()

        // if(isAuthorized)
            this.mainNavInit()
        // else
        //     this.loginNavInit()
    }

    async askAllPermissions()
    {
        requestMultiple([
            PERMISSIONS.ANDROID.CAMERA,
            PERMISSIONS.ANDROID.RECORD_AUDIO,
            PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE,
            PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE,

            PERMISSIONS.IOS.CAMERA,
            PERMISSIONS.IOS.MICROPHONE,
        ])
        // .then(statuses => console.log('--- request statutes', statuses))

        requestNotifications([
            'alert',
            'badge',
            'carPlay',
            'criticalAlert',
            'provisional',
            'sound',
        ]).then(obj => console.log('--- obj', obj))
    }

    async loginNavInit()
    {
        this.setState({
            content: (await import('./src/navigations/LoginNavigation')).default,
        })
    }

    async mainNavInit()
    {
        // let currAuth = await storageService.getCurrentAuth()
        // globalData.setCurrentAuth(currAuth)
        // console.log('---- current auth', currAuth)

        // let currUser = await storageService.getCurrentUser()
        // globalData.setCurrentUser(currUser)


        this.setState({
            content: (await import('./src/navigations/MainNavigation')).default,
            // content: (await im/port('./src/navigations/LoginNavigation')).default,
        })
    }

    updateAllComponent()
    {
        this.forceUpdate()
        navigationRef?.current?.setParams()
    }



    render()
    {
        if(!this.state.content)
            return null

        return (
            <Provider store={ store }>

                <NavigationContainer
                    ref = { ref => { navigationRef; this.navigationContainer = ref } }
                    onReady = { () => {
                        if(this.navigationContainer)
                        this.routeName = this.navigationContainer.getCurrentRoute().name;
                    }}
                    onStateChange = { async () => {
                        const previousRouteName = this.routeName;
                        const currentRouteName = this.navigationContainer.getCurrentRoute().name;
                
                        if (previousRouteName !== currentRouteName)
                            await analytics().logScreenView({
                                screen_name: currentRouteName,
                                screen_class: currentRouteName,
                            })
                
                        this.routeName = currentRouteName;
                    }}
                >

                    <this.state.content/>
                </NavigationContainer>

                <ModalPortal/>
                <GlobalModals/>
            </Provider>
        )
    }


    async onLogined()
    {
        this.setState({
            content: (await import('./src/navigations/MainNavigation')).default,
        })
        this.notificationConfig()
        // this.initialMethodsForMainView()
    }

    async onLogOut()
    {
        // this.initialMethodsForMainView()
        // this.setState({
        //     content: (await import('./src/navigations/LoginNavigation')).default,
        // })
    }

}
