# ------------------------------------------------------------------------------
# Copyright IBM Corp. 2018
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------
#
from .convert_module import convert_objects

def flags(args):
    """
    Starts the program to convert CSV objects to Parquet.
    
    Required flags:
    -a or --apikey      -   IBM Cloud API Key
    -e or --endpoint    -   URI endpoint of the COS bucket being used
    -b or --bucket      -   COS bucket that will be used to check for objects, convert then, and place them back into the bucket

    Future TODO: Users can select a different output bucket as long as they are have the same COS endpoint
    """

    converter = convert_objects(args.apikey, args.cos_endpoint, args.cos_bucket)

    if args.list_all:
        """ 
        -l - Lists all the files in a COS bucket
        """
        print(converter.list_bucket_objects())
        return

    if args.csv:
        """ 
        -c - Lists only CSV files in a COS bucket
        """
        print(converter.list_csv_objects())
        return
    
    if args.csv_names:
        """
        -cn - Lists only the names of CSV objects in a COS bucket
        """
        for i, name in enumerate(converter.list_csv_objects_names()):
            print("{}) {}".format((i + 1), name))

        return

    if args.files:
        """ 
        Flags -f and -n must be used together and the number of CSV objects names must match the new Parquet object names.

        -f - Names CSV objects to convert. If you attempt to name the same object twice,
            it will only accept the name once.
        -n - New names of converted objects to Parquet. Names can include prefixes like `my/new/object.parquet`. 
            Do not put the `.parquet` file name after the new name. It's already added for you. The number of new names must
            equal the number of object names you want converted or it will not convert the objects.
        """
        
        csvs_to_convert = args.files
        csvs_in_bucket = converter.list_csv_objects_names()
        objs_new_names = args.names

        if len(csvs_to_convert) != len(objs_new_names):
            print("The number of objects to convert doesn't equal the new names you provided.")
            return

        if not objs_new_names:
            print("You need to provide new name(s) for the objects you want to convert.")
            return

        objs_to_convert = [obj for obj in csvs_to_convert if obj in csvs_in_bucket]
        
        for converted_obj, name in zip(objs_to_convert, objs_new_names):
            print("Now Converting: {} --> {}.parquet".format(converted_obj, name))
        
        converter.convert_selected_objects(objs_to_convert, objs_new_names)
    
    elif not args.files:
        print("You need to provide the names of objects you want to convert.")
