import React, {useState} from 'react';
import RootNavigation from '../../../navigation/RootNavigation';
import LoginScreen from './LoginScreen';

import {useTranslation} from 'react-i18next';
import OneSignal from 'react-native-onesignal';
import {useDispatch} from 'react-redux';
import AuthApi from '../../../network/api/authApi';
import {getMyInfo} from '../../../store/actions/CommonAction';
import AsyncStorageService from '../../../utils/AsyncStorageService';
import {APP_NAVIGATE_SCREEN} from '../../../utils/constant';
const LoginContainer = props => {
  // console.log("props: ", props)
  const dispatch = useDispatch();
  const {t, i18n} = useTranslation();

  const [loading, setLoading] = useState(false);
  const [showPass, setShowPass] = useState(true);
  const [userAccount, setUserAccount] = useState({
    username: '',
    password: '',
  });
  const [showAlert, setShowAlert] = useState({
    isError: false,
    title: '',
    message: '',
  });

  const navigateToServer = () => {
    RootNavigation.navigate(APP_NAVIGATE_SCREEN.SERVER);
  };
  const handleUserName = text => {
    if (text != null) {
      setUserAccount({username: text, password: userAccount.password});
    }
  };
  const handlePassword = text => {
    if (text != null) {
      setUserAccount({username: userAccount.username, password: text});
    }
  };
  const hasErrors = () => {
    return !userAccount.username.includes('@');
  };

  const handleSetAccount = () => {
    if (
      userAccount.username.length <= 0 ||
      userAccount.username.password <= 0
    ) {
      setLoading(false);
      showAlertError('Hệ thống', 'Xin vui lòng nhập đầy đủ thông tin !!!');
      return false;
    }
    return true;
  };

  const handleLogin = async () => {
    try {
      setLoading(true);
      if (!handleSetAccount) return;

      // const encode_Pass = await encodePass(userAccount.password);
      //console.log(encodeURIComponent(userAccount.password));
      const res = await AuthApi.requestLoginApp({
        name: userAccount.username,
        pass: encodeURIComponent(userAccount.password),
      });
      //console.log(res)
      if (res.success) {
        AsyncStorageService.putToken(res.token);
        sendExternalId();
        RootNavigation.navigate(APP_NAVIGATE_SCREEN.MAIN);
        setLoading(false);
      }
    } catch (e) {
      console.log('error login: ', e);
      showAlertError('Hệ thống', e.toString());
      setLoading(false);
      // alertMessage(e.toString())
    }
  };
  const hideAlert = () => {
    setShowAlert({
      isError: false,
      title: '',
      message: '',
    });
    setLoading(false);
  };

  const showAlertError = (title, message) => {
    setShowAlert({
      isError: true,
      title: title,
      message: message,
    });
    setLoading(true);
  };
  const sendExternalId = async () => {
    const res = await dispatch(getMyInfo());
    OneSignal.setExternalUserId(res.user.id, results => {
      // The results will contain push and email success statuses
      // console.log('Results of setting external user id');
      console.log('setExternalUser', results);
    });
  };
  const loginProps = {
    loading,
    showAlert,
    showPass,
    handleLogin,
    setShowPass,
    userAccount,
    handleUserName,
    handlePassword,
    hideAlert,
    navigateToServer,
  };
  return <LoginScreen {...loginProps} />;
};

export default LoginContainer;
