//@ts-nocheck
import axios from 'axios';
import { action, observable } from 'mobx';
import { Alert } from 'react-native';
import {
  getUniqueId,
  getBundleId,
  getVersion,
  getBuildNumber,
} from 'react-native-device-info';
import publicIP from 'react-native-public-ip';
import { Api } from './services/api';

class AppStore {
  @observable isLoading = false;
  @observable isError = false;
  @observable loanData = [];
  @observable user = {};
  @observable phone = '';
  @observable nationalID = {};
  @observable registerMotor = {};
  @observable selectedTab = 'TAB1';
  @observable deviceId = '';
  @observable publicIp = '';
  @observable lat = 0;
  @observable long = 0;
  @observable appRefID = '';
  @observable userDevicePayload = '';
  @observable info = null;
  @observable isCodepush = false;
  @observable processing = 10;
  @observable callback: () => void = undefined;
  @observable listFirstDayPayment = [];
  @observable listInsurenceTimeAll = [];
  @observable listRateType = [];
  @observable listLoanTime = [];
  @observable listLoanTimeByType = [];

  @action
  async checkQualityImage(image, onSuccess?, onFailed?) {
    this.isLoading = true;
    try {
      const response = await axios.post(
        'https://gateway.tima-ai.dev/computer-vision/image-quality-score/predict-score',
        {
          image: image,
        },
        {
          headers: {
            'Authorization':
              'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhaV9zdXBlcl9hZG1pbl90ZXN0IiwiYXV0aG9yaXRpZXMiOlsiUk9MRV8xNDE0X21lc3NhZ2VfdmFsaWRhdGUiLCJST0xFX2FtYXppbmdfY3Jhd2xlciIsIlJPTEVfYXBwcmFpc2FsX2F1dG9fY2hlY2siLCJST0xFX2F1dG9fbW9qX3NlY3VyZWRfdHJhbnNmZXIiLCJST0xFX2NlbnRlci1zZWN1cml0eSIsIlJPTEVfY2hhdC1nYXRld2F5IiwiUk9MRV9jaGVjay1sb2NhdGlvbiIsIlJPTEVfY2l2LXNlcnZpY2UtYXBwbGljYXRpb24iLCJST0xFX2NvbXB1dGVyX3Zpc2lvbiIsIlJPTEVfY29tcHV0ZXJfdmlzaW9uX3Rlc3QiLCJST0xFX2NvbnRyYWN0X2NoZWNrX2VkaXRlZCIsIlJPTEVfY3Jhd2wtc2VydmljZSIsIlJPTEVfY3JlZGl0LXNjb3JlLXNlcnZpY2UiLCJST0xFX2NyZWRpdC1zY29yaW5nLWJ5LXZlaGljbGUiLCJST0xFX2NyZWRpdC1zY29yaW5nLWJ5LXZlaGljbGUtbmV3LWxhYmVsIiwiUk9MRV9lcnAtc3VwcG9ydCIsIlJPTEVfZmFjZWJvb2staW5mb3JtYXRpb24iLCJST0xFX2ZiLWFjdGl2ZS1zY29yaW5nIiwiUk9MRV9mcmF1ZC1zY29yZS1hcGkiLCJST0xFX2ZyYXVkLXNlcnZpY2UiLCJST0xFX2lkZW50aWZ5LXNlcnZpY2UiLCJST0xFX2ltYWdlLXV0aWxzIiwiUk9MRV9sb2FuLWV2ZW50LWxvZyIsIlJPTEVfbW90b3ItcHJpY2UtcmVwb3J0IiwiUk9MRV9wcmVkaWN0aW5nLXZlaGljbGUtcHJpY2UiLCJST0xFX3ByaWNlLXByZWRpY3Rpb24tc2VydmljZSIsIlJPTEVfcHJpY2UtdmVoaWNsZS1kYXRhLWFwaSIsIlJPTEVfc2FudGltYS1sZWFkLXNjb3JpbmciLCJST0xFX3RheC1pbmZvLXNlcnZpY2UiLCJST0xFX3RleHQtZXh0cmFjdG9yIiwiUk9MRV90aW1hLWRhdGEtc2VydmljZSIsIlJPTEVfdG9wLWZyaWVuZHMtc2VydmljZSIsIlJPTEVfdHJhY2stbG9jYXRpb24iLCJST0xFX3VzZXItaW50ZXJlc3RzLXNlcnZpY2UiLCJST0xFX3Zpbi1wYXJzZXIiXSwiaWF0IjoxNjQ4MjYxNzcxLCJleHAiOjMxNTUyNDgyNjE3NzF9.O2Ka5i97gq9JH8sQwqHtmDuYZ7_ufd8KvcjgtLMAWckvzhrzM236aSrHa1FtFutSG1Ap2-9-FK0ggAeR4G8lBg',
            'content-type': 'application/json',
            'Access-Control-Allow-Headers':
              'Origin, X-Requested-With, Content-Type, Accept',
          },
          timeout: 120000,
        }
      );
      console.log(response, 'check qualify anh');

      this.isLoading = false;
      if (response.status === 200) {
        if (response.data.Score < 3) {
          if (onFailed) onFailed();
        } else {
          if (onSuccess) onSuccess();
        }
      }
    } catch (error) {
      this.isLoading = false;

      console.log(error);
      Alert.alert('Thông báo', 'Có lỗi xảy ra. Vui lòng thử lại sau!');
    }
  }

  @action
  async subscribeTopicFirebase(
    tokenFCM: string,
    topic: string,
    keyFirebase: string
  ) {
    return await axios
      .create({
        timeout: 50000,
      })
      .post(
        `https://iid.googleapis.com/iid/v1/${tokenFCM}/rel/topics/${topic}`,
        null,
        {
          headers: {
            Authorization: `key=${keyFirebase}`,
          },
        }
      );
  }

  @action
  async unSubscribeTopicFirebase(
    tokenFCM: string,
    topic: string,
    keyFirebase: string
  ) {
    return await axios
      .create({
        timeout: 50000,
      })
      .post(
        'https://iid.googleapis.com/iid/v1:batchRemove',
        {
          to: `/topics/${topic}`,
          registration_tokens: [tokenFCM],
        },
        {
          headers: {
            Authorization: `key=${keyFirebase}`,
          },
        }
      );
  }

  @action
  async getTopicFirebaseSubscribed(tokenFCM: string, keyFirebase: string) {
    return await axios
      .create({
        timeout: 50000,
      })
      .post(
        `https://iid.googleapis.com/iid/info/${tokenFCM}?details=true`,
        null,
        {
          headers: {
            Authorization: `key=${keyFirebase}`,
          },
        }
      );
  }

  @action
  async getFCMTocken(onSuccess?) {
    try {
      const resp = await Api.getInstance().getFCMToken();
      if (resp.kind === 'ok') {
        if (resp.data.meta.errorCode === 200) {
          if (onSuccess) onSuccess(resp.data.data);
        } else {
        }
      } else {
      }
    } catch (error) {}
  }

  @action
  async getInfo() {
    try {
      const resp = await Api.getInstance().getInfo();
      if (resp.kind === 'ok') {
        if (resp.data.meta.errorCode === 200) {
          this.info = resp.data.data;
        } else {
        }
      } else {
      }
    } catch (error) {}
  }

  @action
  async logout() {
    try {
      const resp = await Api.getInstance().logout();
      if (resp.kind === 'ok') {
        if (resp.data.meta.errorCode === 200) {
        } else {
        }
      } else {
      }
    } catch (error) {}
  }

  @action
  async getAppId() {
    const data = await getBundleId();
    this.appRefID = data;
  }

  @action
  async getDeviceId() {
    const data = await getUniqueId();
    this.deviceId = data;
  }

  @action
  async getPublicIp() {
    const data = await publicIP();
    this.publicIp = data;
  }

  @action
  async getDevicePayload() {
    const buildNumber = await getBuildNumber();
    const version = await getVersion();
    this.userDevicePayload = `${version},${buildNumber}`;
  }

  @action
  async getLoanTimeAll() {
    try {
      const response = await Api.getInstance().getLoanTimeAll();
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.listLoanTime = response.data.data;
        } else {
        }
      }
    } catch (error) {}
  }

  @action
  async getLoanTimeByType(rateTypeId, typeLoan, onSuccess?) {
    this.listLoanTimeByType = [];
    try {
      const response = await Api.getInstance().getLoanTimeByType(
        rateTypeId,
        typeLoan
      );
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.listLoanTimeByType = response.data.data;
          if (onSuccess) onSuccess();
        } else {
          this.listLoanTimeByType = [];
        }
      }
    } catch (error) {
      this.listLoanTimeByType = [];
    }
  }

  @action
  async getFirstDayPayment(id, type) {
    try {
      const response = await Api.getInstance().getFirstDayPayment(id, type);
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.listFirstDayPayment = response.data.data;
        } else {
        }
      }
    } catch (error) {}
  }

  @action
  async getRateTypeAll(onSuccess?) {
    try {
      const response = await Api.getInstance().getRateTypeAllNew();
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.listRateType = response.data.data;
          if (onSuccess) onSuccess();
        } else {
        }
      }
    } catch (error) {}
  }

  @action
  async getInsurenceTimeAll() {
    try {
      const response = await Api.getInstance().getInsurenceTimeAll();
      if (response.kind === 'ok') {
        if (response.data.meta.errorCode === 200) {
          this.listInsurenceTimeAll = response.data.data;
        } else {
        }
      }
    } catch (error) {}
  }
}

const appStore = new AppStore();
export default appStore;
