package com.cisdi.data.DIClient.protocol.utils;

import com.cisdi.data.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;


/**
 * @author: lhp
 * @date: 2019/10/8
 **/
public class IXComConverterUtils {
	private static final char blankspace = ' ';

	/**
	 * 数字数据：则在不满位数时左补0：
	 例如：421：   “00421”在5位数字值的情况下
	 -421：   “-0421”
	 字符串数据：则在不够长度时右补空格
	 例如：TSB：   “TSBDDDD”在7位字符串值的情况
	 * @param config
	 * @param reader
	 * @return
	 */
	public static Object convertProperty(IXComActionParamDTO.PropertyConfig config, ByteBuffer reader, Charset charset,String runid,String msgKey) {

		char[] buffer = null;

		try {
			byte[] subBody = new byte[config.getFixedLength()];
			reader.get(subBody);

			String subValue = new String(subBody, charset);
			buffer = subValue.toCharArray();
		} catch (Exception e) {
			throw new BusinessException("ixcom29d读取字符数组失败" + e.getLocalizedMessage(), e);
		}

		String value = new String(buffer);
		if(config.getPropertyType()!= IXComActionParamDTO.PropertyType.STRING&&StringUtils.isBlank(value))
		{
			return null;
		}
		try
		{
			switch (config.getPropertyType()) {
				case BYTE:
					return Byte.valueOf(value.trim());
				case SHORT:
					return Short.valueOf(value.trim());
				case INT:
					return Integer.valueOf(value.trim());
				case LONG:
					return Long.valueOf(value.trim());
				case BOOLEAN:
					value = value.trim().replaceFirst("0*", "");

					if("true".equalsIgnoreCase(value) || "1".equals(value)) {
						return true;
					}else if("false".equalsIgnoreCase(value) || "0".equals(value)) {
						return false;
					}
					break;
				case FLOAT:
				case DOUBLE:
					if(config.getPrecision() != null) {
						value=value.trim();
						BigDecimal decimal=null;
						if(config.getPrecision()!=0)
						{
							int splitIndex = value.length() - config.getPrecision();

							if(splitIndex < 0) {
								throw new BusinessException("config:" + config + ",value:" + value + ",precision error");
							}

							String prev = value.substring(0, splitIndex-1);
							String end = value.substring(splitIndex);

							decimal= new BigDecimal(prev + "." + end);
						}
						else
						{
							decimal=new BigDecimal(value);
						}

						return decimal;
					}else {
						return new BigDecimal(value);
					}
				case STRING:
					StringBuilder builder = new StringBuilder();

					int index = buffer.length;
					for (int i = buffer.length -1 ; i >= 0; i--) {

						if(buffer[i] == blankspace) {
							index = i;
							continue;
						}else {
							break;
						}
					}

					for (int j = 0; j < index; j++) {
						builder.append(buffer[j]);
					}

					return builder.toString();
				case PLACEHOLDER:
					return null;
				default:
					return null;
			}
		}
		catch (Exception e)
		{
			throw new BusinessException("连接id:" + String.valueOf(runid) + "电文号:" + msgKey +
					" 解析数据类型错误：属性ID=" + config.getPropertyId()+"属性类型="+config.getPropertyType()+"属性精度="+config.getPrecision()+"属性长度="+config.getFixedLength()+"对应的值="+value+"错误为："+e.getLocalizedMessage());
		}

		throw new BusinessException(config.toString() + "解析失败,ixcom29d");
	}
}
