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

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.uas.platform.b2b.mobile.model.InquiryItem;
import com.uas.platform.b2b.mobile.service.InquiryItemService;
import com.uas.platform.b2b.model.PurchaseInquiryItem;

@Service("mobile.InquiryItemService")
public class InquiryItemServiceImpl implements InquiryItemService{

	@Override
	public Page<InquiryItem> convertInquiryItem(final
			Page<PurchaseInquiryItem> inquiryItemPage) {
		Page<InquiryItem> page = new Page<InquiryItem>() {

			@Override
			public Iterator<InquiryItem> iterator() {
				return getContent().iterator();
			}

			@Override
			public Pageable previousPageable() {
				return inquiryItemPage.previousPageable();
			}

			@Override
			public Pageable nextPageable() {
				return inquiryItemPage.nextPageable();
			}

			@Override
			public boolean isLast() {
				return inquiryItemPage.getNumber() == inquiryItemPage.getTotalPages();
			}

			@Override
			public boolean isFirst() {
				return inquiryItemPage.getNumber() == 1;
			}

			@Override
			public boolean hasPrevious() {
				return inquiryItemPage.hasPrevious();
			}

			@Override
			public boolean hasNext() {
				return inquiryItemPage.hasNext();
			}

			@Override
			public boolean hasContent() {
				return inquiryItemPage.hasContent();
			}

			@Override
			public Sort getSort() {
				return inquiryItemPage.getSort();
			}

			@Override
			public int getSize() {
				return inquiryItemPage.getSize();
			}

			@Override
			public int getNumberOfElements() {
				return inquiryItemPage.getNumberOfElements();
			}

			@Override
			public int getNumber() {
				return inquiryItemPage.getNumber();
			}

			@Override
			public List<InquiryItem> getContent() {
				List<InquiryItem> inquiryItems = new ArrayList<InquiryItem>();
				List<PurchaseInquiryItem> purchaseInquiryItems = inquiryItemPage.getContent();
				if (!CollectionUtils.isEmpty(purchaseInquiryItems)) {
					for (PurchaseInquiryItem purchaseInquiryItem : purchaseInquiryItems) {
						inquiryItems.add(new InquiryItem(purchaseInquiryItem));
					}
				}
				return inquiryItems;
			}

			@Override
			public boolean isLastPage() {
				return inquiryItemPage.isLast();
			}

			@Override
			public boolean isFirstPage() {
				return inquiryItemPage.isFirst();
			}

			@Override
			public boolean hasPreviousPage() {
				return !inquiryItemPage.isFirst();
			}

			@Override
			public boolean hasNextPage() {
				return !inquiryItemPage.isLast();
			}

			@Override
			public int getTotalPages() {
				return inquiryItemPage.getTotalPages();
			}

			@Override
			public long getTotalElements() {
				return inquiryItemPage.getTotalElements();
			}
		};
		return page;
	}

	@Override
	public InquiryItem convertInquiryItem(PurchaseInquiryItem inquiryItem) {
		return new InquiryItem(inquiryItem);
	}

}
