#!/usr/bin/env python

from __future__ import absolute_import, division, print_function, unicode_literals
import argparse
import sys
from libsan.host.fc import get_fc_hosts, get_fc_host_wwpn, get_fc_host_remote_ports, wwpn_of_rport
from libsan.host.fc import get_fc_host_rport_targets, get_fc_host_rport_target_devices
from libsan.host.scsi import get_scsi_disk_name, query_all_scsi_disks


class FC(object):

    def __init__(self):
        parser = argparse.ArgumentParser(
            description='Pretends to be git',
            usage='''fc_tool <command> [<args>]

The most commonly used git commands are:
   show     Show information regarding FC and FCoE ports
''')
        parser.add_argument('command', help='Subcommand to run')
        # parse_args defaults to [1:] for args, but you need to
        # exclude the rest of the args too, or validation will fail
        args = parser.parse_args(sys.argv[1:2])
        if not hasattr(self, args.command):
            print('Unrecognized command')
            parser.print_help()
            exit(1)
        # use dispatch pattern to invoke method with same name
        getattr(self, args.command)()

    @staticmethod
    def show():
        def show_host(args):
            print("Running show host")
            # print args.remote
            host_array = get_fc_hosts()
            if not host_array:
                print("INFO: Host does not have any FC adapter")
                return
            for host in host_array:
                wwpn = get_fc_host_wwpn(host)
                print("Host %s has port name %s" % (host, wwpn))
                if args.remote:
                    r_ports_array = get_fc_host_remote_ports(host)
                    if r_ports_array:
                        for r_port in r_ports_array:
                            wwpn = wwpn_of_rport(r_port)
                            print("\t%s has port name %s" % (r_port, wwpn))
                            targets_array = get_fc_host_rport_targets(host, r_port)
                            if not targets_array:
                                print("\t\tNo disk attached")
                                continue
                            for target in targets_array:
                                print("\t\tTarget %s has the following disks" % target)
                                devices_array = get_fc_host_rport_target_devices(host, r_port, target)
                                if not devices_array:
                                    continue
                                for device_id in devices_array:
                                    dev_name = get_scsi_disk_name(device_id)
                                    print("\t\t\t%s" % dev_name)
                    else:
                        print("INFO: host %s is not connected to any target" % host)

        def show_disks(args):
            print("Running show disks")
            print(args.remote)
            scsi_disks = query_all_scsi_disks()
            if scsi_disks:
                for disk in scsi_disks.keys():
                    print("Disk Name: %s" % scsi_disks[disk]["name"])
                    # for param in scsi_disks[disk].keys():
                    #    print "\t%s = %s" % (param, scsi_disks[disk][param])
                    print("\t%s" % scsi_disks[disk])
            # print scsi_disks

        parser = argparse.ArgumentParser(
            description='Show information about host port')
        subparsers = parser.add_subparsers()
        parser_host = subparsers.add_parser('host')
        # prefixing the argument with -- means it's optional
        parser_host.add_argument('--remote', action='store_true')
        parser_host.set_defaults(func=show_host)
        # parser.add_argument('--amend', action='store_true')
        # now that we're inside a subcommand, ignore the first
        # TWO argvs, ie the command (git) and the subcommand (commit)
        parser_disks = subparsers.add_parser('disks')
        parser_disks.set_defaults(func=show_disks)
        args = parser.parse_args(sys.argv[2:])
        args.func(args)
        # print 'Running fc_tool show, host=%s' % args.host


if __name__ == '__main__':
    FC()
