package com.oqpo.api.service;

import com.oqpo.api.config.JwtTokenProvider;
import com.oqpo.api.entity.oper.MemberEntity;
import com.oqpo.api.entity.stinfo.BrandEntity;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.MemberMapper;
import com.oqpo.api.mapper.stinfo.BrandMapper;
import com.oqpo.api.service.oper.AfflService;
import com.oqpo.api.service.oper.UserMngService;
import com.oqpo.api.service.stinfo.BrandService;
import com.oqpo.api.service.system.ActionService;
import com.oqpo.api.service.system.CodeService;
import com.oqpo.api.service.system.MenuService;
import com.oqpo.api.util.StringUtil;
import com.oqpo.api.web.dto.response.session.GetKeyNumResponse;
import com.oqpo.api.web.dto.response.session.MsessionResponse;
import com.oqpo.api.web.dto.response.session.SessionBrandListResponse;
import com.oqpo.api.web.dto.response.session.SessionResponse;
import com.oqpo.api.web.dto.response.stinfo.brand.BsnsBrandListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Slf4j
public class SessionService extends CommonService {
    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    @Autowired
    private CodeService codeService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private ActionService actionService;

    @Autowired
    private UserMngService userMngService;

    @Autowired
    private AfflService afflService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private BrandMapper brandMapper;



    public SessionResponse data(HttpServletRequest request) throws Exception {
        MemberEntity memberEntity = new MemberEntity();
        String connKey = jwtTokenProvider.resolveToken(request);
        try {
            memberEntity.setUserId(jwtTokenProvider.getUserId(connKey));
            memberEntity.setUserNm(jwtTokenProvider.getUserNm(connKey));
            memberEntity.setSystemAdminYn(jwtTokenProvider.getSystemAdminYn(connKey));
            memberEntity.setAuthTpCd(jwtTokenProvider.getAuthTpCd(connKey));
            memberEntity.setAuthTpNm(jwtTokenProvider.getAuthTpNm(connKey));

            MemberEntity groupInfo =  memberMapper.selectMemberById(memberEntity.getUserId());
            memberEntity.setAfflShopId(groupInfo.getAfflShopId());
            memberEntity.setAfflShopNm(groupInfo.getAfflShopNm());
            memberEntity.setBrandId(groupInfo.getBrandId());
            memberEntity.setBrandNm(groupInfo.getBrandNm());
            memberEntity.setStoreId(groupInfo.getStoreId());
            memberEntity.setStoreNm(groupInfo.getStoreNm());
            memberEntity.setSpplyId(groupInfo.getSpplyId());
            memberEntity.setSpplyNm(groupInfo.getSpplyNm());
            memberEntity.setOqBrandId(groupInfo.getOqBrandId());
            memberEntity.setOqStoreId(groupInfo.getOqStoreId());

            // 권한이 매장이면 결재가능여부 정보 및 mallId 값 전송
            if ("50".equals(memberEntity.getAuthTpCd())) {
                // 매장  selectBrandPgInfo
                BrandEntity pgEntity = brandMapper.selectBrandPgInfo(memberEntity.getBrandId());
                if (pgEntity.getMallId() == null || "".toString().equals(pgEntity.getMallId())) {
                    memberEntity.setApprYn("N");
                    memberEntity.setMallId("");
                } else {
                    memberEntity.setApprYn("Y");
                    memberEntity.setMallId(pgEntity.getMallId());
                }

            } else {
                memberEntity.setApprYn("N");
                memberEntity.setMallId("");
            }

            // 브랜드 일 경우 간소화 프로세스인지 여부
            if ("40".equals(memberEntity.getAuthTpCd())) {
                BrandEntity procBrand = brandMapper.selectBrandInfo(memberEntity.getBrandId());
                memberEntity.setShmtColProcType(procBrand.getShmtColProcType());  // 1 이면 간소화
            } else {
                memberEntity.setShmtColProcType("0");
            }

            // 사용자정보 가져오기
            //if ("60".equals(memberEntity.getAuthTpCd())) {
                // 공급사로 - 공급사 테이블에서 공급사아이디와 공급사명 가져오기
            //}


        } catch (Exception e) {
            e.getStackTrace();
            throw new GlobalException(SystemMessageCode.NEED_AUTH);
        }
        return SessionResponse.toDTO(memberEntity,
                codeService.codeList(memberEntity.getUserId()),
                menuService.authMenuList(memberEntity.getUserId(), memberEntity),
                actionService.actionList(memberEntity.getUserId(), memberEntity)
                );
    }

    public SessionResponse mdata(HttpServletRequest request) throws Exception {
        MemberEntity memberEntity = new MemberEntity();
        String connKey = jwtTokenProvider.resolveToken(request);
        try {
            memberEntity.setUserId(jwtTokenProvider.getUserId(connKey));
            memberEntity.setUserNm(jwtTokenProvider.getUserNm(connKey));
            memberEntity.setSystemAdminYn(jwtTokenProvider.getSystemAdminYn(connKey));
            memberEntity.setAuthTpCd(jwtTokenProvider.getAuthTpCd(connKey));
            memberEntity.setAuthTpNm(jwtTokenProvider.getAuthTpNm(connKey));

            MemberEntity groupInfo =  memberMapper.selectMemberById(memberEntity.getUserId());
            memberEntity.setAfflShopId(groupInfo.getAfflShopId());
            memberEntity.setAfflShopNm(groupInfo.getAfflShopNm());
            memberEntity.setBrandId(groupInfo.getBrandId());
            memberEntity.setBrandNm(groupInfo.getBrandNm());
            memberEntity.setStoreId(groupInfo.getStoreId());
            memberEntity.setStoreNm(groupInfo.getStoreNm());
            memberEntity.setSpplyId(groupInfo.getSpplyId());
            memberEntity.setSpplyNm(groupInfo.getSpplyNm());

            if (StringUtil.isEmpty(groupInfo.getBrandId())) {
                memberEntity.setBrandId(jwtTokenProvider.getBrandId(connKey));
            }

            // 권한이 매장이면 결재가능여부 정보 및 mallId 값 전송
            if ("50".equals(memberEntity.getAuthTpCd())) {
                // 매장  selectBrandPgInfo
                BrandEntity pgEntity = brandMapper.selectBrandPgInfo(memberEntity.getBrandId());
                if (pgEntity.getMallId() == null || "".toString().equals(pgEntity.getMallId())) {
                    memberEntity.setApprYn("N");
                    memberEntity.setMallId("");
                } else {
                    memberEntity.setApprYn("Y");
                    memberEntity.setMallId(pgEntity.getMallId());

                }

            } else {
                memberEntity.setApprYn("N");
                memberEntity.setMallId("");
            }

            // 브랜드 일 경우 간소화 프로세스인지 여부
            if ("40".equals(memberEntity.getAuthTpCd())) {
                BrandEntity procBrand = brandMapper.selectBrandInfo(memberEntity.getBrandId());
                memberEntity.setShmtColProcType(procBrand.getShmtColProcType());  // 1 이면 간소화
            } else {
                memberEntity.setShmtColProcType("0");
            }


        } catch (Exception e) {
            e.getStackTrace();
            throw new GlobalException(SystemMessageCode.NEED_AUTH);
        }
        return SessionResponse.toDTO(memberEntity,
                codeService.codeList(memberEntity.getUserId()),
                menuService.authMobileMenuList(memberEntity.getUserId(), memberEntity),
                actionService.actionList(memberEntity.getUserId(), memberEntity));
    }

    //selectBsnsBrandList
    public SessionBrandListResponse bsnsBrandList(String bsnsRegNo) throws Exception {
        return SessionBrandListResponse.toDTO(brandService.selectBsnsBrandList(bsnsRegNo));
    }


    // key 생성 작업
    public GetKeyNumResponse getKeyInfo(String keyType) throws Exception {
        String keyNum = "";
        if ("Br".equals(keyType)) {  //브랜드
            keyNum = fnGetKeyNo(23);
        } else if ("St".equals(keyType)) {
            keyNum = fnGetKeyNo(24);
        } else if ("It".equals(keyType)) {
            keyNum = fnGetKeyNo(26);
        }
        return GetKeyNumResponse.toDTO(keyNum);
    }

}
