//@ts-nocheck
import { observable, action } from 'mobx';
import { Api } from '../../services/api';
import { myLog } from '../../utils/log';
import { Alert } from 'react-native';

class Store {
  @observable photo;
  @observable isLoading = false;
  @observable isError = false;
  @observable photoBase64 = undefined;

  @action
  async uploadImage(body, onSuccess?, onFailure?) {
    this.isLoading = true;
    const response = await Api.getInstance().uploadImage(body);
    this.isLoading = false;
    myLog(response);
    if (response.kind === 'ok') {
      if (response.data.meta.errorCode === 200) {
        this.isError = false;
        myLog('data', response.data.data);
        if (onSuccess) {
          onSuccess(response.data.data);
        }
      } else {
        Alert.alert('Thông báo', response.data.meta.errorMessage, [
          { text: 'Đồng ý' },
        ]);
        if (onFailure) onFailure();
      }
    } else {
      this.isLoading = false;
      this.isError = false;
      if (onFailure) onFailure();
      Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại sau');
    }
  }

  async checkSelfieImage(body, loanBriefId, onSuccess, onError) {
    this.isLoading = true;
    const response = await Api.getInstance().checkSelfieImage(
      body,
      loanBriefId
    );
    this.isLoading = false;
    myLog(response);
    if (response.kind === 'ok') {
      if (response.data.meta.errorCode === 200) {
        this.isError = false;
        myLog('data', response.data.data);
        if (onSuccess) {
          onSuccess(response.data.data);
        }
      } else {
        Alert.alert('Thông báo', response.data.meta.errorMessage, [
          { text: 'Đồng ý' },
        ]);
        onError();
      }
    } else {
      this.isLoading = false;
      this.isError = false;
      onError();
      Alert.alert('Lỗi', 'Có lỗi xảy ra. Vui lòng thử lại');
    }
  }
}

const selfieStore = new Store();

export default selfieStore;
