# Path: pykintone/application_settings/base_administration_api.py
# class BaseAdministrationAPI(BaseAPI):
# 
#     def __init__(self, account, api_token="", requests_options=(), app_id=""):
#         super(BaseAdministrationAPI, self).__init__(account, api_token, requests_options, app_id)
#         self.__test_mode = False
#         self._commit_revision = -1
#         self._cached_changes = False
# 
#     def as_test_mode(self):
#         self.__test_mode = True
#         return self
# 
#     def _request(self, method, url, params_or_data, headers=None, use_api_token=True):
#         result = super(BaseAdministrationAPI, self)._request(method, url, params_or_data, headers, use_api_token)
#         if method in ("POST", "PUT", "DELETE"):
#             self._cached_changes = True
#         return result
# 
#     def __enter__(self):
#         self._commit_revision = -1
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         if self._cached_changes and not self.app_id:
#             raise Exception("There are some changes to be committed, but no application id.")
#         elif not (self._cached_changes and self.app_id):
#             return None
# 
#         admin = self.__get_admin()
#         result = None
#         exit_type = "Commit"
#         if exc_type is None and not self.__test_mode:
#             result = admin.commit_settings(self.app_id, self._commit_revision)
#         else:
#             exit_type = "Rollback"
#             result = admin.rollback_settings(self.app_id, self._commit_revision)
# 
#         if result.ok:
#             self._commit_revision = -1
#             self._cached_changes = False
#         else:
#             raise Exception(exit_type + " failed. {0}".format(result.message))
# 
#     def __get_admin(self):
#         from pykintone.application_settings.administrator import Administrator
#         admin = Administrator(self.account, self.api_token, self.requests_options, self.app_id)
#         return admin


import json
import requests
import pykintone.structure as ps
import pykintone.application_settings.setting_result as sr
from pykintone.application_settings.base_administration_api import BaseAdministrationAPI
                from pykintone.account import kintoneService
        from time import sleep
        from pykintone.application_settings.general_settings import GeneralSettingsAPI
        from pykintone.application_settings.form import FormAPI
        from pykintone.application_settings.view import ViewAPI
        # this api is preview
        url = "https://{0}.cybozu.com/k/v1/preview/app/deploy.json".format(self.account.domain)

        headers = self.account.to_header()  # you can not use api token when create the application.
        params = {
            "apps": app_ids
        }

        r = requests.get(url, headers=headers, data=json.dumps(params), **self.requests_options)
        return sr.DeployProgressResult(r)

    def general_settings(self):
        return GeneralSettingsAPI(self.account, self.api_token, self.requests_options, self.app_id)

    def form(self):
        return FormAPI(self.account, self.api_token, self.requests_options, self.app_id)

    def view(self):
        return ViewAPI(self.account, self.api_token, self.requests_options, self.app_id)


class ApplicationInformation(ps.kintoneStructure):

    def __init__(self):
        super(ApplicationInformation, self).__init__()
        self.app_id = ""
        # self.code = "" # nothing is set
        self.name = ""
        self.description = ""
        self.space_id = ""

# Path: pykintone/application_settings/base_administration_api.py
# class BaseAdministrationAPI(BaseAPI):
# 
#     def __init__(self, account, api_token="", requests_options=(), app_id=""):
#         super(BaseAdministrationAPI, self).__init__(account, api_token, requests_options, app_id)
#         self.__test_mode = False
#         self._commit_revision = -1
#         self._cached_changes = False
# 
#     def as_test_mode(self):
#         self.__test_mode = True
#         return self
# 
#     def _request(self, method, url, params_or_data, headers=None, use_api_token=True):
#         result = super(BaseAdministrationAPI, self)._request(method, url, params_or_data, headers, use_api_token)
#         if method in ("POST", "PUT", "DELETE"):
#             self._cached_changes = True
#         return result
# 
#     def __enter__(self):
#         self._commit_revision = -1
#         return self
# 
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         if self._cached_changes and not self.app_id:
#             raise Exception("There are some changes to be committed, but no application id.")
#         elif not (self._cached_changes and self.app_id):
#             return None
# 
#         admin = self.__get_admin()
#         result = None
#         exit_type = "Commit"
#         if exc_type is None and not self.__test_mode:
#             result = admin.commit_settings(self.app_id, self._commit_revision)
#         else:
#             exit_type = "Rollback"
#             result = admin.rollback_settings(self.app_id, self._commit_revision)
# 
#         if result.ok:
#             self._commit_revision = -1
#             self._cached_changes = False
#         else:
#             raise Exception(exit_type + " failed. {0}".format(result.message))
# 
#     def __get_admin(self):
#         from pykintone.application_settings.administrator import Administrator
#         admin = Administrator(self.account, self.api_token, self.requests_options, self.app_id)
#         return admin


# Path: pykintone/application_settings/administrator.py
import json
import requests
import pykintone.structure as ps
import pykintone.application_settings.setting_result as sr
from pykintone.application_settings.base_administration_api import BaseAdministrationAPI
                from pykintone.account import kintoneService
        from time import sleep
        from pykintone.application_settings.general_settings import GeneralSettingsAPI
        from pykintone.application_settings.form import FormAPI
        from pykintone.application_settings.view import ViewAPI

        # this api is preview
        url = "https://{0}.cybozu.com/k/v1/preview/app/deploy.json".format(self.account.domain)

        headers = self.account.to_header()  # you can not use api token when create the application.
        params = {
            "apps": app_ids
        }

        r = requests.get(url, headers=headers, data=json.dumps(params), **self.requests_options)
        return sr.DeployProgressResult(r)

    def general_settings(self):
        return GeneralSettingsAPI(self.account, self.api_token, self.requests_options, self.app_id)

    def form(self):
        return FormAPI(self.account, self.api_token, self.requests_options, self.app_id)

    def view(self):
        return ViewAPI(self.account, self.api_token, self.requests_options, self.app_id)


class ApplicationInformation(ps.kintoneStructure):

    def __init__(self):
        super(ApplicationInformation, self).__init__()
        self.app_id = ""
        # self.code = "" # nothing is set
        self.name = ""
        self.description = ""
        self.space_id = ""

        self.thread_id = ""