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

class Store {
  @observable isLoading = false;
  @observable listPayment = [];
  @observable loan = null;
  @observable paymentInfo = null;
  @observable lastDay = null;

  @action
  async getPaymentInfo(id, type, onSuccess, onFailure) {
    try {
      this.isLoading = true;
      const response = await Api.getInstance().getTotalMoneyPayment(id, type);
      this.isLoading = false;
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.paymentInfo = response.data.data;
          onSuccess(response.data.data);
        } else {
          Alert.alert('Thông báo', response.data.meta.errorMessage);
          this.paymentInfo = null;
          onFailure();
        }
      } else {
        Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại sau!');
        this.paymentInfo = null;
        onFailure();
      }
    } catch (error) {
      this.isLoading = false;
      this.paymentInfo = null;
      console.log(error);
      onFailure();
    }
  }

  @action
  async getDetailLoan(id, onSuccess, onFailure) {
    try {
      this.isLoading = true;
      const response = await Api.getInstance().getLoanInfo(id);
      this.isLoading = false;
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.loan = response.data.data;
          if (onSuccess) onSuccess(response.data.data);
        } else {
          this.loan = null;
          Alert.alert('Thông báo', response.data.meta.errorMessage);
          if (onFailure) onFailure();
        }
      } else {
        this.loan = null;
        Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại sau!');
        if (onFailure) onFailure();
      }
    } catch (error) {
      this.loan = null;
      this.isLoading = false;
      console.log(error);
      if (onFailure) onFailure();
    }
  }

  @action
  async getListPayment(id, onSuccess, onFailure) {
    try {
      this.isLoading = true;
      const response = await Api.getInstance().getLoanPaymentSchedule(id);
      this.isLoading = false;
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.listPayment = response.data.data;
          if (onSuccess) onSuccess(response.data?.data);
          const newList = response.data.data.filter(
            (item) => item.status === 0
          );
          this.lastDay = Math.min(
            ...newList?.map((element) => {
              return new Date(element.payDate);
            })
          );
        } else {
          this.listPayment = [];
          if (onFailure) onFailure();
          Alert.alert('Thông báo', response.data.meta.errorMessage);
        }
      } else {
        this.listPayment = [];
        if (onFailure) onFailure();
        Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại sau!');
      }
    } catch (error) {
      this.listPayment = [];
      this.isLoading = false;
      console.log(error);
    }
  }
}
const DetailLoanStore = new Store();
export default DetailLoanStore;
