# Leviton Cloud Services API model App.
# Auto-generated by api_scraper.py.
#
# Copyright 2017 Tim Lyakhovetskiy <tlyakhov@gmail.com>
#
# This code is released under the terms of the MIT license. See the LICENSE
# file for more details.
from decora_wifi.base_model import BaseModel


class App(BaseModel):
    def __init__(self, session, model_id=None):
        super(App, self).__init__(session, model_id)

    @classmethod
    def count(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/count"
        return session.call_api(api, attribs, 'get')

    def count_organizations(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations/count".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    def count_preferences(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences/count".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    def count_subscription_plans(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans/count".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    def count_updates(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates/count".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    @classmethod
    def create(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps"
        return session.call_api(api, attribs, 'post')

    @classmethod
    def create_many(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps"
        return session.call_api(api, attribs, 'post')

    def create_organizations(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations".format(self._id)
        return self._session.call_api(api, attribs, 'post')

    def create_preferences(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences".format(self._id)
        return self._session.call_api(api, attribs, 'post')

    def create_subscription_plans(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans".format(self._id)
        return self._session.call_api(api, attribs, 'post')

    def create_updates(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates".format(self._id)
        return self._session.call_api(api, attribs, 'post')

    def delete_by_id(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}".format(self._id)
        return self._session.call_api(api, attribs, 'delete')

    def delete_organizations(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations".format(self._id)
        return self._session.call_api(api, attribs, 'delete')

    def delete_preferences(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences".format(self._id)
        return self._session.call_api(api, attribs, 'delete')

    def delete_subscription_plans(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans".format(self._id)
        return self._session.call_api(api, attribs, 'delete')

    def delete_updates(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates".format(self._id)
        return self._session.call_api(api, attribs, 'delete')

    def destroy_by_id_organizations(self, organization_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations/{1}".format(self._id, organization_id)
        return self._session.call_api(api, attribs, 'delete')

    def destroy_by_id_preferences(self, preference_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences/{1}".format(self._id, preference_id)
        return self._session.call_api(api, attribs, 'delete')

    def destroy_by_id_subscription_plans(self, subscription_plan_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans/{1}".format(self._id, subscription_plan_id)
        return self._session.call_api(api, attribs, 'delete')

    def destroy_by_id_updates(self, update_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates/{1}".format(self._id, update_id)
        return self._session.call_api(api, attribs, 'delete')

    def exists(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/exists".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    @classmethod
    def find(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps"
        items = session.call_api(api, attribs, 'get')

        result = []
        if items is not None:
            for data in items:
                model = App(session, data['id'])
                model.data = data
                result.append(model)
        return result

    def find_by_id(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}".format(self._id)
        data = self._session.call_api(api, attribs, 'get')

        self.data.update(data)
        return self

    def find_by_id_organizations(self, organization_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations/{1}".format(self._id, organization_id)
        data = self._session.call_api(api, attribs, 'get')

        from .organization import Organization
        model = Organization(self._session, data['id'])
        model.data = data
        return model

    def find_by_id_preferences(self, preference_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences/{1}".format(self._id, preference_id)
        data = self._session.call_api(api, attribs, 'get')

        from .preference import Preference
        model = Preference(self._session, data['id'])
        model.data = data
        return model

    def find_by_id_subscription_plans(self, subscription_plan_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans/{1}".format(self._id, subscription_plan_id)
        return self._session.call_api(api, attribs, 'get')

    def find_by_id_updates(self, update_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates/{1}".format(self._id, update_id)
        return self._session.call_api(api, attribs, 'get')

    @classmethod
    def find_one(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/findOne"
        return session.call_api(api, attribs, 'get')

    def refresh(self):
        api = "/Apps/{0}".format(self._id)
        result = self._session.call_api(api, {}, 'get')
        if result is not None:
            self.data.update(result)
        return self

    def get_organizations(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations".format(self._id)
        items = self._session.call_api(api, attribs, 'get')

        from .organization import Organization
        result = []
        if items is not None:
            for data in items:
                model = Organization(self._session, data['id'])
                model.data = data
                result.append(model)
        return result

    def get_preferences(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences".format(self._id)
        items = self._session.call_api(api, attribs, 'get')

        from .preference import Preference
        result = []
        if items is not None:
            for data in items:
                model = Preference(self._session, data['id'])
                model.data = data
                result.append(model)
        return result

    def get_subscription_plans(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    def get_updates(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates".format(self._id)
        return self._session.call_api(api, attribs, 'get')

    def replace_by_id(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/replace".format(self._id)
        return self._session.call_api(api, attribs, 'post')

    @classmethod
    def replace_or_create(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/replaceOrCreate"
        return session.call_api(api, attribs, 'post')

    @classmethod
    def update_all(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/update"
        return session.call_api(api, attribs, 'post')

    def update_attributes(self, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}".format(self._id)
        data = self._session.call_api(api, attribs, 'put')

        self.data.update(attribs)
        return self

    def update_by_id_organizations(self, organization_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/organizations/{1}".format(self._id, organization_id)
        data = self._session.call_api(api, attribs, 'put')

        from .organization import Organization
        model = Organization(self._session, data['id'])
        model.data = data
        return model

    def update_by_id_preferences(self, preference_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/preferences/{1}".format(self._id, preference_id)
        data = self._session.call_api(api, attribs, 'put')

        from .preference import Preference
        model = Preference(self._session, data['id'])
        model.data = data
        return model

    def update_by_id_subscription_plans(self, subscription_plan_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/subscriptionPlans/{1}".format(self._id, subscription_plan_id)
        return self._session.call_api(api, attribs, 'put')

    def update_by_id_updates(self, update_id, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/{0}/updates/{1}".format(self._id, update_id)
        return self._session.call_api(api, attribs, 'put')

    @classmethod
    def upsert(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps"
        data = session.call_api(api, attribs, 'put')

        model = App(session, data['id'])
        model.data = data
        return model

    @classmethod
    def upsert_with_where(cls, session, attribs=None):
        if attribs is None:
            attribs = {}
        api = "/Apps/upsertWithWhere"
        return session.call_api(api, attribs, 'post')

