import React, { PureComponent } from "react";
import {
  StyleSheet,
  Text,
  View,
  Image,
  TouchableOpacity,
  StatusBar,
  ToastAndroid,
  TextInput
} from "react-native";
import BasePage from "./BasePage";
import SplashScreen from "react-native-splash-screen";
import commonutil from "./utils/commonutil";
export default class Splash extends BasePage {
  state = {
    exist: false,
    MainPageParams: ""
  };
  render() {
    return (
      <View style={{ flex: 1 }}>
        <StatusBar backgroundColor={"transparent"} translucent={true} />
        <View style={{ flex: 1 }} />
        <View style={{ flex: 5 }}>
          <Text style={{ fontSize: 30 }}>模拟活动广告。。。。</Text>
        </View>
      </View>
    );
  }

  componentWillMount() {
    // global.storage.remove({ key: 'userInfo' });
    this.getUserInfo();
  }

  componentDidMount() {}

  async getUserInfo() {
    //判断是否有用户
    await global.storage
      .load({
        key: "userInfo"
      })
      .then(result => {
     
        var usermap = commonutil.jsonToMap(result);
        this.setState({
          exist: true,
          MainPageParams: usermap.get("ageGroup")
        });
      })
      .catch(err => {
        console.log(err.message);
        switch (err.name) {
          case "NotFoundError":
            // TODO;
            // alert('NotFoundError');
            this.setState({
              exist: false
						});
						SplashScreen.hide();
            this.Advertisement();
            break;
          case "ExpiredError":
            // TODO
            // alert('ExpiredError');
            break;
        }
      });
  }

  //获取用户之后的操作,或者是别的操作。
  Advertisement() {
    //假装3秒广告
    setTimeout(() => {
      if (this.state.exist) {
        this.clearPageToNext("MainPage");
      } else {
        this.clearPageToNext("Login");
      }
    }, 3000);
  }
}