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

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

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

@Slf4j
@RequiredArgsConstructor
//@Service
public class CctvControlProbeService2 {

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

//   요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=2&PTZCTRL.speed=30&nRanId=60142769
//   요청 메서드: POST
//   상태 코드: 200 / O
// - 요청 헤더
//   Accept: */*
//    Accept-Encoding: gzip, deflate
//    Accept-Language: ko
//    Authorization: Basic YWRtaW46MTIzNDU=
//            Cache-Control: no-cache
//    Connection: Keep-Alive
//    Content-Length: 8
//    Content-Type: text/html; charset=UTF-8
//    Cookie: nLANGUAGE=0; nDevType=4; nChnCount=1; nRtspPort=554; nRtmpPort=1935; nMsgPort=8080; nUserLevel=4; szUsername=admin; szPassword=12345
//    Host: 172.16.53.45
//    Referer: http://172.16.53.45/login.asp
//    User-Agent: Mozilla/5.0 (
//    Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
//
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0&nRanId=72662842
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=3&PTZCTRL.speed=30&nRanId=16982543
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0&nRanId=28699669
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=4&PTZCTRL.speed=30&nRanId=40995292
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0&nRanId=52674826
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=5&PTZCTRL.speed=30&nRanId=95129168
//    요청 URL: http://172.16.53.45/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0&nRanId=62661428

    @PostConstruct
    void init() {
        this.startCmd.put("tilt-up",   new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=4&PTZCTRL.speed=", ""));
        this.startCmd.put("tilt-down", new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=5&PTZCTRL.speed=",""));
        this.startCmd.put("pan-left",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=2&PTZCTRL.speed=",""));
        this.startCmd.put("pan-right", new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=3&PTZCTRL.speed=",""));
        this.startCmd.put("zoom-in",   new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=13&PTZCTRL.speed=",""));
        this.startCmd.put("zoom-out",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=14&PTZCTRL.speed=",""));
        this.startCmd.put("focus-in",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=15&PTZCTRL.speed=",""));
        this.startCmd.put("focus-out", new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=16&PTZCTRL.speed=",""));

        this.stopCmd.put("tilt-up",   new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("tilt-down", new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("pan-left",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("pan-right", new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("zoom-in",   new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("zoom-out",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("focus-in",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));
        this.stopCmd.put("focus-out", new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=0",""));

        this.presetCmd.put("call",    new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=11&PTZCTRL.no=",""));
        this.presetCmd.put("save",    new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=10&PTZCTRL.no=",""));
        this.presetCmd.put("delete",  new ProbeCommand2("/cgi-bin/control.cgi?action=update&group=PTZCTRL&channel=0&PTZCTRL.action=12&PTZCTRL.no=",""));
    }

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

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

    public static class ProbeCommand2 {
        private String uri;
        private String cmd;
        public ProbeCommand2(String uri, String cmd) {
            this.uri = uri;
            this.cmd = cmd;
        }
        public String getUri() {
            return this.uri;
        }
        public String getCmd() {
            return this.cmd;
        }
    }

}
