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

import cgi
import uuid

from hive.hive import Hive

from lib import db
from lib import api
from lib import errorHandler
from lib.config import config


client = Hive(nodes=config.nodes)

# output variable
data = {}

# get and set variables
form      = cgi.FieldStorage()
username  = form.getvalue('username')
userhash  = form.getvalue('userhash')
switch    = form.getvalue('switch')
account   = form.getvalue('account')
deleteuv  = form.getvalue('deleteupvote')
blacklist = form.getvalue('blacklist')
reason    = form.getvalue('reason')
deletebl  = form.getvalue('deleteblacklist')
deletedv  = form.getvalue('deletedownvote')


# check permissions
results = db.select('users',['admin'],{'account':username,'hash':userhash,'admin':1},'account')
if len(results) < 1 or results[0]['admin'] == 0:
  errorHandler.throwError('No permission')

if switch:
  if switch == 'admin' or switch == 'delete':
    results = db.select('users',['admin'],'1=1','id')
    if len(results) < 2:
      errorHandler.throwError('There has to be one admin')
  if switch == 'delete':
    db.delete('users',{'id':account})
  else:
    results = db.select('users',[switch],{'id':account},switch)
    if results[0][switch] == 1:
      new = 0
    else:
      new = 1
    db.update('users',{switch:new},{'id':account})
  data['status'] = 'success'
elif deleteuv:
  post = db.select('upvotes',['status'],{'id':deleteuv},'id')
  if post[0]['status'] == 'in queue':
    db.update('upvotes',{'status':'removed by '+username},{'id':deleteuv})
    data['status'] = 'success'
  else:
    errorHandler.throwError('Only posts in queue can be removed from the queue. Duh!')
elif blacklist:
  if reason:
    bl = db.select('blacklist',['user'],{'user':blacklist},'id')
    if len(bl) > 0:
      errorHandler.throwError('User is already blacklisted.')
    db.insert('blacklist',{'id':uuid.uuid4().hex,'user':blacklist,'reason':reason,'account':username})
    data['status'] = 'success'
  else:
    errorHandler.throwError('You need to give a reason for blacklisting')
elif deletebl:
  db.delete('blacklist',{'id':deletebl})
  data['status'] = 'success'
elif deletedv:
  post = db.select('downvotes',['status'],{'id':deletedv},'id')
  if post[0]['status'] == 'wait':
    db.update('downvotes',{'status':'removed by '+username},{'id':deletedv})
    data['status'] = 'success'
  else:
    errorHandler.throwError('Only posts that are waiting for a downvote can be removed. Duh!')
else:
  # get users
  results = db.select('users',['id','account','curator','admin','created'],'1=1','created DESC')
  users = []
  for row in results:
    user = dict(row)
    user['delegator'] = 0
    if "skip_delegator_check" in config and config.skip_delegator_check:
      delegations = []
    else:
      delegations = client.get_vesting_delegations(user['account'],'curangel',1)

    if len(delegations) > 0 and delegations[0]['delegatee'] == 'curangel':
      user['delegator'] = 1
    users.append(user)

  data['users'] = users

  # get upvotes
  upvotes = []
  results = db.select('upvotes',['id','account','created','link','user','category','slug','title','type','payout','status','reward_sp'],'1=1','created DESC', 100)
  for row in results:
    upvotes.append(dict(row))

  data['upvotes'] = upvotes

  # get blacklist
  blacklist = []
  results = db.select('blacklist',['id','user','reason','created','account'],'1=1','created DESC')
  for row in results:
    blacklist.append(dict(row))

  data['blacklist'] = blacklist

  # get downvotes
  downvotes = []
  results = db.select('downvotes',['id','created','account','reason','link','user','category','slug','title','type','payout','maxi','status'],'1=1','created DESC', 100)
  for row in results:
    downvotes.append(dict(row))

  data['downvotes'] = downvotes

api.output(data)
