import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { Selector, createSelector } from '@ngrx/store';
import {
  InfoRequest,
  RoomInfo,
  UserInfoShort,
  UserInfo
} from '@ucap-webmessenger/protocol-room';

export interface UserInfoShortListState extends EntityState<UserInfoShort> {}
export interface UserInfoListState extends EntityState<UserInfo> {}

export interface State {
  roomInfo: RoomInfo | null;
  userInfoShortList: UserInfoShortListState;
  userInfoList: UserInfoListState;
}

export const adapterUserInfoShort = createEntityAdapter<UserInfoShort>({
  selectId: userInfo => userInfo.seq
});
export const adapterUserInfo = createEntityAdapter<UserInfo>({
  selectId: userInfo => userInfo.seq
});

const userInfoShortListInitialState: UserInfoShortListState = adapterUserInfoShort.getInitialState(
  {}
);
const userInfoListInitialState: UserInfoListState = adapterUserInfo.getInitialState(
  {}
);

export const initialState: State = {
  roomInfo: null,
  userInfoShortList: userInfoShortListInitialState,
  userInfoList: userInfoListInitialState
};

const {
  selectAll: ngeSelectAllUserInfoShort,
  selectEntities: ngeSelectEntitiesUserInfoShort,
  selectIds: ngeSelectIdsUserInfoShort,
  selectTotal: ngeSelectTotalUserInfoShort
} = adapterUserInfoShort.getSelectors();
const {
  selectAll: ngeSelectAllUserInfo,
  selectEntities: ngeSelectEntitiesUserInfo,
  selectIds: ngeSelectIdsUserInfo,
  selectTotal: ngeSelectTotalUserInfo
} = adapterUserInfo.getSelectors();

export function selectors<S>(selector: Selector<any, State>) {
  const selectUserInfoShortList = createSelector(
    selector,
    (state: State) => state.userInfoShortList
  );

  const selectUserInfoList = createSelector(
    selector,
    (state: State) => state.userInfoList
  );

  return {
    roomInfo: createSelector(
      selector,
      (state: State) => state.roomInfo
    ),
    selectUserInfoShortList: createSelector(
      selectUserInfoShortList,
      ngeSelectAllUserInfoShort
    ),
    selectUserinfolist: createSelector(
      selectUserInfoList,
      ngeSelectAllUserInfo
    )
  };
}
