#!/usr/bin/env python

#import blipapi
#from blipapi import BlipApi
# needed: python-json
import elementary,edje

#blip = BlipApi('dos','password')
#blip.uagent='Blipomoko pre-0.0.1'

def nick_from_user(user):
  if user.startswith('/users/'):
    user=user[7:]
  return user

def dashboard(user=None):
  updates = blip.dashboard_read(user=user)

  if updates['status_code']==200:
    updates=updates['body']
    updates.reverse()
    for update in updates:
      if update['type']=='Status':
        handle_status(update)
      elif update['type']=='DirectedMessage':
        handle_directed(update)
      elif update['type']=='PrivateMessage':
        handle_private(update)
      elif update['type']=='Notice':
        handle_notice(update)
      else:
        unknown(update)
  else:
    print 'error'

def handle_status(update):
  anch = elementary.AnchorBlock(win)
  body = entry.utf8_to_markup(update['body'])
  if 'pictures_path' in update:
    body = body + ' [FOTO]'
  anch.text_set(body)
  anch.show()
  fr = elementary.Frame(win)
  fr.label_set(nick_from_user(update['user_path'])+':')
  fr.content_set(anch)
  fr.size_hint_align_set(-1.0,0.0)
  fr.size_hint_weight_set(1.0,0.0)
  fr.show()
  blibox.pack_start(fr)
  bliboxcontent.append(fr)

def handle_directed(update):
#  print nick_from_user(update['user_path']) + '>'+nick_from_user(update['recipient_path'])+': ' + update['body']
  anch = elementary.AnchorBlock(win)
  body = entry.utf8_to_markup(update['body'])
  anch.text_set(body)
  anch.show()
  fr = elementary.Frame(win)
  fr.label_set(nick_from_user(update['user_path'])+' > '+nick_from_user(update['recipient_path'])+':')
  fr.content_set(anch)
  fr.size_hint_align_set(-1.0,0.0)
  fr.size_hint_weight_set(1.0,0.0)
  fr.show()
  blibox.pack_start(fr)
  bliboxcontent.append(fr)

def handle_private(update):
#  print nick_from_user(update['user_path']) + ' >> '+nick_from_user(update['recipient_path'])+': ' + update['body']
  anch = elementary.AnchorBlock(win)
  body = entry.utf8_to_markup(update['body'])
  anch.text_set(body)
  anch.show()
  fr = elementary.Frame(win)
  fr.label_set(nick_from_user(update['user_path'])+' >> '+nick_from_user(update['recipient_path'])+':')
  fr.content_set(anch)
  fr.size_hint_align_set(-1.0,0.0)
  fr.size_hint_weight_set(1.0,0.0)
  fr.show()
  blibox.pack_start(fr)
  bliboxcontent.append(fr)

def handle_notice(update):
  anch = elementary.AnchorBlock(win)
  body = entry.utf8_to_markup(update['body'])
  anch.text_set(body)
  anch.size_hint_align_set(-1.0,0.5)
  anch.size_hint_weight_set(1.0,0.0)
  anch.show()
  info = elementary.Label(win)
  info.label_set('(i)')
  info.scale_set(2.5)
  info.show()
  box = elementary.Box(win)
  box.horizontal_set(True)
  box.size_hint_align_set(-1.0,0.0)
  box.size_hint_weight_set(1.0,0.0)
  box.show()
  box.pack_start(info)
  box.pack_end(anch)
  fr2 = elementary.Frame(win)
  fr2.style_set('outdent_top')
  fr2.content_set(box)
  fr2.size_hint_align_set(-1.0,0.0)
  fr2.size_hint_weight_set(1.0,0.0)
  fr2.show()
  fr = elementary.Frame(win)
  fr.style_set('outdent_bottom')
  fr.content_set(fr2)
  fr.size_hint_align_set(-1.0,0.0)
  fr.size_hint_weight_set(1.0,0.0)
  fr.show()
  blibox.pack_start(fr)
  bliboxcontent.append(fr)

def handle_unknown(update):
  print 'UNKNOWN: '+update['type']
  print update

def clean_blibox():
  global bliboxcontent
  for u in bliboxcontent:
    u.hide()
    u.delete()
    del u
  bliboxcontent = []

def post_blip(obj, *args, **kwargs):
  global entrydefault
  clean_blibox()
  text=utnij(entry.markup_to_utf8(entry.entry_get()))
  blip.update_create(body=text)
  entry.entry_set('Type your entry here...')
  entrydefault = True
  dashboard()

def update_clicked(obj, *args, **kwargs):
  global entrydefault
  if entrydefault:
    entry.entry_set('')
    entrydefault=False

def utnij(text):
  if text.endswith('\n'):
    text=text[:len(text)-1]
  return text

def update_chars(obj, *args, **kwargs):
  entryleft.label_set("%d chars left" % (160-len(utnij(obj.markup_to_utf8(obj.entry_get())))))

def display_dashboard(*args,**kwargs):
  if entrydefault:
    nick=None
  else:
    nick=utnij(entry.markup_to_utf8(entry.entry_get()))
  clean_blibox()
  entry.entry_set('Type your entry here...')
  entrydefault = True
  dashboard(nick)

def destroy(*args,**kwargs):
  print 'kabum!'
  elementary.exit()

elementary.init()
win = elementary.Window('blipomoko',0);
bg = elementary.Background(win)
bg.show()
win.title_set('Blipomoko')
win.destroy = destroy
win.show()
box = elementary.Box(win)
box.show()

layout = elementary.Layout(win)
layout.file_set('blipomoko.edj', 'layout')
layout.show()
layout.content_set('bg', bg)

#def handler(obj, event, name, *args, **kargs):
#  print name+': '+event
#layout.edje_get().signal_callback_add('*', '*', handler)

topbox = elementary.Box(win)
topbox.size_hint_align_set(-1.0,-1.0)
topbox.size_hint_weight_set(1.0,1.0)
topbox.horizontal_set(True)
topbox.show()
#box.pack_start(topbox)
layout.content_set('gora', topbox)

entry = elementary.Entry(win)
entry.entry_set('Type your entry here...')
entrydefault = True
entry.on_mouse_down_add( update_clicked)
entry.show()
entry.size_hint_align_set(-1.0,-1.0)
entry.size_hint_weight_set(1.0,1.0)

entryscr = elementary.Scroller(win)
entryscr.size_hint_align_set(-1.0,-1.0)
entryscr.size_hint_weight_set(1.0,1.0)
entryscr.content_set(entry)
entryscr.show()

entrybox = elementary.Box(win)
entrybox.show()
entrybox.size_hint_align_set(-1.0,-1.0)
entrybox.size_hint_weight_set(1.0,1.0)
entrybox.pack_start(entryscr)

entryleft = elementary.Label(win)
entryleft.label_set('? chars left')
entryleft.show()
entry.changed = update_chars
entryleft.size_hint_align_set(1.0,1.0)
entrybox.pack_end(entryleft)

entryfr = elementary.Frame(win)
entryfr.style_set('pad_small')
entryfr.content_set(entrybox)
entryfr.show()
entryfr.size_hint_align_set(-1.0,-1.0)
entryfr.size_hint_weight_set(1.0,1.0)
topbox.pack_start(entryfr)

rightbox = elementary.Box(win)
rightbox.size_hint_align_set(-1.0, -1.0)
rightbox.size_hint_weight_set(0.0,1.0)
rightbox.show()
topbox.pack_end(rightbox)

post = elementary.Button(win)
post.label_set(' Post it! ')
post.clicked = post_blip
post.show()
post.size_hint_align_set(-1.0, -1.0)
post.size_hint_weight_set(0.0,1.0)
rightbox.pack_start(post)

scr = elementary.Scroller(win)
scr.size_hint_align_set(-1.0,-1.0)
scr.size_hint_weight_set(1.0,1.0)
#box.pack_end(scr)
layout.content_set('dol', scr)
win.resize_object_add(layout)
scr.show()
blibox = elementary.Box(win)
scr.content_set(blibox)
scr.bounce_set(0,1)
blibox.size_hint_align_set(-1.0,0.0)
blibox.size_hint_weight_set(1.0,0.0)
blibox.show()
bliboxcontent = []
#dashboard()
handle_status({'body':'Some testing message', 'user_path':'/users/dos'})
handle_notice({'body':'^dos mblah blah some notice'})
handle_status({'body':'Some testing message', 'user_path':'/users/dos'})
handle_notice({'body':'^dos mblah blah some notice'})
handle_status({'body':'Some testing message', 'user_path':'/users/dos'})
handle_notice({'body':'^dos mblah blah some notice'})

elementary.run()
elementary.shutdown()
