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

import com.beanit.asn1dsrc.dsrc.C2CAuthenticatedMessage;
import com.beanit.asn1dsrc.dsrc.Initiate;
import com.beanit.asn1dsrc.dsrc.PDUs;
import com.its.app.utils.NettyUtils;
import com.its.dsrc.config.RunningConfig;
import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.xnettcp.dsrc.process.service.LoginDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

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

    public InitiateResponse(TbRseCtlr obj, ChannelHandlerContext ctx, C2CAuthenticatedMessage c2c) {
        this.obj = obj;
        this.ctx = ctx;
        this.c2c = c2c;
    }

    @Override
    public boolean response(RunningConfig runningConfig) {
        // 처리내용 없음
        String ipAddress = NettyUtils.getRemoteIpAddress(this.ctx.channel());
        log.info("InitiateResponse.response: {}", ipAddress);

        PDUs pdus = this.c2c.getPdu();
        Initiate initiate = pdus.getDatexInitiateNull();
        if (initiate != null) {
            /**
             * Login 요청에 대한 응답을 처리함
             */
            return LoginDeviceService.getInstance().requestLogin(obj, this.ctx.channel(), runningConfig);
        }

        return true;
    }

}
