import React from 'react';
import {SafeAreaView, Text, Image, Alert, Linking} from 'react-native';
import {images} from '../../assets/images/imageIndex';
import messaging from '@react-native-firebase/messaging';
import {colors} from '../../assets/styles/colors-theme';
import { inject, observer } from "mobx-react"
import { observable, makeObservable } from 'mobx'
import AppVersion from '../../../appVersion';
import AsyncStorage from '@react-native-community/async-storage';

const SplashScreen = inject("appStore")(observer(class SplashScreen extends React.Component {

  constructor(props) {
    super(props);
    this.store = props.appStore;
  }
  async componentDidMount() {
    // await this.performTimeConsumingTask();

    this.init();

    //this.init();
    // this.props.navigation.navigate('BottomTabScreen');
  }

  performTimeConsumingTask = async () => {
    return new Promise(resolve =>
      setTimeout(() => {
        resolve('result');
      }, 1500),
    );
  };

  async requestUserPermission() {
    const authStatus = await messaging().requestPermission();
    const enabled =
      authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authStatus === messaging.AuthorizationStatus.PROVISIONAL;

    if (enabled) {
      console.log('Authorization status:', authStatus);
    }
  }

  async init() {
    try {
      const value = await AsyncStorage.getItem('lang');
      if (value !== null) {
        this.store.changeLanguage(value);
      } else {
      }
    } catch (e) {
      // error reading value
    }
    this.store.notificationInit();
    var data = await this.store.getLocalData('logindata');
    var searchHistoryData = await this.store.getLocalData('searchHistory');
    if (searchHistoryData != null) {
      this.store.searchHistory = searchHistoryData;
      console.log(this.store.searchHistoryData);
    } else {
      console.log('null');
    }
    // this.store.removeValue('searchHistory');
    if (data != null) {
      var userloginData = {
        token: data.token,
        device: data.device,
      };
      // this.store.userData = userloginData;
      console.log(data);
      this.store.token = data.token;
      this.store.uuid = data.device;
      await this.store.getUserData(this, userloginData, false);
    }
    await this.store.getCategories();
  }
  render() {
    return (
      <SafeAreaView
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: colors.mainColor,
        }}>
        <Image source={images.signIn.illustration} />
      </SafeAreaView>
    );
  }
}))
export default SplashScreen
