#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from kaikout.log import Logger
import sys
import xml.sax.saxutils as saxutils

logger = Logger(__name__)

"""

NOTE

See XEP-0367: Message Attaching

"""

class XmppMessage:


    def send(self, jid_bare, message_body, chat_type):
        function_name = sys._getframe().f_code.co_name
        logger.debug(f'{function_name},{jid_bare},Start')
        logger.debug(f'{function_name},{jid_bare},Type: {chat_type}; Message: {message_body}')
        jid_full_self = self.boundjid.full if self.is_component else None
        self.send_message(mto=jid_bare,
                          mfrom=jid_full_self,
                          mbody=message_body,
                          mtype=chat_type)
        logger.debug(f'{function_name},{jid_bare},Finish')


    def send_headline(self, jid_bare, message_subject, message_body, chat_type):
        function_name = sys._getframe().f_code.co_name
        logger.debug(f'{function_name},{jid_bare},Start')
        logger.debug(f'{function_name},{jid_bare},Type: {chat_type}; Subject: {message_subject}; Message: {message_body}')
        jid_full_self = self.boundjid.full if self.is_component else None
        self.send_message(mto=jid_bare,
                          mfrom=jid_full_self,
                          # mtype='headline',
                          msubject=message_subject,
                          mbody=message_body,
                          mtype=chat_type,
                          mnick=self.alias)
        logger.debug(f'{function_name},{jid_bare},Finish')


    # NOTE We might want to add more characters
    # def escape_to_xml(raw_string):
    # escape_map = {
    #     '"' : '&quot;',
    #     "'" : '&apos;'
    # }
    # return saxutils.escape(raw_string, escape_map)
    def send_oob(self, jid_bare, url, chat_type):
        jid_full_self = self.boundjid.full if self.is_component else None
        url = saxutils.escape(url)
        # try:
        html = ('<body xmlns="http://www.w3.org/1999/xhtml">'
                f'<a href="{url}">{url}</a></body>')
        message = self.make_message(mto=jid_bare,
                                    mfrom=jid_full_self,
                                    mbody=url,
                                    mhtml=html,
                                    mtype=chat_type)
        message['oob']['url'] = url
        message.send()
        # except:
        #     logging.error('ERROR!')
        #     logging.error(jid, url, chat_type, html)


    # FIXME Solve this function
    def send_oob_reply_message(message, url, response):
        reply = message.reply(response)
        reply['oob']['url'] = url
        reply.send()


    # def send_reply(self, message, message_body):
    #     message.reply(message_body).send()


    def send_reply(self, message, response):
        function_name = sys._getframe().f_code.co_name
        jid_bare = message['from'].bare
        logger.debug(f'{function_name},{jid_bare},Start')
        logger.debug(f'{function_name},{jid_bare},To: {message["to"].bare}; Response: {response}')
        message.reply(response).send()
        logger.debug(f'{function_name},{jid_bare},Finish')
