package ru.micord.ervu.account_applications.component.service;

import java.util.List;
import java.util.Map;

import model.BpmnVariableForSave;
import model.FieldData;
import org.springframework.stereotype.Service;
import ru.micord.ervu.account_applications.component.exception.UserDataLoadException;
import ru.micord.ervu.account_applications.component.util.FormUtils;
import ru.micord.ervu.account_applications.service.EntityFetchService;
import service.container.FormMode;
import service.container.FormService;

import ru.cg.webbpm.modules.webkit.annotations.RpcSharedProperty;
import ru.cg.webbpm.modules.webkit.beans.Behavior;


/**
 * @author Adel Kalimullin
 */
@Service
public class ErvuFormLoadService extends Behavior implements FormService {
  public EntityFetchService entityFetchService;
  @RpcSharedProperty
  public FormMode mode = FormMode.SIMPLE;

  @Override
  public List<FieldData> loadData(Object id) {
    try {
      Map<String, List<String>> componentNameToIdMap = FormUtils.getComponentNameToIdMap(this);
      Map<String, Object> fieldNameAndValueMap = entityFetchService.fetchDataById(id);
      return FormUtils.createFieldData(componentNameToIdMap, fieldNameAndValueMap);
    }
    catch (Exception e) {
      throw new UserDataLoadException("Ошибка при загрузке данных", e);
    }
  }

  @Override
  public Object saveData(String s, List<FieldData> list, Boolean aBoolean,
      List<BpmnVariableForSave> list1) throws Exception {
    throw new UnsupportedOperationException();
  }

  @Override
  public Object saveData(Object o, List<FieldData> list) throws Exception {
    throw new UnsupportedOperationException();
  }

  @Override
  public List<FieldData> loadData() {
    throw new UnsupportedOperationException();
  }

  @Override
  public Object deleteData(Object o) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Object deleteData(String s, List<BpmnVariableForSave> list) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Object parseId(String id) {
    return id;
  }
}
