package com.oqpo.api.web.dto.response.session;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.*;
import com.oqpo.api.entity.oper.AfflEntity;
import com.oqpo.api.entity.oper.MemberEntity;
import com.oqpo.api.entity.oper.MenuEntity;
import com.oqpo.api.entity.oper.PopEntity;
import com.oqpo.api.entity.CodeEntity;
import com.oqpo.api.entity.stinfo.BrandEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

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

@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class SessionResponse {
    @ApiModelProperty(value = "사용자ID")
    private String userId;
	
    @ApiModelProperty(value = "사용자명")
    private String userNm;

    @ApiModelProperty(value = "시스템 관리자 여부")
    private String systemAdminYn;

    @ApiModelProperty(value = "사용자타입(10 운영자, 20 영업담당자, 30 가맹점, 40 브랜드, 50 매장, 60 공급사)")
    private String authTpCd;

    @ApiModelProperty(value = "10/20 오더퀸, 30 가맹점명, 40,50,60 브랜드명표시")
    private String authTpNm;

    @ApiModelProperty(value = "가맹점Key정보")
    private String afflAbbr;

    @ApiModelProperty(value = "가맹점아이디")
    private String afflShopId;

    @ApiModelProperty(value = "가맹점명")
    private String afflShopNm;

    @ApiModelProperty(value = "브랜드아이디")
    private String brandId;

    @ApiModelProperty(value = "브랜드명")
    private String brandNm;

    @ApiModelProperty(value = "코드 리스트")
    private List<CodeInfo> codeList;

    @ApiModelProperty(value = "메뉴 리스트")
    private List<AuthMenuInfo> authMenuList;

    @ApiModelProperty(value = "액션 리스트")
    private List<ActInfo> actList;

    @ApiModelProperty(value = "가맹점 리스트")
    private List<AfflInfo> afflList;

    @ApiModelProperty(value = "브랜드 리스트")
    private List<BrandInfo> brandList;

    public static SessionResponse toDTO(MemberEntity entity, List<CodeEntity> codeList, List<MenuEntity> authMenuList, List<ActionEntity> actList, List<AfflEntity> afflList, List<BrandEntity> brandList) {
        if (entity == null) return null;
        return SessionResponse.builder()
                .userId(entity.getUserId())
                .userNm(entity.getUserNm())
                .systemAdminYn(StringUtils.isBlank(entity.getSystemAdminYn()) ? "N" : entity.getSystemAdminYn())
                .authTpCd(entity.getAuthTpCd())
                .authTpNm(entity.getAuthTpNm() == null ? "" :entity.getAuthTpNm())
                .codeList(codeList.stream()
                        .map(m -> CodeInfo.builder()
                                .codeCd(m.getCodeCd())
                                .codeNm(m.getCodeNm())
                                .codeDtlList(m.getCodeDtlList().stream()
                                        .map(n -> CodeDtlInfo.builder()
                                                .codeDtlCd(n.getCodeDtlCd())
                                                .codeDtlNm(n.getCodeDtlNm())
                                                .build())
                                        .collect(Collectors.toList()))
                                .build())
                        .collect(Collectors.toList()))
                .authMenuList(authMenuList.stream()
                        .map(m -> AuthMenuInfo.builder()
                                .menuNo(m.getMenuNo())
                                .topMenuNo(m.getTopMenuNo())
                                .menuId(m.getMenuId())
                                .menuNm(m.getMenuNm())
                                .topMenuNm(m.getTopMenuNm())
                                .viewNum(m.getViewNum())
                                .level(m.getLevel())
                                .useYn(m.getUseYn())
                                .useYnNm(m.getUseYnNm())
                                .openCd(m.getOpenCd())
                                .bMenuCnt(m.getBMenuCnt())
                                .connUrl(m.getConnUrl())
                                .build())
                        .collect(Collectors.toList()))
                .actList(actList.stream()
                        .map(m -> ActInfo.builder()
                                .menuNo(m.getMenuNo())
                                .actId(m.getActId())
                                .actTpCd(m.getActTpCd())
                                .menuId(m.getMenuId())
                                .actNm(m.getActNm())
                                .connUrl(m.getConnUrl())
                                .viewYn(m.getViewYn())
                                .build())
                        .collect(Collectors.toList()))
                .afflList(afflList.stream()
                         .map(m -> AfflInfo.builder()
                                 .afflShopId(m.getAfflShopId())
                                 .afflShopNm(m.getAfflShopNm())
                                 .afflAbbr(m.getAfflAbbr())
                                 .build())
                         .collect(Collectors.toList()))
                .brandList(brandList.stream()
                        .map(m -> BrandInfo.builder()
                                .brandId(m.getBrandId())
                                .brandNm(m.getBrandNm())
                                .afflShopId(m.getAfflShopId())
                                .afflShopNm(m.getAfflShopNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class CodeInfo {
        @ApiModelProperty(value = "코드")
        private String codeCd;

        @ApiModelProperty(value = "코드명")
        private String codeNm;

        @ApiModelProperty(value = "상세코드 리스트")
        private List<CodeDtlInfo> codeDtlList;
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class CodeDtlInfo {
        @ApiModelProperty(value = "상세코드")
        private String codeDtlCd;

        @ApiModelProperty(value = "상세코드명")
        private String codeDtlNm;
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class AuthMenuInfo {
        @ApiModelProperty(value = "메뉴번호")
        private int menuNo;

        @ApiModelProperty(value = "상위메뉴번호")
        private int topMenuNo;

        @ApiModelProperty(value = "메뉴ID")
        private String menuId;

        @ApiModelProperty(value = "메뉴명")
        private String menuNm;

        @ApiModelProperty(value = "상위메뉴명")
        private String topMenuNm;

        @ApiModelProperty(value = "출력순번")
        private int viewNum;

        @ApiModelProperty(value = "레벨")
        private int level;

        @ApiModelProperty(value = "사용여부")
        private String useYn;

        @ApiModelProperty(value = "사용여부명")
        private String useYnNm;

        @ApiModelProperty(value = "출력구분(S-시스템, T-타이틀, C-화면전환, P-팝업")
        private String openCd;

        @ApiModelProperty(value = "하위 메뉴 카운트")
        private Integer bMenuCnt;

        @ApiModelProperty(value = "메뉴URL")
        private String connUrl;
    }



    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class ActInfo {
        @ApiModelProperty(value = "메뉴번호")
        private Integer menuNo;

        @ApiModelProperty(value = "액션ID")
        private String actId;

        @ApiModelProperty(value = "액션명")
        private String actNm;

        @ApiModelProperty(value = "액션타입코드")
        private String actTpCd;

        @ApiModelProperty(value = "메뉴ID")
        private String menuId;

        @ApiModelProperty(value = "호출URL")
        private String connUrl;

        @ApiModelProperty(value = "출력여부")
        private String viewYn;

    }


    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class AfflInfo {
        @ApiModelProperty(value = "가맹코드")
        private String afflShopId ;

        @ApiModelProperty(value = "가맹점명")
        private String afflShopNm;

        @ApiModelProperty(value = "약어")
        private String afflAbbr;

    }


    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class BrandInfo {
        @ApiModelProperty(value = "브랜드아이디")
        private String brandId ;

        @ApiModelProperty(value = "브랜드명")
        private String brandNm;

        @ApiModelProperty(value = "가맹코드")
        private String afflShopId ;

        @ApiModelProperty(value = "가맹점명")
        private String afflShopNm;

    }

}
