import React from 'react';
import { SafeAreaView, Text, Image, Alert, Linking, ImageBackground, View } from 'react-native';
import { images } from '../../assets/images/imageIndex';
import { colors } from '../../assets/styles/colors-theme';
import { inject, observer } from "mobx-react"
import { observable, makeObservable } from 'mobx'
import I18n from '../../services/i18n'
import AsyncStorage from '@react-native-community/async-storage';
import { width } from '../../services/screen';

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');
        this.store.splash = true
      }, 100),
    );
  };



  async init() {
    const version = await this.store.getAppVersion()
    console.log(version)
    try {
      const value = await AsyncStorage.getItem('lang');
      if (value !== null) {
        // this.store.changeLanguage(value);
      } else {
      }
    } catch (e) {
      // error reading value
    }
    this.store.notificationInit();
    I18n.locale = 'zh_hk'
    this.store.getNews()
    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 (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: colors.bgc,
        }}>
        <Image source={images.topbgc} resizeMode='contain' style={{ position: 'absolute', top: -30, width: width }} />
        <Image source={images.titleBarLogo} />
        <Image source={images.logo} style={{ marginTop: 20 }} />

      </View>
    );
  }
}))
export default SplashScreen
