package com.uas.platform.b2b.erp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.uas.platform.b2b.dao.AttachDao;
import com.uas.platform.b2b.erp.model.QuotationDown;
import com.uas.platform.b2b.erp.model.QuotationDownDecide;
import com.uas.platform.b2b.erp.model.RemoteFile;
import com.uas.platform.b2b.erp.service.QuotationDownService;
import com.uas.platform.b2b.model.Attach;
import com.uas.platform.b2b.model.PurchaseInquiryItem;

@Service
public class QuotationDownServiceImpl implements QuotationDownService {

	@Autowired
	private AttachDao attachDao;

	@Override
	public List<QuotationDown> convertPurchaseInquiry(List<PurchaseInquiryItem> inquiryItems) {
		List<QuotationDown> downs = new ArrayList<QuotationDown>();
		if (!CollectionUtils.isEmpty(inquiryItems)) {
			for (PurchaseInquiryItem item : inquiryItems) {
				QuotationDown down = new QuotationDown(item);
				if (StringUtils.hasText(item.getInquiry().getAttach())) {
					String[] attachIds = item.getInquiry().getAttach().split(",");
					List<RemoteFile> files = new ArrayList<RemoteFile>();
					for (String attachId : attachIds) {
						Attach attach = attachDao.findOne(Long.parseLong(attachId));
						if (attach != null) {
							files.add(new RemoteFile(attach));
						}
					}
					down.setFiles(files);
				}
				downs.add(down);
			}
		}
		return downs;
	}

	@Override
	public List<QuotationDown> convertInquiryReply(List<PurchaseInquiryItem> inquiryItems) {
		List<QuotationDown> downs = new ArrayList<QuotationDown>();
		if (!CollectionUtils.isEmpty(inquiryItems)) {
			for (PurchaseInquiryItem item : inquiryItems)
				downs.add(new QuotationDown(item));
		}
		return downs;
	}

	@Override
	public List<QuotationDownDecide> convertInquiryReplyDecide(List<PurchaseInquiryItem> inquiryItems) {
		List<QuotationDownDecide> decides = new ArrayList<QuotationDownDecide>();
		if (!CollectionUtils.isEmpty(inquiryItems)) {
			for (PurchaseInquiryItem item : inquiryItems)
				decides.add(new QuotationDownDecide(item));
		}
		return decides;
	}

}
