package com.signal.sig.comm;

import java.util.ArrayList;
import java.util.List;

import com.signal.sig.ws.SigWebSocketClientMap;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class SigCommDecoder extends ByteToMessageDecoder {
	public SigCommDecoder(){}
	
	public static List<Object> outList;

	public static int UnisignedIntToInt(long srcValue) {
		return (int)(srcValue & 0xFFFFFFFF);
	}
	
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> objList) throws Exception {
        if (byteBuf.readableBytes() < 16) {
        	System.out.println("헤더사이즈만큼 읽지 못함: " + byteBuf.readableBytes());
        	return;
        }
        
        int stx    = byteBuf.readUnsignedByte();
        int opcode = byteBuf.readUnsignedByte();
        int year   = byteBuf.readUnsignedByte();
        int mon    = byteBuf.readUnsignedByte();
        int day    = byteBuf.readUnsignedByte();
        int hour   = byteBuf.readUnsignedByte();
        int min    = byteBuf.readUnsignedByte();
        int sec    = byteBuf.readUnsignedByte();
        int seq1   = byteBuf.readUnsignedByte();
        int seq2   = byteBuf.readUnsignedByte();
        int r_id1  = byteBuf.readUnsignedByte();
        int r_id2  = byteBuf.readUnsignedByte();
        int len1   = byteBuf.readUnsignedByte();
        int len2   = byteBuf.readUnsignedByte();
        int count1 = byteBuf.readUnsignedByte();
        int count2 = byteBuf.readUnsignedByte();
        int regionId = r_id1*256+r_id2;
        
        if (stx != 2) {
        	/// 소켓 연결 종료 시킴...
        	System.out.println("[ DBUG ]STX Error readerIndex_2: " + byteBuf.readerIndex() + ",  Remain: " + byteBuf.readableBytes() + ", STX: " + stx);
        	return;
        }
        int pktLen = len1*256+len2;
        pktLen += 2;
        if (byteBuf.readableBytes() < pktLen) 
        {
        	byteBuf.resetReaderIndex();
        	return;
        }
        
        //ByteBuf dataBuf = byteBuf.readBytes(pktLen);
        ByteBuf dataBuf = byteBuf.readSlice(pktLen);
        byteBuf.markReaderIndex();
        
        int pktCnt = (count1*256)+count2;
        String sOpCode="";
        String sDataCnt="";
        String sData = "";
        if (opcode == 161)
        {
        	sOpCode = "A";
        	sDataCnt = Integer.toString(pktCnt);
        	
        	System.out.println("SIG_PHASE_CHANGE: " + pktCnt + " EA*****************************************************************");
        	for (int ii = 0; ii < pktCnt; ii++)
        	{
        		int int_no1 = dataBuf.readUnsignedByte();
        		int int_no2 = dataBuf.readUnsignedByte();
        		int grp_id1 = dataBuf.readUnsignedByte();
        		int grp_id2 = dataBuf.readUnsignedByte();
        		int mode    = dataBuf.readUnsignedByte();
        		int ring_a  = (dataBuf.readUnsignedByte() >> 5) & 0x07;
        		int ring_b  = (dataBuf.readUnsignedByte() >> 5) & 0x07;
        		int status  = dataBuf.readUnsignedByte();
        		int count   = dataBuf.readUnsignedByte();
        		int cycle   = dataBuf.readUnsignedByte();
        		int phase_a1=dataBuf.readUnsignedByte();
        		int phase_a2=dataBuf.readUnsignedByte();
        		int phase_a3=dataBuf.readUnsignedByte();
        		int phase_a4=dataBuf.readUnsignedByte();
        		int phase_a5=dataBuf.readUnsignedByte();
        		int phase_a6=dataBuf.readUnsignedByte();
        		int phase_a7=dataBuf.readUnsignedByte();
        		int phase_a8=dataBuf.readUnsignedByte();
    			int phase_b1=dataBuf.readUnsignedByte();
    			int phase_b2=dataBuf.readUnsignedByte();
    			int phase_b3=dataBuf.readUnsignedByte();
    			int phase_b4=dataBuf.readUnsignedByte();
    			int phase_b5=dataBuf.readUnsignedByte();
    			int phase_b6=dataBuf.readUnsignedByte();
    			int phase_b7=dataBuf.readUnsignedByte();
    			int phase_b8=dataBuf.readUnsignedByte();
    			    			
    			sData += "|"+( regionId+","+((int_no1*256)+int_no2)+","+((grp_id1*256)+grp_id2)+","+mode+","+ring_a+","+ring_b+","+status+","+count+","+cycle+","+
				phase_a1+","+phase_a2+","+phase_a3+","+phase_a4+","+phase_a5+","+phase_a6+","+phase_a7+","+phase_a8+","+phase_b1+","+phase_b2+","+phase_b3+","+
				phase_b4+","+phase_b5+","+phase_b6+","+phase_b7+","+phase_b8 );
    			
        	}
        }
        else if (opcode == 162)
        {
        	sOpCode = "B";
        	sDataCnt = Integer.toString(pktCnt);
        	for (int ii = 0; ii < pktCnt; ii++)
        	{
        		int int_no1 = dataBuf.readUnsignedByte();
        		int int_no2 = dataBuf.readUnsignedByte();
        		int mode    = dataBuf.readUnsignedByte();
        		int ring_a  = (dataBuf.readUnsignedByte() >> 5) & 0x07;
        		int ring_b  = (dataBuf.readUnsignedByte() >> 5) & 0x07;
        		int status  = dataBuf.readUnsignedByte();
        		int count   = dataBuf.readUnsignedByte();
        		int cycle   = dataBuf.readUnsignedByte();
        		sData += "|"+( regionId+","+((int_no1*256)+int_no2)+","+mode+","+ring_a+","+ring_b+","+status+","+count+","+cycle );
        	}
        }
        else
        {
            return;
        }
        
        System.out.println("receive SigData : " + sOpCode+"|"+sDataCnt+sData);
        
        SigWebSocketClientMap.sendBroadCast(sOpCode+"|"+sDataCnt+sData);
        
        //dataBuf.discardReadBytes().clear();
        //byteBuf.discardReadBytes().clear();
        
	}

}