# -*- coding: utf-8 -*-
import re
import platform
system = platform.system()
import widgetUtils
import output
import url_shortener
import sound
from pubsub import pub
if system == "Windows":
 from wxUI.dialogs import message, urlList
 from extra import translator, SpellChecker, autocompletionUsers
 from extra.AudioUploader import audioUploader
elif system == "Linux":
 from gtkUI.dialogs import message
from twitter import utils

class basicTweet(object):
 """ This class handles the tweet main features. Other classes should derive from this class."""
 def __init__(self, session, title, caption, text, messageType="tweet", max=140):
  super(basicTweet, self).__init__()
  self.max = max
  self.title = title
  self.session = session
  self.message = getattr(message, messageType)(title, caption, text)
  widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
  widgetUtils.connect_event(self.message.attach, widgetUtils.BUTTON_PRESSED, self.attach)
#  if system == "Windows":
#  if messageType != "dm":
  widgetUtils.connect_event(self.message.text, widgetUtils.ENTERED_TEXT, self.text_processor)
  self.text_processor()
  widgetUtils.connect_event(self.message.shortenButton, widgetUtils.BUTTON_PRESSED, self.shorten)
  widgetUtils.connect_event(self.message.unshortenButton, widgetUtils.BUTTON_PRESSED, self.unshorten)
  widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate)

 def translate(self, event=None):
  dlg = translator.gui.translateDialog()
  if dlg.get_response() == widgetUtils.OK:
   text_to_translate = self.message.get_text()
   source = [x[0] for x in translator.translator.available_languages()][dlg.get("source_lang")]
   dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")]
   msg = translator.translator.translate(text=text_to_translate, source=source, target=dest)
   self.message.set_text(msg)
   self.message.text_focus()
   output.speak(_(u"Translated"))
  else:
   return

 def shorten(self, event=None):
  urls = utils.find_urls_in_text(self.message.get_text())
  if len(urls) == 0:
   output.speak(_(u"There's no URL to be shortened"))
   self.message.text_focus()
  elif len(urls) == 1:
   self.message.set_text(self.message.get_text().replace(urls[0], url_shortener.shorten(urls[0])))
   output.speak(_(u"URL shortened"))
   self.message.text_focus()
  elif len(urls) > 1:
   list_urls = urlList.urlList()
   list_urls.populate_list(urls)
   if list_urls.get_response() == widgetUtils.OK:
    self.message.set_text(self.message.get_text().replace(urls[list_urls.get_item()], url_shortener.shorten(list_urls.get_string())))
    output.speak(_(u"URL shortened"))
    self.message.text_focus()

 def unshorten(self, event=None):
  urls = utils.find_urls_in_text(self.message.get_text())
  if len(urls) == 0:
   output.speak(_(u"There's no URL to be expanded"))
   self.message.text_focus()
  elif len(urls) == 1:
   self.message.set_text(self.message.get_text().replace(urls[0], url_shortener.unshorten(urls[0])))
   output.speak(_(u"URL expanded"))
   self.message.text_focus()
  elif len(urls) > 1:
   list_urls = urlList.urlList()
   list_urls.populate_list(urls)
   if list_urls.get_response() == widgetUtils.OK:
    self.message.set_text(self.message.get_text().replace(urls[list_urls.get_item()], url_shortener.unshorten(list_urls.get_string())))
    output.speak(_(u"URL expanded"))
    self.message.text_focus()

 def text_processor(self, *args, **kwargs):
  if len(self.message.get_text()) > 1:
   self.message.enable_button("shortenButton")
   self.message.enable_button("unshortenButton")
  else:
   self.message.disable_button("shortenButton")
   self.message.disable_button("unshortenButton")
  if self.message.get("long_tweet") == False:
   self.message.set_title(_(u"%s - %s of %d characters") % (self.title, len(self.message.get_text()), self.max))
   if len(self.message.get_text()) > self.max:
    self.session.sound.play("max_length.ogg")
  else:
   self.message.set_title(_(u"%s - %s characters") % (self.title, len(self.message.get_text())))

 def spellcheck(self, event=None):
  text = self.message.get_text()
  checker = SpellChecker.spellchecker.spellChecker(text, "")
  if hasattr(checker, "fixed_text"):
   self.message.set_text(checker.fixed_text)
   self.message.text_focus()

 def attach(self, *args, **kwargs):
  def completed_callback(dlg):
   url = dlg.uploaderFunction.get_url()
   pub.unsubscribe(dlg.uploaderDialog.update, "uploading")
   dlg.uploaderDialog.destroy()
   if url != 0:
    self.message.set_text(self.message.get_text()+url+" #audio")
   else:
    output.speak(_(u"Unable to upload the audio"))
   dlg.cleanup()
  dlg = audioUploader.audioUploader(self.session.settings, completed_callback)
  self.message.text_focus()

class tweet(basicTweet):
 def __init__(self, session, title, caption, text, twishort_enabled, messageType="tweet", max=140):
  super(tweet, self).__init__(session, title, caption, text, messageType, max)
  self.image = None
  widgetUtils.connect_event(self.message.upload_image, widgetUtils.BUTTON_PRESSED, self.upload_image)
  widgetUtils.connect_event(self.message.autocompletionButton, widgetUtils.BUTTON_PRESSED, self.autocomplete_users)
  if twishort_enabled == False:
   try: self.message.long_tweet.SetValue(False)
   except AttributeError: pass

 def upload_image(self, *args, **kwargs):
  if self.message.get("upload_image") == _(u"Discard image"):
   del self.image
   self.image = None
   output.speak(_(u"Discarded"))
   self.message.set("upload_image", _(u"Upload a picture"))
  else:
   self.image = self.message.get_image()
   if self.image != None:
    self.message.set("upload_image", _(u"Discard image"))
  self.message.text_focus()

 def autocomplete_users(self, *args, **kwargs):
  c = autocompletionUsers.completion.autocompletionUsers(self.message, self.session.session_id)
  c.show_menu()

class reply(tweet):
 def __init__(self, session, title, caption, text, twishort_enabled, users=None):
  super(reply, self).__init__(session, title, caption, text, twishort_enabled, messageType="reply")
  self.users = users
  if self.users != None and len(self.users) > 1:
   widgetUtils.connect_event(self.message.mentionAll, widgetUtils.BUTTON_PRESSED, self.mention_all)
   self.message.enable_button("mentionAll")
  self.message.set_cursor_at_end()

 def mention_all(self, *args, **kwargs):
  self.message.set_text(self.message.get_text()+self.users)
  self.message.set_cursor_at_end()
  self.message.text_focus()

class dm(basicTweet):
 def __init__(self, session, title, caption, text):
  super(dm, self).__init__(session, title, caption, text, messageType="dm", max=10000)
  widgetUtils.connect_event(self.message.autocompletionButton, widgetUtils.BUTTON_PRESSED, self.autocomplete_users)

 def autocomplete_users(self, *args, **kwargs):
  c = autocompletionUsers.completion.autocompletionUsers(self.message, self.session.session_id)
  c.show_menu("dm")

class viewTweet(basicTweet):
 def __init__(self, tweet, tweetList, is_tweet=True):
  if is_tweet == True:
   image_description = []
   text = ""
   for i in xrange(0, len(tweetList)):
    if tweetList[i].has_key("message"):
     value = "message"
    else:
     value = "text"
    if tweetList[i].has_key("retweeted_status"):
     text = text + "rt @%s: %s\n" % (tweetList[i]["retweeted_status"]["user"]["screen_name"], tweetList[i]["retweeted_status"][value])
    else:
     text = text + "@%s: %s\n" % (tweetList[i]["user"]["screen_name"], tweetList[i][value])
    if tweetList[i].has_key("extended_entities") and tweetList[i]["extended_entities"].has_key("media"):
     for z in tweetList[i]["extended_entities"]["media"]:
      if z.has_key("ext_alt_text") and z["ext_alt_text"] != None:
       image_description.append(z["ext_alt_text"])
   rt_count = str(tweet["retweet_count"])
   favs_count = str(tweet["favorite_count"])
   source = str(re.sub(r"(?s)<.*?>", "", tweet["source"].encode("utf-8")))
   if text == "":
    if tweet.has_key("message"):
     value = "message"
    else:
     value = "text"
    if tweet.has_key("retweeted_status"):
     text = "rt @%s: %s" % (tweet["retweeted_status"]["user"]["screen_name"], tweet["retweeted_status"]["text"])
    else:
     text = tweet[value]
   text = self.clear_text(text)
   if tweet.has_key("extended_entities") and tweet["extended_entities"].has_key("media"):
    for z in tweet["extended_entities"]["media"]:
     if z.has_key("ext_alt_text") and z["ext_alt_text"] != None:
      image_description.append(z["ext_alt_text"])
   self.message = message.viewTweet(text, rt_count, favs_count, source.decode("utf-8"))
   self.message.set_title(len(text))
   [self.message.set_image_description(i) for i in image_description]
  else:
   text = tweet
   self.message = message.viewNonTweet(text)
  widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
  widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate)
  if self.contain_urls() == True:
   self.message.enable_button("unshortenButton")
   widgetUtils.connect_event(self.message.unshortenButton, widgetUtils.BUTTON_PRESSED, self.unshorten)
  self.message.get_response()

 def contain_urls(self):
  if len(utils.find_urls_in_text(self.message.get_text())) > 0:
   return True
  return False

 def clear_text(self, text):
  urls = utils.find_urls_in_text(text)
  for i in urls:
   if "https://twitter.com/" in i:
    text = text.replace(i, "")
  return text
