import {observable, action, autorun, computed} from 'mobx';
import {Http} from "../util/http";
import {Authentication} from "./authenticaton";

import GlobalUI from './global_ui';
import Agent from './agent';
import Employee from './employee';
import Reward from './reward';
import User from './user';
import PackageSchedules from './package_schedules';
import PackagePaymentMethod from './package_payment_method';
import Notification from './notification';
import Dashboard from './dashboard';
import Bank from './bank';
import PaymentMethod from './payment_method.js';
import Currency from './currency.js';
import PackagesService from './package_service.js';
import Packages from './packages.js';
import CustomerStore from "./customer";
import OrderStore from "./order";
import TransportationModule from './transportationStore';
import DepositStore from "./deposit";
import WithdrawStore from "./withdraw";
import Task from './task';
import Transaction from './transaction';
import {Firebase} from './firebase';
import AirlinesDomestic from "./airlines_domestic";
import Airports from "./airports";
import Categories from './categories';
import Recipes from './recipes';
import Help from './help';
import ContentManager from './content_manager';
import Setting from './setting';
import Roles from './roles';
import Commission from './commission';
import AppStore from './app';
import Airlines from './airlines';
import UserBanks from './user_banks';
import Profile from './profile';
import Stores from './stores';
import StoreList from './store_list';
import UserAddress from './user_address';
import { ItemStore } from './item';
import { MyStoreStore } from './my_store';
import { MyStoreItemStore } from './my_store_item';
import { ShippingMethodStore } from './shipping_method';
import {MyStoreShippingMethodStore} from "./my_store_shipping_method";
import {Messages} from "./message";
import {FeaturedItem} from './featured_item';
import {FeaturedStore} from './featured_store';
import EntitiesStore from "./entities";
import {CustomMenus} from './custom_menus';
import {FeaturedCategory} from "./featured_category";
import {FeaturedBanner} from "./featured_banner";
import {Tags} from './tags';
import Surf from './surf';
import Odoo from './odoo';

import Vouchers from './vouchers';
import {PurchasedItemStore} from "./purchased_item";
import WalletStore from "./wallet";
import {PurchasedItemVouchserStore} from "./purchased_item_voucher";

export default class AppState {
  http = new Http(this.token);
  app = new AppStore(this);
  auth = new Authentication(this);
  bank = new Bank(this);
  reward = new Reward(this);
  agent = new Agent(this);
  airlinesDomestic = new AirlinesDomestic(this);
  airports = new Airports(this);
  packageSchedules = new PackageSchedules(this);
  packagePaymentMethod = new PackagePaymentMethod(this);
  transportationStore = new TransportationModule(this);
  currencyStore = new Currency(this);
  employee = new Employee(this);
  commission = new Commission(this);
  airlines = new Airlines(this);
  featured_item = new FeaturedItem(this);
  featured_banner = new FeaturedBanner(this);
  custom_menu = new CustomMenus(this);
  featured_store = new FeaturedStore(this);
  featured_category = new FeaturedCategory(this);
  dashboard = new Dashboard(this);
  paymentMethod = new PaymentMethod(this);
  packages = new Packages(this);
  packageServiceStore = new PackagesService(this);
  user = new User(this);
  roles = new Roles(this);
  globalUI = new GlobalUI();
  customer = new CustomerStore(this);
  profile = new Profile(this);
  category = new Categories(this);
  recipes = new Recipes(this);
  help = new Help(this);
  contentManager = new ContentManager(this);
  order = new OrderStore(this);
  deposit = new DepositStore(this);
  withdraw = new WithdrawStore(this);
  task = new Task(this);
  transaction = new Transaction(this);
  setting = new Setting(this);
  firebase = new Firebase(this);
  userBanks = new UserBanks(this);
  stores = new Stores(this);
  storeList = new StoreList(this);
  vouchers = new Vouchers(this);
  userAddress = new UserAddress(this);
  item = new ItemStore(this);
  myStore = new MyStoreStore(this);
  myStoreItem = new MyStoreItemStore(this);
  myStoreShippingMethod = new MyStoreShippingMethodStore(this);
  shippingMethod = new ShippingMethodStore(this);
  message = new Messages(this);
  odoo = new Odoo(this);
  entities = new EntitiesStore(this);
  notification = new Notification(this);
  tags = new Tags(this);
  surf_turf = new Surf(this);
  purchased_items = new PurchasedItemStore(this);
  wallet = new WalletStore(this);
  purchased_voucher = new PurchasedItemVouchserStore(this);

  constructor(initialState) {
    this.token = initialState.token;
    this.http.token = this.token;
    if (typeof window !== 'undefined') {
      if (!this.token) {
        localStorage.removeItem('id_token');
      } else {
        localStorage.setItem('id_token', this.token);
        this.user.getUserGeolocation();
      }
    }
    if(this.token) {
      this.loadDataAfterLogin();
    }
  }

  @action
  setLogin(status) {
    this.isLoggedIn = status;
  }

  @action
  setToken(token) {
    this.token = token;
    this.http.token = token;
    localStorage.setItem('id_token', token);
  }

  @action
  setUserData(data) {
    // this.userData = data;
  }

  @action
  addItem(item) {
    this.items.push(item);
  }

  @action
  loadDataAfterLogin() {
    this.wallet.getData();
  }

  @computed get userData() {
    const token = this.token;
    if (!token) {
      return {
        user_id: '',
        role: ''
      }
    }

    let tokenData = JSON.parse(atob(token.split('.')[1]));

    return tokenData;
  }
}
