package com.its.op.service.its.cctv;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class CctvControlProbeService {

    Map<String, ProbeCommand> startCmd = new HashMap<>();
    Map<String, ProbeCommand> stopCmd = new HashMap<>();
    Map<String, ProbeCommand> presetCmd = new HashMap<>();

    @PostConstruct
    void init() {
        this.startCmd.put("tilt-up",   new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=4&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x30, (byte)0x34 }));
        this.startCmd.put("tilt-down", new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=5&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x30, (byte)0x35 }));
        this.startCmd.put("pan-left",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=2&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x30, (byte)0x32 }));
        this.startCmd.put("pan-right", new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=3&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x30, (byte)0x33 }));
        this.startCmd.put("zoom-in",   new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=13&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x33 }));
        this.startCmd.put("zoom-out",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=14&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x34 }));
        this.startCmd.put("focus-in",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=15&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x35 }));
        this.startCmd.put("focus-out", new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=16&PTZCTRL.speed=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x36 }));

        this.stopCmd.put("tilt-up",   new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("tilt-down", new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("pan-left",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("pan-right", new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("zoom-in",   new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("zoom-out",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("focus-in",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));
        this.stopCmd.put("focus-out", new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x32, (byte)0x30, (byte)0x30 }));

        this.presetCmd.put("call",    new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=11&PTZCTRL.no=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x31 }));
        this.presetCmd.put("save",    new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=10&PTZCTRL.no=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30 }));
        this.presetCmd.put("delete",  new ProbeCommand("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=12&PTZCTRL.no=",
                new byte[]{ (byte)0x38, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30, (byte)0x31, (byte)0x30 }));
    }

    public String getPtzCmd(String control, String action, int speed) {
        if (StringUtils.equalsIgnoreCase("start", action)) {
            ProbeCommand command = this.startCmd.get(control);
            if (command != null) {
                return command.getUri() + speed;
            }
        } else if (StringUtils.equalsIgnoreCase("stop", action)) {
            ProbeCommand command = this.stopCmd.get(control);
            if (command != null) {
                return command.getUri();
            }
        }
        return null;
    }
    public byte[] getPtzControlCmd(String control, String action, int speed) {
        if (StringUtils.equalsIgnoreCase("start", action)) {
            ProbeCommand command = this.startCmd.get(control);
            if (command != null) {
                return command.getCmd();
            }
        } else if (StringUtils.equalsIgnoreCase("stop", action)) {
            ProbeCommand command = this.stopCmd.get(control);
            if (command != null) {
                return command.getCmd();
            }
        }
        return null;
    }

    public String getPresetCmd(String action, Integer psetNmbr) {
        ProbeCommand command = this.presetCmd.get(action);
        if (command != null) {
            return command.getUri() + psetNmbr;
        }
        return null;
    }
    public byte[] getPresetControlCmd(String action, Integer psetNmbr) {
        ProbeCommand command = this.presetCmd.get(action);
        if (command != null) {
            return command.getCmd();
        }
        return null;
    }

    public static class ProbeCommand {
        private String uri;
        private byte[] cmd;
        public ProbeCommand(String uri, byte[] cmd) {
            this.uri = uri;
            if (cmd != null && cmd.length > 0) {
                this.cmd = new byte[cmd.length];
                System.arraycopy(cmd, 0, this.cmd, 0, cmd.length);
            }
        }
        public String getUri() {
            return this.uri;
        }
        public byte[] getCmd() {
            return this.cmd;
        }
    }

}
