import os,requests,json,time,base64
from igf_data.utils.dbutils import read_json_data
from igf_data.utils.fileutils import check_file_path

class IGF_ms_team:
  def __init__(self,webhook_conf_file):
    try:
      webhook_conf =  read_json_data(webhook_conf_file)[0]
    except Exception as e:
      raise ValueError(
        "Failed to parse webhook config file {0}, error: {1}".\
          format(webhook_conf_file,e))
    self.webhook_conf = webhook_conf

  def post_image_to_team(self,image_path,message='',reaction=''):
    '''
    A method for posting image file to MS team chanel

    :param image_path: An image file path
    :param message: An optional message, default ''
    :param reaction: A text for image reacion theme, default ''
    '''
    try:
      webhook_url = self.webhook_conf.get('webhook_url')
      check_file_path(image_path)
      encoded_image = base64.b64encode(open(image_path, "rb").read()).decode()
      formatted_message = "![Failed image upload](data:image/png;base64,{0}){1}".\
                            format(encoded_image,message)
      themeColor = '#FFFFFF'
      if reaction !='' or reaction is not None:
        if reaction == 'pass':
          reaction =  '&#x2705;'
          themeColor = '#00cc44'
        elif reaction == 'fail':
          reaction = '&#x274C'
          themeColor = '#DC143C'
        elif reaction == 'sleep':
          reaction = '&#128564'
          themeColor = '#000080'
      json_data = {
        "@context": "https://schema.org/extensions",
        "@type": "MessageText",
        "themeColor": themeColor,
        "TextFormat":"markdown",
        "text": formatted_message}
      r = requests.post(url=webhook_url,json=json_data)
      if r.status_code != 200:
        raise ValueError(
          "Failed to post message to team, error code: {0},{1}".\
            format(r.status_code,r.json()))
      time.sleep(2)
    except Exception as e:
      raise ValueError('Failed to send image file to team, error: {0}, file: {1}'.\
              format(e,image_path))


  def post_message_to_team(self,message,reaction=''):
    try:
      webhook_url = self.webhook_conf.get('webhook_url')
      formatted_message = ''
      themeColor = '#FFFFFF'
      if reaction !='' or reaction is not None:
        if reaction == 'pass':
          reaction =  '&#x2705;'
          themeColor = '#00cc44'
        elif reaction == 'fail':
          reaction = '&#x274C'
          themeColor = '#DC143C'
        elif reaction == 'sleep':
          reaction = '&#128564'
          themeColor = '#000080'
        formatted_message = "{0}; {1}".format(reaction,message)
      else:
        formatted_message = message
      json_data = {
        "@context": "https://schema.org/extensions",
        "@type": "MessageText",
        "themeColor": themeColor,
        "TextFormat":"markdown",
        "text": formatted_message}
      r = requests.post(url=webhook_url,json=json_data)
      if r.status_code != 200:
        raise ValueError(
          "Failed to post message to team, error code: {0},{1}".\
            format(r.status_code,r.json()))
      time.sleep(2)
    except Exception as e:
      raise ValueError('Failed to send message to team, error: {0}'.format(e))

@@ -946,42 +946,17 @@
 ted_
+i
 m
-ess
 age = %22
-!%5BFailed image upload%5D(
 data
@@ -980,102 +980,31 @@
 ,%7B0%7D
-)%7B1%7D%22.%5C%0A                            format(encoded_image,message)%0A      themeColor = '#FFFFFF'
+%22.format(encoded_image)
 %0A   
@@ -1106,49 +1106,12 @@
 =  '
-&#x2705;'%0A          themeColor = '#00cc44
+good
 '%0A  
@@ -1167,48 +1167,17 @@
  = '
-&#x274C'%0A          themeColor = '#DC143C
+attention
 '%0A  
@@ -1234,57 +1234,23 @@
  = '
-&#128564'%0A          themeColor = '#000080
+default
 '%0A      
 json
@@ -1241,24 +1241,25 @@
 ault'%0A      
+#
 json_data = 
@@ -1258,32 +1258,33 @@
 _data = %7B%0A      
+#
   %22@context%22: %22h
@@ -1312,32 +1312,33 @@
 ensions%22,%0A      
+#
   %22@type%22: %22Mess
@@ -1345,32 +1345,33 @@
 ageText%22,%0A      
+#
   %22themeColor%22: 
@@ -1380,32 +1380,33 @@
 emeColor,%0A      
+#
   %22TextFormat%22:%22
@@ -1414,32 +1414,33 @@
 arkdown%22,%0A      
+#
   %22text%22: format
@@ -1444,32 +1444,610 @@
 matted_message%7D%0A
+      json_data = %7B%0A        %22type%22:%22message%22,%0A        %22attachments%22:%5B%7B%0A          %22contentType%22:%22application/vnd.microsoft.card.adaptive%22,%0A          %22contentUrl%22:None,%0A          %22content%22:%7B%0A            %22$schema%22:%22http://adaptivecards.io/schemas/adaptive-card.json%22,%0A            %22type%22:%22AdaptiveCard%22,%0A            %22version%22:%221.2%22,%0A            %22body%22:%5B%7B%0A              %22type%22: %22Image%22,%0A              %22url%22: formatted_image%0A              %7D,%7B%0A              %22type%22:%22TextBlock%22,%0A              %22text%22:message,%0A              %22color%22:reaction%0A            %7D%5D%0A          %7D%0A        %7D%5D%0A      %7D%0A
       r = reques

fix for image posting