package com.eactive.eai.rms.onl.manage.inflow.inflow;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.inflow.InflowControl;
import com.eactive.eai.data.entity.onl.inflow.InflowControlId;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.data.entity.onl.inflow.InflowControlService;
import com.eactive.eai.rms.data.entity.onl.inflow.InflowControlServiceDto;

@Service
public class InflowControlManService extends BaseService {

	private static final String ADAPTER_TYPE_INFLOW = "01";
	private static final String INTERFACE_TYPE_INFLOW = "02";

	@Autowired
	@Qualifier("comboService")
	public ComboService comboService;

	@Autowired
	private InflowControlService service;

	@Autowired
	private InflowControlManMapper mapper;

	// 어댑터 유량제어
	public Page<InflowControlManUI> selectAdapterList(Pageable pageable, String searchName) {
		Page<InflowControlServiceDto> dtoList = service.findAllForAdapter(pageable, searchName);
		return dtoList.map(mapper::toVo);
	}

	public InflowControlManUI selectAdapterDetail(String name) {
		InflowControlServiceDto dto = service.findByIdForAdapter(name);
		return mapper.toVo(dto);
	}

	public void mergeAdapter(InflowControlManUI ui) {
		ui.setType(ADAPTER_TYPE_INFLOW);
		InflowControlId id = toId(ui);
		Optional<InflowControl> inflowControlOptional = service.findById(id);

		InflowControl entity = null;
		if (inflowControlOptional.isPresent()) {
			entity = inflowControlOptional.get();
			mapper.updateToEntity(ui, entity);
		} else {
			entity = mapper.toEntity(ui);
		}
		service.save(entity);
	}

	public void deleteAdater(String name) {
		InflowControlId id = toId(ADAPTER_TYPE_INFLOW, name);
		service.deleteById(id);
	}
	
	// 인터페이스 유량제어
	public Page<InflowControlManUI> selectInterfaceList(Pageable pageable, String searchName) {
		Page<InflowControlServiceDto> dtoList = service.findAllForInterface(pageable, searchName);
		return dtoList.map(mapper::toVo);
	}

	public InflowControlManUI selectInterfaceDetail(String name) {
		return mapper.toVo(service.findByIdForInterface(name));
	}

	public void mergeInterface(InflowControlManUI ui) {
		ui.setType(INTERFACE_TYPE_INFLOW);
		InflowControlId id = toId(ui);
		Optional<InflowControl> inflowControlOptional = service.findById(id);

		InflowControl entity = null;
		if (inflowControlOptional.isPresent()) {
			entity = inflowControlOptional.get();
			mapper.updateToEntity(ui, entity);
		} else {
			entity = mapper.toEntity(ui);
		}
		service.save(entity);
	}

	public void deleteInterface(String name) {
		InflowControlId id = toId(INTERFACE_TYPE_INFLOW, name);
		service.deleteById(id);
	}
	
	private InflowControlId toId(InflowControlManUI ui) {
		return toId(ui.getType(), ui.getName());
	}
	
	private InflowControlId toId(String type, String name) {
		InflowControlId id = new InflowControlId();
		id.setType(type);
		id.setName(name);
		return id;
	}
	
}
