#!/usr/bin/python3
# -*- coding: utf-8 -*-	
#	
#  PSNN.py
#  terminal app	
#  	
#  Copyright 2019 Lukáš Plevač <lukasplevac@gmail.com>	
#  	
#  This program is free software; you can redistribute it and/or modify	
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#

import PSNN
import argparse
import json
from getpass import getpass

model = PSNN.model()
serverTools = PSNN.server_tools(model)

#add parser
parser = argparse.ArgumentParser(description='Terminal app for PSNN')

#add argumets
parser.add_argument(
    '--server',
    default=None,
    type=str,
    help='set custom PSNN models server (--server=http://example.com/api.php)'
)

parser.add_argument(
    '--model',
    default=None,
    type=str,
    help='show info about model (--model=PSNN/test)'
)

parser.add_argument(
    '--download',
    default=None,
    type=str,
    help='download model (--download=PSNN/test)'
)

parser.add_argument(
    '--of',
    default=None,
    type=str,
    help='output file for some actions (--of=/test/test.nn)'
)

parser.add_argument(
    '--upload',
    default=None,
    type=str,
    help='upload model to server (--upload=/test/test.nn)'
)

parser.add_argument(
    '--desc',
    default=None,
    type=str,
    help='description of model for upload (--desc=this model do ....)'
)

parser.add_argument(
    '--loss',
    default=None,
    type=float,
    help='loss of model what you want upload (--loss=0.001)'
)

parser.add_argument(
    '--name',
    default=None,
    type=str,
    help='name of model what you want upload (--name=hello)'
)

parser.add_argument(
    '--register',
    default=None,
    action='store_true',
    help='register user on server'
)

parser.add_argument(
    '--search',
    default=None,
    type=str,
    help='search for model with name like this (--search=test)'
)

args = parser.parse_args()

if not(args.server is None):
    model.modelsServer = args.server
    serverTools.modelsServer = args.server

if args.register:
    name = input("Your new user name: ")
    password = getpass()
    print(
        json.dumps(serverTools.register(name, password), indent=4, sort_keys=True)
    )

elif not(args.search is None):

    print(
        json.dumps(serverTools.findModel(args.search), indent=4, sort_keys=True)
    )

elif not(args.upload is None):

    if not(args.loss is None) and not(args.desc is None) and not(args.name is None):
        
        user = input("username: ")
        password = getpass()

        model.load(args.upload)

        print(
            json.dumps(serverTools.upload(user, password, model, args.name, args.desc, args.loss), indent=4, sort_keys=True)
        )

    else:
        print("one of --loss --name --desc is not specifikated")
        print("type -h or --help for show help")

elif not(args.download is None):
    
    if args.of is None:
        print("output file --of is not specifikated")
        print("type -h or --help for show help")
    else:
        model.get(args.download)
        model.dump(args.of)

elif not(args.model is None):

    print(
        json.dumps(serverTools.getModelInfo(args.model), indent=4, sort_keys=True)
    )

else:
    print("no action specifikated")
    print("type -h or --help for show help")