from time import sleep
 from datetime import datetime, timedelta
 from decimal import Decimal
-from json import loads as json_loads
+from json import dumps as json_dumps, loads as json_loads
 from urllib.request import urlretrieve
 from uuid import uuid4
 from pathlib import Path
 
 # default configuration
 DEFAULTS = {
+    'api_key': '',
+    'allow_file_edit': True,
+    'background_color': '#ffffff',
     'host': '127.0.0.1',  # NB: to be found remotely, use '0.0.0.0'!
+    'link_originals': True,
     'port': 8090,
     'port_remote': 8090,
-    'background_color': '#ffffff',
+    'remote': '192.168.1.100',
     'queries_cutoff': '',
+    'tags_default': ['new'],
+    'tags_display_whitelist': [],
     'tags_prefilter_whitelist': [],
     'tags_prefilter_needed': [],
-    'tags_display_whitelist': [],
-    'tags_default': ['new'],
-    'allow_file_edit': True,
-    'link_originals': True
 }
 
 # type definitions for mypy
 
 class Config:
     """Collects user-configurable settings."""
+    allow_file_edit: bool
+    api_key: str
+    background_color: str
     host: str
-    remote: str
+    link_originals: bool
     port: int
     port_remote: int
-    api_key: str
-    background_color: str
-    allow_file_edit: bool
     queries_cutoff: DatetimeStr
+    remote: str
+    tags_default: TagSet
+    tags_display_whitelist: TagSet
     tags_prefilter_needed: TagSet
     tags_prefilter_whitelist: TagSet
-    tags_display_whitelist: TagSet
-    tags_default: TagSet
-    link_originals: bool
 
     def __init__(self):
         def set_attrs_from_dict(d):
                          if key.startswith('tags_')]:
             setattr(VideoFile, attr_key, getattr(self, attr_key))
 
+    def save_file(self) -> None:
+        'Write own state as JSON to PATH_CONFFILE.'
+        if PATH_CONFFILE.is_file():
+            raise HandledException(
+                    'Config file already present, not overwriting.')
+        d: dict[str, bool | int | str | list[str]] = {}
+        for key, value in [(key, getattr(self, key))
+                           for key in self.__class__.__annotations__.keys()]:
+            d[key] = value.as_str_list if isinstance(value, TagSet) else value
+        PATH_CONFFILE.write_text(json_dumps(d), encoding='utf8')
+
 
 class YoutubeQuery(DbData):
     """Representation of YouTube query (without results)."""