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

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

import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.uas.platform.b2b.mobile.model.Purchase;
import com.uas.platform.b2b.mobile.service.PurchaseService;
import com.uas.platform.b2b.model.PurchaseOrderAll;

@Service("mobile.PurchaseService")
public class PurchaseServiceImpl implements PurchaseService {

	@Override
	public Page<Purchase> convertPurchaseOrder(final Page<PurchaseOrderAll> orderPage) {
		Page<Purchase> purchasePage = new Page<Purchase>() {

			@Override
			public List<Purchase> getContent() {
				List<Purchase> purchases = new ArrayList<Purchase>();
				List<PurchaseOrderAll> orders = orderPage.getContent();
				if (!CollectionUtils.isEmpty(orders)) {
					for (PurchaseOrderAll order : orders) {
						purchases.add(new Purchase(order));
					}
				}
				return purchases;
			}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		};
		return purchasePage;
	}

	@Override
	public Purchase convertPurchaseOrder(PurchaseOrderAll order) {
		return new Purchase(order);
	}

}
