# -*- coding: utf-8 -*-

from json import JSONEncoder

from ._PosVersions import PosVersions
from ._UpdateControl import UpdateControl


class ModelJsonEncoder(JSONEncoder):
    def default(self, o):
        if isinstance(o, PosVersions):
            return (
                {
                    "versions": {
                        "catalog": o.catalog,
                        "user": o.user,
                        "src": o.src,
                        "core": o.core,
                        "loader": o.loader,
                        "binaries": o.binaries
                    },
                    "dates": {
                        "catalog": o.catalog_date,
                        "user": o.user_date,
                        "src": o.src_date,
                        "core": o.core_date,
                        "loader": o.loader_date,
                        "binaries": o.binaries_date
                    },
                    "updateControl": o.update_control,
                    "menuVersion": o.menu_version,
                    "menuName": o.menu_name,
                }
            )

        if isinstance(o, UpdateControl):
            return (
                {
                    "binaries": o.binaries,
                    "catalog": o.catalog,
                    "loader": o.loader,
                    "user": o.user,
                    "updating": o.updating
                }
            )

        return super(ModelJsonEncoder, self).default(o)
