package com.yfd.platform.component.iec104.common;

import cn.hutool.core.util.TypeUtil;
import com.yfd.platform.component.iec104.core.Encoder104;
import com.yfd.platform.component.iec104.message.MessageDetail;
import com.yfd.platform.component.iec104.message.MessageInfo;
import com.yfd.platform.component.iec104.enums.QualifiersEnum;
import com.yfd.platform.component.iec104.enums.TypeIdentifierEnum;
import com.yfd.platform.component.iec104.enums.UControlEnum;
import com.ydl.iec.util.ByteUtil;
import com.ydl.iec.util.Iec104Util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 104 规约的基本指令封装
* @ClassName: BasicInstruction104
* @Description: 返回指定的指令
* @author YDL
 */

public class BasicInstruction104 {
	// 68040B 00 00 00

	/**
	 * 链路启动指令
	 */
	public static final byte[] STARTDT = new byte[] {0x68, 0x04, 0x07, 0x00, 0x00, 0x00};

	/**
	 * 链路启动确认指令
	 */
	public static final byte[] STARTDT_YES = new byte[] {0x68, 0x04, 0x0B, 0x00, 0x00, 0x00};

	/**
	 * 测试命令指令
	 */
	public static final byte[] TESTFR = new byte[] {0x68, 0x04, (byte) 0x43, 0x00, 0x00, 0x00};

	/**
	 * 测试回复确认
	 */
	public static final byte[] TESTFR_YES = new byte[] {0x68, 0x04, (byte) 0x83, 0x00, 0x00, 0x00};




	/**
	 * 停止确认
	 */
	public static final byte[] STOPDT_YES = new byte[] {0x68, 0x04, 0x23, 0x00, 0x00, 0x00};





	/**
	 *
	* @Title: getGeneralCallRuleDetail104
	* @Description: 总召唤指令
	* @param @return
	* @param @throws IOException
	* @return MessageDetail
	* @throws
	 */
	public static MessageDetail getGeneralCallRuleDetail104() throws IOException {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		int sq = 0;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 0;
		// 发送序号
		short send = 0;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 6;
		boolean isTest = false;
		boolean isPn = true;
		// 终端地址 实际发生的时候会被替换
		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;
		QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageInfo messageInfo=new MessageInfo();
		byte[] message=new byte[]{0x00, 0x00, 0x00, 0x00};
		messageInfo.setMessageInfos(message);
		messages.add(messageInfo);
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPn, transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);

		byte[] bytes = Encoder104.encoder(ruleDetail104);
		ruleDetail104.setHexString(ByteUtil.byteArrayToHexString(bytes));
		return ruleDetail104;
	}


	/**
	 *
	* @Title: getYesGeneralCallRuleDetail104
	* @Description: 总召唤确认指令
	* @return
	* @return MessageDetail
	* @throws
	 */
	public static MessageDetail getYesGeneralCallRuleDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		 //SQ=0 length =1
		int sq = 0;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 0;
		// 发送序号
		short send = 0;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 7;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;

		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;

		QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN, transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}


	/**
	 *
	* @Title: getEndGeneralCallRuleDetail104
	* @Description: 总召唤结束指令
	* @return
	* @return MessageDetail
	* @throws
	 */
	public static MessageDetail getEndGeneralCallRuleDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		 //SQ=0 length =1
		int sq = 0;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 1;
		// 发送序号
		short send = 4;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 0x0A;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;

		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;
		// 老板限定词
		QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN, transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	public static MessageDetail getInitRuleDetail104() {
		byte[] control = ByteUtil.intToByteArray(UControlEnum.STARTDT.getValue());
		MessageDetail ruleDetail104 = new MessageDetail(control);
		return ruleDetail104;
	}

	/**
	 *
	 * @Title: gettTimeScale104
	 * @Description: 对时
	 * @param @return
	 * @param @throws IOException
	 * @return MessageDetail
	 * @throws
	 */
	public static MessageDetail getTimeScale104() {

		// 接收序号
		short accept = 0;
		// 发送序号
		short send = 0;
		//控制欲
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 消息地址 总召唤地址为0
		int messageAddress = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		Calendar calendar = Calendar.getInstance();
		Date timeScale  = calendar.getTime();
        //对时没有限定词
//		QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageInfo message = new MessageInfo();
		message.setMessageInfos(new byte[] {0x01});
		//类型标识 对时同步
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.timeSynchronization;

		int sq = 0;
		//可变结构限定词 01
		boolean isContinuous = sq == 0 ? false : true;

		// 传输原因
		short transferReason = 6;
		// 终端地址 实际发生的时候会被替换 公共地址
		short terminalAddress = 1;

		boolean isTest = false;
		boolean isPn = true;

		message.setMessageAddress(messageAddress);
		messages.add(message);
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPn, transferReason,
				terminalAddress, messageAddress, messages, timeScale,null);
		return ruleDetail104;
	}
}
