import { request } from 'utils/fetch';
import { api } from 'constants/config';

export function getTenants() {
  return async (dispatch) => {
    const result = await request(api.tenantList, undefined, { limit: 999 });

    if (result) {
      dispatch({
        type: 'TENANT_LIST',
        payload: result
      });
    }

    return result;
  };
}

export function changeTenantsPage(page) {
  return {
    type: 'TENANT_PAGE_CHANGE',
    payload: page
  };
}

export function searchTenants(search) {
  return {
    type: 'TENANT_SEARCH',
    payload: search
  };
}

export function inviteUser(param, callback) {
  return async (dispatch) => {
    const result = await request(api.tenantInvite, { id: param.tenantId }, undefined, JSON.stringify({ userId: param.userId }));

    if (result) {
      dispatch({
        type: 'TENANT_INVITE',
        payload: result
      });
      callback && callback();
    }
  };
}

export function removeUser(param) {
  return async (dispatch) => {
    const result = await request(api.tenantRemove, param);

    if (result) {
      dispatch({
        type: 'TENANT_REMOVE',
        payload: param
      });
    }
  };
}

export function createTenant(param, callback) {
  return async (dispatch) => {
    const result = await request(api.tenantCreate, undefined, undefined, JSON.stringify(param));

    if (result) {
      dispatch({
        type: 'TENANT_CREATE',
        payload: {
          ...param,
          ...result
        }
      });
      callback && callback();
    }
  };
}

export function deleteTenant(id) {
  return async (dispatch) => {
    const result = await request(api.tenantDelete, { id });

    if (result) {
      dispatch({
        type: 'TENANT_DELETE',
        payload: id
      });
    }

    return result;
  };
}

export function getUsers(id) {
  return async (dispatch) => {
    const result = await request(api.tenantUserList, { id }, { limit: 999 });

    if (result) {
      dispatch({
        type: 'TENANT_USER_LIST',
        payload: {
          id,
          list: result
        }
      });
    }

    return result;
  };
}

const initialState = {
  tenants: {
    search: '',
    page: 1,
    pageSize: 20,
    list: []
  },
  users: {}
};

export default function reducer(state = initialState, { type, payload }) {
  switch (type) {
    case 'TENANT_LIST': {
      let rootTenant = payload.filter((d) => d.id === 1);
      let tenants = payload.filter((d) => d.id !== 1);
      let newList = rootTenant.concat(tenants);
      return {
        ...state,
        tenants: {
          ...state.tenants,
          list: newList
        }
      };
    }
    case 'TENANT_REMOVE': {
      const { tenantId, userId } = payload;

      return {
        ...state,
        users: {
          ...state.users,
          [tenantId]: state.users[tenantId].filter((o) => o.id != userId)
        }
      };
    }
    case 'TENANT_INVITE': {
      const { tenantId, user } = payload;
      return {
        ...state,
        users: {
          ...state.users,
          [tenantId]: [user].concat(state.users[tenantId] || [])
        }
      };
    }
    case 'TENANT_PAGE_CHANGE': {
      return { ...state, tenants: { ...state.tenants, page: payload } };
    }
    case 'TENANT_SEARCH': {
      return { ...state, tenants: { ...state.tenants, search: payload } };
    }
    case 'TENANT_CREATE': {
      let rootTenant = state.tenants.list.filter((d: any) => d.id === 1);
      let tenants = state.tenants.list.filter((d: any) => d.id !== 1);
      let newList = rootTenant.concat(payload).concat(tenants);
      return { ...state, tenants: { ...state.tenants, list: newList } };
    }
    case 'TENANT_DELETE': {
      let rootTenant = state.tenants.list.filter((d: any) => d.id === 1);
      let deletedTenants = state.tenants.list.filter((d: any) => d.id !== 1 && d.id != payload);
      let newList = rootTenant.concat(deletedTenants);
      return { ...state, tenants: { ...state.tenants, list: newList } };
    }
    case 'TENANT_USER_LIST': {
      return {
        ...state,
        users: {
          ...state.users,
          [payload.id]: payload.list
        }
      };
    }
    default:
      return state;
  }
}
