import json


def via_json(video_filename, description=None):
    """
    Create a dictionary to be saved as VIA json annotation file.

    :param video_filename: relative video filename_or_buffer
    :param description: annotation description
    :return: dict
    """
    json_template = """
    {
      "project": {
        "pid": "__VIA_PROJECT_ID__",
        "rev": "__VIA_PROJECT_REV_ID__",
        "rev_timestamp": "__VIA_PROJECT_REV_TIMESTAMP__",
        "pname": "",
        "creator": "",
        "created": 1573815248007,
        "vid_list": [
          "1"
        ]
      },
      "config": {
        "file": {
          "loc_prefix": {
            "1": "",
            "2": "",
            "3": "",
            "4": ""
          }
        },
        "ui": {
          "file_content_align": "center",
          "file_metadata_editor_visible": true,
          "spatial_metadata_editor_visible": true,
          "spatial_region_label_attribute_id": ""
        }
      },
      "attribute": {},
      "file": {
        "1": {
          "fid": "1",
          "fname": "",
          "type": 4,
          "loc": 1,
          "src": ""
        }
      },
      "metadata": {},
      "view": {
        "1": {
          "fid_list": [
            "1"
          ]
        }
      }
    }
    """
    json_out = json.loads(json_template)
    json_out["file"]["1"]["fname"] = video_filename
    if description is not None:
        json_out["project"]["pname"] = description
    return json_out