#!/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import pyhfcus
from pyhfcus.rest import ApiException
from pprint import pprint
from pyhfcus.models.population_data import PopulationData
from pyhfcus.models.license import License
from pyhfcus.models.resolution_info import ResolutionInfo
from pyhfcus.models.haplotype_frequency_data import HaplotypeFrequencyData
from pyhfcus.models.haplotype_frequency import HaplotypeFrequency
from pyhfcus.models.hf_curation_request import HFCurationRequest

import json
import time
import datetime
import argparse
from pprint import pprint


def main():
    """This is run if file is directly executed, but not if imported as
    module. Having this in a separate function  allows importing the file
    into interactive python, and still able to execute the
    function for testing"""
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", "--url",
                        required=False,
                        default="http://hfcus.b12x.org:8080",
                        help="HFCuS URL",
                        type=str)

    parser.add_argument("-f", "--freqfile",
                        required=False,
                        help="Inpute frequency file",
                        type=str)

    parser.add_argument("-c", "--config",
                        required=False,
                        help="Configuration file",
                        type=str)

    parser.add_argument("-v", "--verbose",
                        help="Option for running in verbose",
                        default=False,
                        type=bool)

    args = parser.parse_args()
    url = args.url
    freqfile = args.freqfile
    config = args.config
    client = pyhfcus.ApiClient(host=url)
    api_instance = pyhfcus.DefaultApi(api_client=client)
    with open(config) as data_file:
        config = json.load(data_file)

    resolution = config['resolution']
    license_type = config['license_type']
    popname = config['population_name']
    resinfo = [ResolutionInfo(resolution=resolution)]
    license = License(type_of_license=license_type)
    hapfreqdata = HaplotypeFrequencyData()
    popdata = PopulationData(name=popname)

    hapfreqs = []
    with open(freqfile, 'r') as f:
        for line in f:
            line = line.rstrip()
            haplotype, freq = line.split(",")
            hapfreq = HaplotypeFrequency(haplotype_string=haplotype,
                                         frequency=freq)
            hapfreqs.append(hapfreq)
        f.close()

    hapfreqdata = HaplotypeFrequencyData(haplotype_frequency_list=hapfreqs,
                                         resolution_data=resinfo,
                                         license=license)
    cur_request = HFCurationRequest(population_data=popdata,
                                    haplotype_frequency_data=hapfreqdata)

    response = api_instance.hfc_post(hf_curation_request=cur_request)

    print(response)

if __name__ == '__main__':
    """The following will be run if file is executed directly,
    but not if imported as a module"""
    main()


