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 AppVersion from '../../../appVersion';
import AsyncStorage from '@react-native-community/async-storage';
import { inject, observer } from "mobx-react"
import { observable, makeObservable } from 'mobx'
const SplashScreen = inject("appStore")(observer(class SplashScreen extends React.Component {

  constructor(props) {
    super(props);
    this.store = props.appStore;
    makeObservable(this, {
    })
  }
  async componentDidMount() {
    // await this.performTimeConsumingTask();
    // console.log(AppVersion.version);
    // if ((await this.store.checkVersion()) == AppVersion.version) {
    //   this.init();
    // } else {
    //   Alert.alert(
    //     '請更新',
    //     '好新鮮已有新版本，這到APP STORE更新',
    //     [
    //       {
    //         text: '更新',
    //         onPress: () => {
    //           Linking.canOpenURL('http://onelink.to/3nqphw').then(
    //             supported => {
    //               if (supported) {
    //                 Linking.openURL('http://onelink.to/3nqphw');
    //               } else {
    //                 console.log(
    //                   "Don't know how to open URI: " +
    //                     response.data.wechat.qr_code_url,
    //                 );
    //               }
    //             },
    //           );
    //         },
    //       },
    //     ],
    //     {cancelable: false},
    //   );
    // }
    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');
    }
 
    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);
    }
    this.store.init(false);
    //await this.store.getAllProductFromStart(this);
  }
  render() {
    return (
      <SafeAreaView
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: colors.mainColor,
        }}>
        <Image source={images.signIn.illustration} />
      </SafeAreaView>
    );
  }
}))
export default SplashScreen
