package vn.azteam.tpf.service.util;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.service.dto.*;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class PageableUtil {
    private final Logger log = LoggerFactory.getLogger(PageableUtil.class);

    public PageableUtil() {
    }

    public Page<TBObjectParameterDTO> changeTBObjectParameterDTOToPageFromList(List<TBObjectParameterDTO> tbObjectParameterDTOs, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbObjectParameterDTOs.size() ? tbObjectParameterDTOs.size() : (start + pageable.getPageSize()));
        if (tbObjectParameterDTOs.size() > start) {
            return new PageImpl<>(tbObjectParameterDTOs.subList(start, end), pageable, tbObjectParameterDTOs.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbObjectParameterDTOs.size());
    }

    public Page<TBActivityTypeDTO> changeTBActivityTypeDTOToPageFromList(List<TBActivityTypeDTO> tBActivityTypeDTOs, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tBActivityTypeDTOs.size() ? tBActivityTypeDTOs.size() : (start + pageable.getPageSize()));
        if (tBActivityTypeDTOs.size() > start) {
            return new PageImpl<>(tBActivityTypeDTOs.subList(start, end), pageable, tBActivityTypeDTOs.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tBActivityTypeDTOs.size());
    }

    public Page<TBGuidelineDetailsDTO> changeTBGuidelineDetailsDTOToPageFromList(List<TBGuidelineDetailsDTO> tBGuidelineDetailsDTOS,
                                                                                 Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tBGuidelineDetailsDTOS.size() ? tBGuidelineDetailsDTOS.size() : (start + pageable.getPageSize()));
        if (tBGuidelineDetailsDTOS.size() > start) {
            return new PageImpl<>(tBGuidelineDetailsDTOS.subList(start, end), pageable, tBGuidelineDetailsDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tBGuidelineDetailsDTOS.size());
    }

    public Page<TBActivityDTO> changeTBActivityDTOToPageFromList(List<TBActivityDTO> tbActivityDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbActivityDTOS.size() ? tbActivityDTOS.size() : (start + pageable.getPageSize()));
        if (tbActivityDTOS.size() > start) {
            return new PageImpl<>(tbActivityDTOS.subList(start, end), pageable, tbActivityDTOS.size());
        }
        return new PageImpl<>(com.google.common.collect.Lists.newArrayList(), pageable, tbActivityDTOS.size());
    }


    public Page<TBDetailUserDTO> changeTBDetailUserDTOToPageFromList(List<TBDetailUserDTO> tbDetailUserDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbDetailUserDTOS.size() ? tbDetailUserDTOS.size() : (start + pageable.getPageSize()));
        if (tbDetailUserDTOS.size() > start) {
            return new PageImpl<>(tbDetailUserDTOS.subList(start, end), pageable, tbDetailUserDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbDetailUserDTOS.size());
    }

    public Page<TBEquipmentDTO> changeTBEquipmentDTOToPageFromList(List<TBEquipmentDTO> tbEquipmentDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbEquipmentDTOS.size() ? tbEquipmentDTOS.size() : (start + pageable.getPageSize()));
        if (tbEquipmentDTOS.size() > start) {
            return new PageImpl<>(tbEquipmentDTOS.subList(start, end), pageable, tbEquipmentDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbEquipmentDTOS.size());
    }

    public Page<TBGuidelineDTO> changeTBGuidelineDTODTOToPageFromList(List<TBGuidelineDTO> tbGuidelineDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbGuidelineDTOS.size() ? tbGuidelineDTOS.size() : (start + pageable.getPageSize()));
        if (tbGuidelineDTOS.size() > start) {
            return new PageImpl<>(tbGuidelineDTOS.subList(start, end), pageable, tbGuidelineDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbGuidelineDTOS.size());
    }

    public Page<TBNotificationDTO> changeTBNotificationDTOToPageFromList(List<TBNotificationDTO> tbNotificationDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbNotificationDTOS.size() ? tbNotificationDTOS.size() : (start + pageable.getPageSize()));
        if (tbNotificationDTOS.size() > start) {
            return new PageImpl<>(tbNotificationDTOS.subList(start, end), pageable, tbNotificationDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbNotificationDTOS.size());
    }

    public Page<TBSuppliesDTO> changeTBSuppliesDTOToPageFromList(List<TBSuppliesDTO> tbSuppliesDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbSuppliesDTOS.size() ? tbSuppliesDTOS.size() : (start + pageable.getPageSize()));
        if (tbSuppliesDTOS.size() > start) {
            return new PageImpl<>(tbSuppliesDTOS.subList(start, end), pageable, tbSuppliesDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbSuppliesDTOS.size());
    }

    public Page<TBActivityExtendTypeDTO> changeTBActivityExtendTypeDTOToPageFromList(List<TBActivityExtendTypeDTO> tbActivityExtendTypeDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbActivityExtendTypeDTOS.size() ? tbActivityExtendTypeDTOS.size() : (start + pageable.getPageSize()));
        if (tbActivityExtendTypeDTOS.size() > start) {
            return new PageImpl<>(tbActivityExtendTypeDTOS.subList(start, end), pageable, tbActivityExtendTypeDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbActivityExtendTypeDTOS.size());
    }

    public Page<TBProductBlockDetailsDTO> changeTBProductBlockDetailsDTOToPageFromList(List<TBProductBlockDetailsDTO> tbProductBlockDetailsDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbProductBlockDetailsDTOS.size() ? tbProductBlockDetailsDTOS.size() : (start + pageable.getPageSize()));
        if (tbProductBlockDetailsDTOS.size() > start) {
            return new PageImpl<>(tbProductBlockDetailsDTOS.subList(start, end), pageable, tbProductBlockDetailsDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbProductBlockDetailsDTOS.size());
    }

    public Page<TBEquipmentOfCustomerDTO> changeTBEquipmentOfCustomerDTOToPageFromList(List<TBEquipmentOfCustomerDTO> tbEquipmentOfCustomerDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbEquipmentOfCustomerDTOS.size() ? tbEquipmentOfCustomerDTOS.size() : (start + pageable.getPageSize()));
        if (tbEquipmentOfCustomerDTOS.size() > start) {
            return new PageImpl<>(tbEquipmentOfCustomerDTOS.subList(start, end), pageable, tbEquipmentOfCustomerDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbEquipmentOfCustomerDTOS.size());
    }

    public Page<TBCropDTO> changeTBCropDTOToPageFromList(List<TBCropDTO> tbCropDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbCropDTOS.size() ? tbCropDTOS.size() : (start + pageable.getPageSize()));
        if (tbCropDTOS.size() > start) {
            return new PageImpl<>(tbCropDTOS.subList(start, end), pageable, tbCropDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbCropDTOS.size());
    }

    public Page<TBCodeDTO> changeTBCodeDTOToPageFromList(List<TBCodeDTO> tbCodeDTOS, Pageable pageable) {
        int start = Math.toIntExact(pageable.getOffset());
        int end = Math.toIntExact((start + pageable.getPageSize()) > tbCodeDTOS.size() ? tbCodeDTOS.size() : (start + pageable.getPageSize()));
        if (tbCodeDTOS.size() > start) {
            return new PageImpl<>(tbCodeDTOS.subList(start, end), pageable, tbCodeDTOS.size());
        }
        return new PageImpl<>(Lists.newArrayList(), pageable, tbCodeDTOS.size());
    }
}
