#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2015 James Gardiner

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from .base import Service

"""
TODO:
  - Officer appointments

"""


class CompanySearch(Service):
    """Search for companies by name.
    """

    def __init__(self, access_token=None):
        super(CompanySearch, self).__init__()
        self.session = self.get_session(access_token)
        self.baseuri = self._BASE_URI + 'search/companies'

    def search(self,
               term,
               items_per_page=None,
               start_index=None):

        params = {
            "q": term,
            "items_per_page": items_per_page,
            "start_index": start_index
        }

        res = self.session.get(self.baseuri,
                               params=params)

        self.handle_http_error(res)

        return res


class OfficerSearch(CompanySearch):
    """Search for officers registered with companies house."""

    def __init__(self, access_token=None):
        super(OfficerSearch, self).__init__()
        self.session = self.get_session(access_token)
        self.baseuri = self._BASE_URI + 'search/officers'


class CompanyInfo(CompanySearch):
    """Search for company information by company number."""

    def __init__(self, access_token=None):
        super(CompanyInfo, self).__init__(access_token)
        self.baseuri = self._BASE_URI + 'company/'

    def get_profile(self, num):
        res = self.session.get(self.baseuri + num)
        self.handle_http_error(res)
        return res

    def get_address(self, num):
        res = self.session.get(self.baseuri +
                               num +
                               '/registered-office-address')
        self.handle_http_error(res)
        return res

    def insolvency(self, num):
        res = self.session.get(self.baseuri +
                               num +
                               '/insolvency')
        self.handle_http_error(res)
        return res

    def filing_history(self,
                       num,
                       transaction=None,
                       category=None,
                       company_number=None,
                       items_per_page=None,
                       start_index=None):

        params = {
            "category": category,
            "items_per_page": items_per_page,
            "start_index": start_index
        }

        if transaction is not None:
            res = self.session.get(self.baseuri +
                                   num +
                                   '/filing-history/' +
                                   transaction,
                                   params=params)
        else:
            res = self.session.get(self.baseuri +
                                   num +
                                   '/filing-history',
                                   params=params)
        return res

    def charges(self, num, charge_id=None, **kwargs):

        if kwargs is None:
            params = {}
        else:
            params = kwargs

        if charge_id is not None:
            res = self.session.get(self.baseuri +
                                   num +
                                   '/charges/' +
                                   charge_id,
                                   params=params)
        else:
            res = self.session.get(self.baseuri +
                                   num +
                                   '/charges',
                                   params=params)
        return res

    def get_officers(self,
                     num,
                     items_per_page=None,
                     start_index=None,
                     order_by=None):

        params = {
            "items_per_page": items_per_page,
            "start_index": start_index,
            "order_by": order_by
        }

        res = self.session.get(self.baseuri +
                               num +
                               '/officers',
                               params=params)
        return res

@@ -2423,16 +2423,43 @@
 ompany/'
+%0A        self.params = None
 %0A%0A    de
@@ -3202,263 +3202,83 @@
     
-company_number=None,%0A                       items_per_page=None,%0A                       start_index=None):%0A%0A        params = %7B%0A            %22category%22: category,%0A            %22items_per_page%22: items_per_page,%0A            %22start_index%22: start_index%0A        %7D
+**kwargs):%0A%0A        if kwargs is not None:%0A            self.params = kwargs
 %0A%0A  
@@ -3540,32 +3540,37 @@
          params=
+self.
 params)%0A        
@@ -3754,32 +3754,37 @@
          params=
+self.
 params)%0A        
@@ -3871,16 +3871,20 @@
 args is 
+not 
 None:%0A  
@@ -3897,46 +3897,13 @@
     
-params = %7B%7D%0A        else:%0A            
+self.
 para
@@ -4169,32 +4169,37 @@
          params=
+self.
 params)%0A        
@@ -4376,32 +4376,37 @@
          params=
+self.
 params)%0A        

Move params to instance variable