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.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.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;



    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.setAfflShopId(jwtTokenProvider.getAfflShopId(connKey));
            memberEntity.setBrandId(jwtTokenProvider.getBrandId(connKey));
            memberEntity.setAuthTpNm(jwtTokenProvider.getAuthTpNm(connKey));


        } 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),
                afflService.selectTargetAfflGridList(memberEntity.getUserId(), memberEntity.getAuthTpCd()),
                brandService.selectTargetBrandGridList(memberEntity.getUserId(), memberEntity.getAuthTpCd()));
    }

    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.setAfflShopId(jwtTokenProvider.getAfflShopId(connKey));
            memberEntity.setBrandId(jwtTokenProvider.getBrandId(connKey));
            memberEntity.setAuthTpNm(jwtTokenProvider.getAuthTpNm(connKey));


        } 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),
                afflService.selectTargetAfflGridList(memberEntity.getUserId(), memberEntity.getAuthTpCd()),
                brandService.selectTargetBrandGridList(memberEntity.getUserId(), memberEntity.getAuthTpCd()));
    }

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

}
