package com.its.dsrc.xnettcp.dsrc.process.response;

import com.beanit.asn1dsrc.dsrc.C2CAuthenticatedMessage;
import com.its.app.AppUtils;
import com.its.app.utils.NettyUtils;
import com.its.dsrc.config.RunningConfig;
import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.entity.TbRseCtlrCnncHs;
import com.its.dsrc.process.DbmsData;
import com.its.dsrc.process.DbmsDataProcess;
import com.its.dsrc.process.DbmsDataType;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LogoutResponse implements DsrcAsn1Response {
    private TbRseCtlr obj;
    private ChannelHandlerContext ctx;
    private C2CAuthenticatedMessage c2c;

    private DbmsDataProcess dbmsDataProcess;

    public LogoutResponse(TbRseCtlr obj, ChannelHandlerContext ctx, C2CAuthenticatedMessage c2c) {
        this.obj = obj;
        this.ctx = ctx;
        this.c2c = c2c;
        this.dbmsDataProcess = (DbmsDataProcess) AppUtils.getBean(DbmsDataProcess.class);
    }

    @Override
    public boolean response(RunningConfig runningConfig) {
        String ipAddress = NettyUtils.getRemoteIpAddress(this.ctx.channel());
        log.info("LogoutResponse.response: {}", ipAddress);

        // 로그아웃 정보를 데이터베이스에 저장.
        if (this.dbmsDataProcess != null) {
            TbRseCtlrCnncHs voLog = new TbRseCtlrCnncHs();
            voLog.setRSE_CTLR_NMBR(this.obj.getID());
            voLog.setLOG_ADDRESS(NettyUtils.getRemoteIpAddress(this.ctx.channel()));
            voLog.setLoginInfo(this.c2c);
            this.dbmsDataProcess.add(new DbmsData(DbmsDataType.DBMS_DATA_LOG_HS, false, voLog));
        }
        return true;
    }
}
