//@ts-nocheck
import { observable, action, toJS, makeAutoObservable } from 'mobx';
import { Api } from '../../services/api';
import { myLog } from '../../utils/log';
import { Alert } from 'react-native';
import appStore from '../../AppStore';
import { LOAN_DATA, save } from '../../utils/storage';

class Store {
  @observable isLoading = false;
  @observable isError = false;
  @observable data = {};
  @observable checkEKYCLoading = false;
  @observable checkEKYC = {};
  @observable checkValidESignLoading = false;
  @observable verifyImageLoading = false;
  @observable verifyImage = false;
  @observable listLoan = [];
  @observable listLoanPending = [];

  constructor() {
    makeAutoObservable(this);
  }
  @action
  async schedulePayment(onSuccess?, onFailure?) {
    this.isLoading = true;
    const response = await Api.getInstance().schedulePayment({
      phone: appStore.loanData.phone,
    });
    this.isLoading = false;
    myLog(response);
    if (response.kind === 'ok') {
      if (response.data.meta.errorCode === 200) {
        this.isError = false;
        this.data = response.data.data;
        myLog('data', response.data.data);
        if (onSuccess) {
          onSuccess(response.data.data);
        }
      } else {
        // Alert.alert('Thông báo', response.data.meta.errorMessage, [{ text: 'Đồng ý' }])
      }
    } else {
      this.isLoading = false;
      this.isError = false;
      Alert.alert('Lỗi', response.Messages);
    }
  }

  @action
  async loanInfo(onSuccess?, onFailure?) {
    const response = await Api.getInstance().loanInfo({
      phone: appStore.phone,
    });
    myLog(response);
    if (response.kind === 'ok') {
      if (response.data.meta.errorCode === 200) {
        save(LOAN_DATA, response.data.data);
        appStore.loanData = response.data.data;
        if (onSuccess) {
          onSuccess(response.data.data);
        }
      } else if (response.data.meta.errorCode === 404) {
        save(LOAN_DATA, { status: 0, phone: appStore.phone });
        appStore.loanData = { status: 0, phone: appStore.phone };
        // Alert.alert('Thông báo', response.data.meta.errorMessage, [{ text: 'Đồng ý' }])
      }
    } else {
      Alert.alert('Lỗi', response.Messages);
    }
  }

  @action
  async getCheckEKYC(params, onSuccess?, onFailure?) {
    this.checkEKYCLoading = true;
    const response = await Api.getInstance().checkEKYC(params);
    // myLog(response);
    this.checkEKYCLoading = false;
    if (response.kind === 'ok') {
      this.checkEKYC[params.loanBriefId] = response.data.meta !== null;
      // myLog(toJS(this.checkEKYC));
      if (onSuccess) {
        onSuccess(response);
      }
    } else {
      // Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại', [{ text: 'Đồng ý' }])
    }
  }

  @action
  async checkValidateESign(params, onSuccess?, onFailure?) {
    this.checkValidESignLoading = true;
    const response = await Api.getInstance().checkValidateESign(params);
    this.checkValidESignLoading = false;
    if (response.kind === 'ok') {
      if (response.data.meta.errorCode === 200) {
        if (onSuccess) onSuccess(response.data.data);
      } else {
        Alert.alert('Thông báo', response.data.meta.errorMessage);
      }
    } else {
      Alert.alert(
        'Thông báo',
        'Kiểm tra thông tin đơn vay không thành công. Vui lòng thử lại'
      );
    }
  }

  @action
  async checkVerifyImage(params, onSuccess?, onFailure?) {
    this.verifyImageLoading = true;
    const response = await Api.getInstance().checkVerifyImage(params);
    this.verifyImageLoading = false;
    this.verifyImage = response.data.meta?.errorCode === 200;
    if (response.data.meta?.errorCode === 200) {
      if (onSuccess) onSuccess(response.data.data);
    } else {
      if (onFailure) onFailure();
    }
  }

  @action
  async renderContract(loanBriefId, onSuccess?) {
    this.isLoading = true;
    const response = await Api.getInstance().renderContract(loanBriefId);
    this.isLoading = false;
    if (response.data.meta?.errorCode === 200) {
      if (onSuccess) onSuccess(response.data.data);
    } else {
      Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại sau!');
    }
  }

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

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

const homeStore = new Store();

export default homeStore;
