package com.icomsys.main_vm.biz.common.system.service;

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.common.login.service.LoginService;
import com.icomsys.main_vm.biz.common.system.vo.DslDataTableRes;
import com.icomsys.main_vm.biz.common.system.vo.SystemPolicyMenuReq;
import com.icomsys.main_vm.biz.common.system.vo.SystemPolicyMenuRes;
import com.icomsys.main_vm.common.code.enumresource.PolicyResource;
import com.icomsys.main_vm.db.jpa.repo.system.TbCustPolicyRepo;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class SystemPolicyMenuService {

    private final LoginService loginService;
    private final TbCustPolicyRepo tbCustPolicyRepo;

    public DslDataTableRes<SystemPolicyMenuRes> PolicyMenuList(SystemPolicyMenuReq dto) {
        List<SystemPolicyMenuRes> result = tbCustPolicyRepo.PolicyMenuListQueryList(dto.ofUserVo(loginService.getUserVo()));

        List<SystemPolicyMenuRes> filtering = result.stream().map(e ->
                        SystemPolicyMenuRes.builder()
                                .custCode(e.getCustCode())
                                .custName(e.getCustName())
                                .serviceGroup(e.getServiceGroup())
                                .serviceGroupName(e.getServiceGroupName())
                                .policyVal(e.getPolicyVal())
                                .policyName(PolicyResource.valueOf(e.getPolicyVal()).getPolicyName())
                                .menuName(PolicyResource.valueOf(e.getPolicyVal()).getMenu().getMenuName())
                                .menuSeq(PolicyResource.valueOf(e.getPolicyVal()).getMenu().getMenuSeq())
                                .build()
                )
                .filter(h -> {
                    if (dto.getServiceGroup() != null && !dto.getServiceGroup().equals("") && !dto.getServiceGroup().equals("ALL")) {
                        if (h.getServiceGroup().equals(dto.getServiceGroup())) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                    return true;
                })
                .filter(f -> {
                    if (dto.getCodeText1() != null && !dto.getCodeText1().equals("")
                    ) {
                        if (f.getPolicyVal().toUpperCase().contains(dto.getCodeText1().trim().toUpperCase())) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                    if (dto.getCodeText2() != null && !dto.getCodeText2().equals("")
                    ) {
                        if (f.getPolicyName().toUpperCase().contains(dto.getCodeText2().trim().toUpperCase())) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                    if (dto.getCodeText3() != null && !dto.getCodeText3().equals("")
                    ) {
                        if (f.getMenuName().toUpperCase().contains(dto.getCodeText3().trim().toUpperCase())) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                    return true;
                })
                .collect(Collectors.toList());

        return new DslDataTableRes<SystemPolicyMenuRes>(
                filtering.stream().skip(dto.getSize() * dto.getPage())
                        .limit(dto.getSize())
                        .sorted(Comparator.comparing(SystemPolicyMenuRes::getMenuSeq)
                                ).collect(Collectors.toList())
                , Long.parseLong(String.valueOf(filtering.size()))
                , Long.parseLong(String.valueOf(dto.getSize()))
                , Long.parseLong(String.valueOf(dto.getSize() * dto.getPage())));
    }

}
