package com.bonait.bnframework.HBL.Communication.Modbus;

import com.bonait.bnframework.HBL.Logs.MessageLog;
import com.licheedev.modbus4android.ModbusRespException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;

import java.util.concurrent.ExecutionException;

public class ExceptionServer {

    public ExceptionServer(String Address, int length,IExceptionHandling excpet)
    {
        try {
            int add = GetAddress(Address);
            if (add < 0) {
                MessageLog.ShowError("地址解析失败=" + Address);
                return;
            }
            if (excpet!=null) excpet.onSuccess(add);
        } catch (InterruptedException e) {
            MessageLog.ShowError("onFailure，Address=" + Address + ",length=" + length + ",msg:" + e.toString());
            if (excpet!=null) excpet.onFailure(e.toString());
        } catch (ExecutionException e) {
            MessageLog.ShowError("onFailure，Address=" + Address + ",length=" + length + ",msg:" + e.toString());
            if (excpet!=null) excpet.onFailure(e.toString());
        } catch (ModbusTransportException e) {
            MessageLog.ShowError("onFailure，Address=" + Address + ",length=" + length + ",msg:" + e.toString());
            if (excpet!=null) excpet.onFailure(e.toString());
        } catch (ModbusInitException e) {
            MessageLog.ShowError("onFailure，Address=" + Address + ",length=" + length + ",msg:" + e.toString());
            if (excpet!=null) excpet.onFailure(e.toString());
        } catch (ModbusRespException e) {
            MessageLog.ShowError("onFailure，Address=" + Address + ",length=" + length + ",msg:" + e.toString());
            if (excpet!=null) excpet.onFailure(e.toString());
        }
    }

    public int GetAddress(String address) {
        if (address == null) return -1;
        if (address.length() > 0) {
            address = address.trim();
            try{
                if (address.toUpperCase().contains("M") &&address.toUpperCase().contains(".")&& address.length() >= 4) {
                    String[] res = address.substring(1).split("[.]");
                    if (res != null && res.length == 2) {
                            int firstAdd = Integer.parseInt(res[0]);
                            int endAdd = Integer.parseInt(res[1]);
                            if (endAdd >= 0 && endAdd <= 7) {
                                return (firstAdd * 8) + 320 + endAdd;
                            }
                    }
                } else if (address.toUpperCase().contains("I") && address.length() >= 4) {
                    String[] res = address.substring(1).split("[.]");
                    if (res != null && res.length == 2) {
                        int firstAdd = Integer.parseInt(res[0]);
                        int endAdd = Integer.parseInt(res[1]);
                        if (endAdd >= 0 && endAdd <= 7) {
                            return (firstAdd * 8) + endAdd;
                        }
                    }
                } else if ((address.toUpperCase().contains("VW") || address.toUpperCase().contains("VD")) && address.length() >= 3) {
                    String res = address.substring(2);
                    if (res != null) {
                        int tempAdd = Integer.parseInt(res);
                        return (tempAdd / 2) + 100;
                    }
                }else if (address.toUpperCase().contains("M")&&address.length() >=2){
                    return Integer.parseInt(address.substring(1))+4000;
                }else if (address.toUpperCase().contains("X")&&address.length() >=2){
                    int xadd = Integer.parseInt( address.substring(1));
                    return (xadd / 10 * 8) + xadd % 10;
                }else if (address.toUpperCase().contains("Y")&&address.length() >=2){
                   int yadd = Integer.parseInt( address.substring(1));
                   return ((yadd / 10 * 8) + yadd % 10) + 300;
                }else if (address.toUpperCase().contains("D")&&address.length() >=2){
                    return Integer.parseInt( address.substring(1));
                }
                else {
                    return Integer.parseInt(address);
                }
            }catch (Exception e){
                MessageLog.ShowError(e);
            }
        }
        return -1;
    }
}
