package org.wcs.business.led;

import org.springframework.stereotype.Service;
import org.wcs.model.bo.tuple.Tuple2;
import org.wcs.model.pojo.led.LedBaseInfo;
import org.wcs.model.pojo.led.LedDataDetail;
import org.wcs.model.pojo.led.LedShowData;
import org.wcs.plugin.lxLed.LxLedCommunication;

import java.util.List;
import java.util.Optional;

/**
 * 灵信LED屏操作类
 */
@Service
public class LxLedOperation implements ledOperation {

    /**
     * 显示LED屏 ---- 基本方法
     * @param ledShowData 显示LED屏的数据
     * @return 显示结果
     */
    @Override
    public synchronized String showLedBase(LedShowData ledShowData) {
        try {
            LxLedCommunication.InitLedType(ledShowData.getLedType()); // 初始化卡型号
            // 创建一个节目
            long createResult = LxLedCommunication.CreateProgram(ledShowData.getLedWidth(), ledShowData.getLedHeight(), ledShowData.getColorType(), 0, 3);
            if (createResult == 0) {
                return "节目对象创建失败";
            }
            List<LedDataDetail> dataDetails = ledShowData.getDataDetails();
            if(dataDetails == null || dataDetails.isEmpty()) {
                return "显示内容为空";
            }
            for (int i = 0; i < dataDetails.size(); i++) {
                LedDataDetail dataDetail = dataDetails.get(i);
                int createAreaResult = LxLedCommunication.AddMultiLineTextToImageTextArea(createResult, 0, i, 0,
                        dataDetail.getContent(), dataDetail.getFontName(), dataDetail.getFontSize(), dataDetail.getFontColor(), 0, 0, 0,
                        dataDetail.getInStyle(), dataDetail.getNSpeed(), 0, dataDetail.getNSpeed(), dataDetail.isVCenterIs() ? 1 : 0);
                if (createAreaResult != 0) {
                    return "区域创建失败，错误信息：" + LxLedCommunication.GetErrorCodeInfo(createAreaResult);
                }
            }
            int sendResult = LxLedCommunication.NetWorkSend(ledShowData.getLedIp(), createResult);
            if (sendResult != 0) {
                return "发送失败，错误信息：" + LxLedCommunication.GetErrorCodeInfo(sendResult);
            }
            LxLedCommunication.DeleteProgram(createResult);
            return "发送成功";
        } catch (Exception e) {
            return "LED屏显示失败，异常信息：" + e.getMessage();
        }
    }
}
