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.PurchaseChange;
import com.uas.platform.b2b.mobile.service.PurchaseChangeService;
import com.uas.platform.b2b.model.PurchaseOrderChange;

@Service("mobile.PurchaseChangeService")
public class PurchaseChangeServiceImpl implements PurchaseChangeService {

	@Override
	public Page<PurchaseChange> convertPurchaseOrderChange(final Page<PurchaseOrderChange> orderChangePage) {
		Page<PurchaseChange> page = new Page<PurchaseChange>() {

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

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

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

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

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

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

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

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

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

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

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

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

			@Override
			public List<PurchaseChange> getContent() {
				List<PurchaseChange> changes = new ArrayList<PurchaseChange>();
				List<PurchaseOrderChange> orders = orderChangePage.getContent();
				if (!CollectionUtils.isEmpty(orders)) {
					for (PurchaseOrderChange order : orders) {
						changes.add(new PurchaseChange(order));
					}
				}
				return changes;
			}

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

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

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

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

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

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

	@Override
	public PurchaseChange convertPurchaseOrderChange(PurchaseOrderChange orderChange) {
		return new PurchaseChange(orderChange);
	}

}
