import { plainToInstance } from 'class-transformer';
import { MaterialEntity } from '../entity/MaterialEntity';
import { NoticeEntity } from '../entity/NoticeEntity';
import { apiService } from '../net/ApiService';
import { ToastUtils } from '../utils/ToastUtils';
import { BaseViewModel } from './BaseViewModel';

@ObservedV2
export class HomeViewModel extends BaseViewModel {
  @Trace noticeEntity?: NoticeEntity;
  @Trace materialList?: Array<MaterialEntity>;

  async noticeList() {
    try {
      const result = await apiService.noticeList();
      if (result.isSuccess()) {
        this.noticeEntity = plainToInstance(NoticeEntity, result.data);
      } else {
        ToastUtils.show(result.message, true);
      }
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
    }
  }

  async getMaterialList(page: string) {
    try {
      const result = await apiService.getMaterialList(page);
      if (result.isSuccess()) {
        this.materialList = plainToInstance(MaterialEntity, result.data!!['items'] as Array<Object>);
      } else {
        ToastUtils.show(result.message, true);
      }
    } catch (e) {
      console.log(e);
      ToastUtils.show(e);
    }
  }
}