#!/usr/bin/env python
# -*- coding: utf-8 -*-
# PychoHistory ###############################################################
#
# Dependencies
# sqlite3, requests, python-dateutil, twitter
#
# Imports ###
import os
from optparse import OptionParser
import logging

from modules import BusinessLogic

# Release info ###
__version__ = "0.2"
__author__ = 'Giannis Dzegoutanis'

# Globals ###
DEFAULT_DB_DIRECTORY = os.path.join(os.path.dirname(os.path.abspath(__file__)), "PychoHistoryDatastore")

# Header #####################################################################


# Main #######################################################################
# todo make testable pls
def queries_arg(option, opt_str, value, parser):
  keywords = parser.rargs[0].split(',') if len(parser.rargs) > 0 else None
  setattr(parser.values, option.dest, keywords)


def main():
  usage = 'usage: %prog'
  parser = OptionParser(usage)

  parser.add_option('-d', '--debug', action='store_true', dest='debug',
                    help='Set debug messages')
  parser.add_option('-r', '--read', action='store_true', dest='read',
                    help='Read from the datastore')
  parser.add_option('-w', '--write', action='callback', callback=queries_arg, dest='write',
                    help='Write to the datastore by fetching data from the net i.e. keyword1,keyword2[,...]')
  parser.add_option('-n', '--namespace', action='store', dest='ns',
                    default=False, help='Specify the database namespace')
  parser.add_option('-f', '--format', action="store", dest='format',
                    default='python', help='Input or output the datastore as python, json, tsv')
  parser.add_option('-t', '--time', action="store", dest='time',
                    default=1, help='Get the results of from the last days (default: 1)')
  parser.add_option('-l', '--list', action="store_true", dest='list',
                    help='List all the databases')
  parser.add_option('-m', '--module', action="store", dest='module',
                    default='TweetCatcher', help='Read data from the specified module')
  parser.add_option('-i', '--import', action="store", dest='import_data',
                    default=False, help='Import data')



  options, args = parser.parse_args()

  if options.read is None and options.write is None and options.list is None and options.import_data is None:
    parser.error("Must provide --read or --write operations")

  db_directory = DEFAULT_DB_DIRECTORY

  # Set debug messages
  if options.debug:
    logging.basicConfig(level=logging.DEBUG)
  else:
    logging.basicConfig(level=logging.INFO)

  with BusinessLogic.BusinessLogic(db_directory, options.ns) as logic:
    if options.write:
      logic.write(options.write)
    elif options.read:
      logic.read(module=options.module, time=options.time, output_format=options.format)
    elif options.list:
      logic.list()
    elif options.import_data:
      logic.import_data(options.import_data, input_format=options.format, module=options.module)

if __name__ == '__main__':
  main()

# Main #######################################################################
