package com.screenshot.config;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
public class AppConfig {
private static final String CONFIG_FILE = "config.properties";
private final Properties properties;
public AppConfig() {
properties = new Properties();
loadConfig();
}
private void loadConfig() {
Path configPath = Path.of(CONFIG_FILE);
if (Files.exists(configPath)) {
try (FileInputStream fis = new FileInputStream(CONFIG_FILE)) {
properties.load(fis);
} catch (IOException e) {
System.err.println("Ошибка загрузки конфигурации: " + e.getMessage());
setDefaults();
}
} else {
setDefaults();
saveConfig();
}
}
private void setDefaults() {
// Скриншоты
properties.setProperty("save.path", System.getProperty("user.home") + "/Screenshots");
properties.setProperty("schedule.cron", "0 * * * * *");
properties.setProperty("target.window", "");
properties.setProperty("schedule.interval.minutes", "1");
properties.setProperty("use.cron", "false");
// Отправка
properties.setProperty("upload.server.url", "http://localhost:8080/api/upload");
properties.setProperty("upload.schedule.time", "23:00");
properties.setProperty("upload.enabled", "true");
properties.setProperty("upload.max.zip.size.mb", "50");
properties.setProperty("upload.retry.count", "3");
properties.setProperty("upload.retry.delay.seconds", "30");
properties.setProperty("upload.delete.after.success", "false");
}
public void saveConfig() {
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE)) {
properties.store(fos, "Screenshot Application Configuration");
} catch (IOException e) {
System.err.println("Ошибка сохранения конфигурации: " + e.getMessage());
}
}
// === Геттеры/Сеттеры для скриншотов ===
public String getSavePath() {
return properties.getProperty("save.path");
}
public void setSavePath(String path) {
properties.setProperty("save.path", path);
}
public String getCronExpression() {
return properties.getProperty("schedule.cron");
}
public void setCronExpression(String cron) {
properties.setProperty("schedule.cron", cron);
}
public String getTargetWindow() {
return properties.getProperty("target.window", "");
}
public void setTargetWindow(String windowTitle) {
properties.setProperty("target.window", windowTitle);
}
public int getIntervalMinutes() {
return Integer.parseInt(properties.getProperty("schedule.interval.minutes", "1"));
}
public void setIntervalMinutes(int minutes) {
properties.setProperty("schedule.interval.minutes", String.valueOf(minutes));
}
public boolean useCron() {
return Boolean.parseBoolean(properties.getProperty("use.cron", "false"));
}
public void setUseCron(boolean useCron) {
properties.setProperty("use.cron", String.valueOf(useCron));
}
// === Геттеры/Сеттеры для отправки ===
public String getUploadServerUrl() {
return properties.getProperty("upload.server.url");
}
public void setUploadServerUrl(String url) {
properties.setProperty("upload.server.url", url);
}
public LocalTime getUploadScheduleTime() {
String time = properties.getProperty("upload.schedule.time", "23:00");
return LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm"));
}
public void setUploadScheduleTime(LocalTime time) {
properties.setProperty("upload.schedule.time",
time.format(DateTimeFormatter.ofPattern("HH:mm")));
}
public boolean isUploadEnabled() {
return Boolean.parseBoolean(properties.getProperty("upload.enabled", "true"));
}
public void setUploadEnabled(boolean enabled) {
properties.setProperty("upload.enabled", String.valueOf(enabled));
}
public long getMaxZipSizeBytes() {
int mb = Integer.parseInt(properties.getProperty("upload.max.zip.size.mb", "50"));
return mb * 1024L * 1024L;
}
public void setMaxZipSizeMb(int mb) {
properties.setProperty("upload.max.zip.size.mb", String.valueOf(mb));
}
public int getUploadRetryCount() {
return Integer.parseInt(properties.getProperty("upload.retry.count", "3"));
}
public void setUploadRetryCount(int count) {
properties.setProperty("upload.retry.count", String.valueOf(count));
}
public int getUploadRetryDelaySeconds() {
return Integer.parseInt(properties.getProperty("upload.retry.delay.seconds", "30"));
}
public void setUploadRetryDelaySeconds(int seconds) {
properties.setProperty("upload.retry.delay.seconds", String.valueOf(seconds));
}
public boolean isDeleteAfterUpload() {
return Boolean.parseBoolean(properties.getProperty("upload.delete.after.success", "false"));
}
public void setDeleteAfterUpload(boolean delete) {
properties.setProperty("upload.delete.after.success", String.valueOf(delete));
}
}