#
# Copyright 2013, Couchbase, Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
This module contains the core functionality of '_libcouchbase'. In short,
this contains the convergence between the C module and code written in Python.

While the _libcouchbase module should never be used directly, in the off chance
that this does happen, ensure this module is loaded as well before anything is
done, otherwise Bad Things May Happen.

Additionally, this
module contains python functions used exclusively from C. They are here
because it was quicker to write them in Python than it was in C. Do not touch
this file at all. You have been warned
"""
import json
import pickle

import couchbase.exceptions as E
import couchbase._libcouchbase as C

def _result__repr__(self):
    """
    This is used as the `__repr__` function for the :class:`Result`
    """

    details = []
    flags = self.__class__._fldprops

    rcstr = "RC=0x{0:X}".format(self.rc)
    if self.rc != 0:
        rcstr += "[{0}]".format(self.errstr)

    details.append(rcstr)

    if flags & C.PYCBC_RESFLD_KEY and hasattr(self, 'key'):
        details.append("Key={0}".format(self.key))

    if flags & C.PYCBC_RESFLD_VALUE and hasattr(self, 'value'):
        details.append("Value={0}".format(repr(self.value)))

    if flags & C.PYCBC_RESFLD_CAS and hasattr(self, 'cas'):
        details.append("CAS=0x{cas:x}".format(cas=self.cas))

    if flags & C.PYCBC_RESFLD_CAS and hasattr(self, 'flags'):
        details.append("Flags=0x{flags:x}".format(flags=self.flags))

    if flags & C.PYCBC_RESFLD_HTCODE and hasattr(self, "http_status"):
        details.append("HTTP={0}".format(self.http_status))

    if flags & C.PYCBC_RESFLD_URL and hasattr(self, "url"):
        details.append("URL={0}".format(self.url))

    ret = "{0}<{1}>".format(self.__class__.__name__, ', '.join(details))
    return ret

def _json_encode_wrapper(*args):
    return json.dumps(*args, ensure_ascii=False)

C._init_helpers(
                result_reprfunc = _result__repr__,
                fmt_utf8_flags = C.FMT_UTF8,
                fmt_bytes_flags = C.FMT_BYTES,
                pickle_encode = pickle.dumps,
                pickle_decode = pickle.loads,
                json_encode = _json_encode_wrapper,
                json_decode = json.loads,
                lcb_errno_map = E._LCB_ERRNO_MAP,
                misc_errno_map = E._EXCTYPE_MAP,
                default_exception = E.CouchbaseError)
