package com.its.bis.config;

import com.its.app.AppUtils;
import com.its.app.utils.StringUtils;
import com.its.bis.service.AppRepositoryService;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

@Slf4j
@Getter
@Setter
@ToString
@Component
public class DebugConfig {

    private boolean isDebug = false;

    @PostConstruct
    private void init() {
        loadDefaultInfo();
    }


    public void loadDefaultInfo() {
        try {
            String workingDir = System.getProperty("user.dir");
            FileInputStream in = null;
            try {
                in = new FileInputStream(workingDir + "/conf/debug.properties");
            }
            catch(Exception e) {
                log.error("{}.loadDebugInfo: Exception1: {}", this.getClass().getSimpleName(), e.toString());
                return;
            }

            Properties props = new Properties();
            props.load(in);
            in.close();

            String isDebug  = props.getProperty("system-debug",  "false").trim();
            this.isDebug = "true".equalsIgnoreCase(isDebug);
        }
        catch(Exception e) {
            log.error("{}.loadDebugInfo: Exception2: {}", this.getClass().getSimpleName(), e.toString());
        }
    }

    public void loadDebugInfo() {
        try
        {
            String workingDir = System.getProperty("user.dir");
            FileInputStream in = null;
            try {
                in = new FileInputStream(workingDir + "/conf/debug.properties");
            }
            catch(Exception e) {
                log.error("{}.loadDebugInfo: Exception1: {}", this.getClass().getSimpleName(), e.toString());
                return;
            }

            Properties props = new Properties();
            props.load(in);
            in.close();

            String isDebug  = props.getProperty("system-debug",  "false").trim();
            this.isDebug = "true".equalsIgnoreCase(isDebug);

            AppRepositoryService repoService = (AppRepositoryService) AppUtils.getBean(AppRepositoryService.class);
//            repoService.getCtlrMap().forEach((ctlrNmbr, vmsObj) -> {
//                vmsObj.getDebug().init();
//            });

            String packetDump  = props.getProperty("packet-dump",  "x").trim();
            if (!packetDump.equals("x")) {
                List<String> packetDumps = StringUtils.split(packetDump, ",");
                packetDumps.forEach(id -> {
//                    Long vmsCtlrNmbr = Long.parseLong(id);
//                    TbVmsCtlrDto vmsObj = repoService.getCtlrMap(vmsCtlrNmbr);
//                    if (vmsObj != null) {
//                        vmsObj.getDebug().setDump(true);
//                    }
                });
            }
            String packetInfo  = props.getProperty("packet-info",  "x").trim();
            if (!packetInfo.equals("x")) {
                List<String> packetInfos = StringUtils.split(packetInfo, ",");
                packetInfos.forEach(id -> {
//                    Long vmsCtlrNmbr = Long.parseLong(id);
//                    TbVmsCtlrDto vmsObj = repoService.getCtlrMap(vmsCtlrNmbr);
//                    if (vmsObj != null) {
//                        vmsObj.getDebug().setDebug(true);
//                    }
                });
            }
        }
        catch(Exception e) {
            log.error("{}.loadDebugInfo: Exception2: {}", this.getClass().getSimpleName(), e.toString());
        }
    }

    public void saveDebugInfo() {
        try
        {
            final String[] packetDump = {""};
            final String[] packetInfo = {""};

            AppRepositoryService repoService = (AppRepositoryService) AppUtils.getBean(AppRepositoryService.class);
//            repoService.getCtlrMap().forEach((ctlrNmbr, vmsObj) -> {
//                if (vmsObj.getDebug().isDump()) {
//                    if (!packetDump[0].equals("")) {
//                        packetDump[0] += ("," + vmsObj.getVmsCtlrNmbr());
//                    }
//                    else {
//                        packetDump[0] = String.valueOf(vmsObj.getVmsCtlrNmbr());
//                    }
//                }
//                if (vmsObj.getDebug().isDebug()) {
//                    if (!packetInfo[0].equals("")) {
//                        packetInfo[0] += ("," + vmsObj.getVmsCtlrNmbr());
//                    }
//                    else {
//                        packetInfo[0] = String.valueOf(vmsObj.getVmsCtlrNmbr());
//                    }
//                }
//            });

            if (packetDump[0].equals("")) {
                packetDump[0] = "x";
            }
            if (packetInfo[0].equals("")) {
                packetInfo[0] = "x";
            }

            Properties props = new Properties();

            props.setProperty("packet-dump", packetDump[0]);
            props.setProperty("packet-info", packetInfo[0]);
            props.setProperty("system-debug", this.isDebug ? "true" : "false");

            String workingDir = System.getProperty("user.dir");
            File file = new File(workingDir + "/conf/debug.properties");
            try {
                FileWriter writer = new FileWriter(file);
                props.store(writer,"system debug setting configuration...");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        catch(Exception e) {
            log.error("{}.saveDebugInfo: Exception: {}", this.getClass().getSimpleName(), e.toString());
        }
    }

}
