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.Notice;
import com.uas.platform.b2b.mobile.service.NoticeService;
import com.uas.platform.b2b.model.PurchaseNotice;

@Service("mobile.NoticeService")
public class NoticeServiceImpl implements NoticeService{

	@Override
	public Page<Notice> convertNotice(final
			Page<PurchaseNotice> notices) {
		Page<Notice> page = new Page<Notice>() {

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

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

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

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

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

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

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

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

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

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

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

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

			@Override
			public List<Notice> getContent() {
				List<Notice> list = new ArrayList<Notice>();
				List<PurchaseNotice> purchaseNotices = notices.getContent();
				if (!CollectionUtils.isEmpty(purchaseNotices)) {
					for (PurchaseNotice purchaseNotice : purchaseNotices) {
						list.add(new Notice(purchaseNotice));
					}
				}
				return list;
			}

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

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

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

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

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

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

	@Override
	public Notice convertNotice(PurchaseNotice notice) {
		return new Notice(notice);
	}

}
