# Copyright (C) 2023 Cochise Ruhulessin
#
# All rights reserved. No warranty, explicit or implicit, provided. In
# no event shall the author(s) be liable for any claim or damages.
#
# 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.
import argparse
import asyncio
import logging
from typing import Any

from headless.ext.foxway import FoxwayClient

from molano.canon import ExternalCatalog
from molano.canon import ExternalCatalogItem
from molano.canon import ProductCatalog
from molano.lib import foxway
from molano.__main__ import application


logger: logging.Logger = logging.getLogger('cbra')


def postprocess_laptop(item: ExternalCatalogItem):
    item.product_name = '{item.product_name} {item.features[computer.keyboard.layout]}'.format(item=item)
    if item.features.get('appearance.color'):
        item.product_name = '{item.product_name} {item.features[appearance.color]}'.format(item=item)
    if item.features['quality.grade.apparent'] not in {None, 'Any', 'N/A'}:
        item.product_name = '{item.product_name} {item.features[quality.grade.apparent]}'.format(item=item)
    return item


def postprocess_samsung(item: ExternalCatalogItem):
    if item.features.get('product.brand') != 'Samsung':
        return
    if not item.features.get('computer.storage.primary'):
        return
    item.product_name = '{item.product_name} {item.features[quality.grade.apparent]}'.format(item=item)
    item.product_name = '{item.features[product.brand]} {item.features[product.model]}'.format(item=item)
    item.product_name = '{item.product_name} {item.features[computer.storage.primary]} {item.features[appearance.color]}'.format(item=item)
    if item.features.get('computer.networking.cellular') in {'4G', '5G'}:
        item.product_name = '{item.product_name} {item.features[computer.networking.cellular]}'.format(item=item)
    if item.features.get('cellular.sim.slots') == '2':
        item.product_name = f'{item.product_name} Dual Sim'
    item.product_name = '{item.product_name} {item.features[quality.grade.apparent]}'.format(item=item)


CATEGORIES: dict[str, dict[str, Any]] = {
    'mobile.samsung': {
        'when': lambda x: all([ # type: ignore
            x.features['product.brand'] == 'Samsung', # type: ignore
            x.features['product.line'] == 'Galaxy', # type: ignore
            not x.product_name.startswith('Samsung Galaxy A3'), # type: ignore
            not x.product_name.startswith('Samsung Galaxy A5'), # type: ignore
            #not x.product_name.startswith('Samsung Galaxy A6'), # type: ignore
            #not x.product_name.startswith('Samsung Galaxy A7'), # type: ignore
        ]),
        'postprocess': postprocess_samsung,
        'defaults': {
            'cellular.sim.slots': '1',
            'computer.networking.cellular': 'Unknown',
            'product.release.generation': 'N/A',
            'product.release.market': 'Worldwide',
        },
        'color_mapping': {
            'Galaxy S23': {
                'Beige': 'Cream',
                'Green': None,
                'Black': 'Phantom Black',
                'Purple': 'Lavender',
            },
            'Galaxy S23 Plus': {
                'Beige': 'Cream',
                'Black': 'Phantom Black',
                'Green': None,
                'Purple': 'Lavender',
            },
            'Galaxy S23 Ultra': {
                'Beige': 'Cream',
                'Black': 'Phantom Black',
                'Green': None,
                'Grey': 'Graphite',
                'Purple': 'Lavender',
            },
            'Galaxy S22': {
                'Black': 'Phantom Black',
                'White': 'Phantom White',
                'Pink': 'Pink Gold',
                'Grey': 'Graphite',
                'Green': 'Green',
                'Purple': 'Bora Purple',
                'Yellow': 'Cream'
            },
            'Galaxy S22 Plus': {
                'Black': 'Phantom Black',
                'White': 'Phantom White',
                'Pink': 'Pink Gold',
                'Green': 'Green',
                'Grey': 'Graphite',
                'Yellow': 'Cream',
            },
            'Galaxy S22 Ultra': {
                'Black': 'Phantom Black',
                'White': 'Phantom White',
                'Green': 'Green',
                'Red': None,
                'Blue': 'Sky Blue',
                'Grey': 'Graphite',
                'Purple': 'Bora Purple'
            },
            'Galaxy S21': {
                'Blue': 'Phantom Blue',
                'Pink': 'Phantom Pink',
                'Purple': 'Phantom Purple',
                'Grey': 'Phantom Grey',
                'White': 'Phantom White'
            },
            'Galaxy S21 Plus': {
                'Black': 'Phantom Black',
                'Silver': 'Phantom Silver',
                'Purple': 'Phantom Violet',
                'Red': 'Phantom Red',
                'Gold': 'Phantom Gold',
            },
            'Galaxy S21 Ultra': {
                'Black': 'Phantom Black',
                'Silver': 'Phantom Silver',
                'Brown': 'Phantom Brown',
                'Blue': 'Phantom Blue',
                'Brown': 'Phantom Brown',
            },
            'Galaxy S21 FE': {
                'White': 'White',
                'Black': NotImplemented,
                #'Grey': 'Graphite',
                'Purple': 'Lavender',
                'Green': 'Olive'
            },
            'Galaxy S20': {
                'Grey': 'Cosmic Gray',
                'Blue': 'Cloud Blue',
                'Pink': 'Cloud Pink',
                'White': 'Cloud White',
            },
            'Galaxy S20 FE': {
                'Blue': 'Cloud Navy',
                'Purple': 'Cloud Lavender',
                'Green': 'Cloud Green',
                'Red': 'Cloud Red',
                'Orange': 'Cloud Orange',
                'White': 'Cloud White'
            },
            'Galaxy S20 Plus': {
                'Aura Blue': 'Aura Blue',
                'Black': 'Cosmic Black',
                'Grey': 'Cosmic Gray',
                'Blue': 'Cloud Blue',
                'Red': 'Aura Red',
                'White': 'Cloud White'
            },
            'Galaxy S20 Ultra': {
                'Black': 'Cosmic Black',
                'Grey': 'Cosmic Gray',
                'White': 'Cloud White'
            },
            'Galaxy S10': {
                'White': 'Prism White',
                'Black': 'Prism Black',
                'Green': 'Prism Green',
                'Blue': 'Prism Blue',
                'Silver': 'Prism Silver',
                'Red': 'Cardinal Red'
            },
            'Galaxy S10 Plus': {
                'White': NotImplemented,
                'Black': NotImplemented,
                'Green': 'Green',
            },
            'Galaxy S9': {
                'Black': 'Midnight Black',
                'Blue': 'Coral Blue',
                'Purple': 'Lilac Purple',
                'Grey': 'Titanium Gray',
                'Gold': 'Sunrise Gold',
            },
            'Galaxy S9 Plus': {
                'Black': 'Midnight Black',
                'Blue': 'Coral Blue',
                'Purple': 'Lilac Purple',
                'Grey': 'Titanium Gray'
            },
            'Galaxy S8': {
                'Blue': 'Coral Blue',
                'Pink': 'Rose Pink',
                'Silver': 'Arctic Silver',
                'Black': 'Midnight Black',
                'Grey': 'Orchid Grey',
            },
            'Galaxy S8 Plus': {
                'Blue': 'Coral Blue',
                'Pink': 'Rose Pink',
                'Silver': 'Arctic Silver',
                'Black': 'Midnight Black',
                'Grey': 'Orchid Grey',
            },
            'Galaxy S7': {
                'White': 'White Pearl',
                'Black': 'Black Onyx',
                'Gold': 'Gold Platinum'
            },
            'Galaxy S6': {
                'Black': 'Black Sapphire',
                'Blue': 'Blue Topaz',
                'Green': 'Emerald Green',
                'Gold': 'Gold Platinum',
                'White': 'White Pearl',
            },
            'Galaxy S6 Edge': {
                'Black': 'Black Sapphire',
                'Blue': 'Blue Topaz',
                'Green': 'Emerald Green',
                'Gold': 'Gold Platinum',
                'White': 'White Pearl',
            },
            'Galaxy S6 Edge Plus': {
                'Black': 'Black Sapphire',
                'Blue': 'Blue Topaz',
                'Green': 'Emerald Green',
                'Gold': 'Gold Platinum',
                'White': 'White Pearl',
            },
            'Galaxy S5': {
                'Gold': 'Copper Gold',
                'Black': 'Charcoal Black',
                'Blue': 'Electric Blue',
                'White': 'Shimmery White'
            },
            'Galaxy S5 Neo': {
                'Black': None,
                'Gray': None,
                'Gold': None
            },
            'Galaxy S4 Mini': {
                'White': 'White Frost',
                'Black': 'Black Mist',
                'Blue': 'Blue',
                'Pink': 'Pink',
                'Orange': 'Orange',
                'Purple': 'Purple'
            },
            'Galaxy S III Mini': {
                'Grey': 'Titan Gray'
            },
            'Galaxy Z Fold 4': {
                'Black': 'Phantom Black',
                'Beige': None,
                'Grey': 'Graygreen',
            },
            'Galaxy Z Fold 3': {
                'Black': 'Phantom Black',
                'Green': 'Phantom Green',
                'Silver': 'Phantom Silver',
            },
            'Galaxy Z Fold 2': {
                'Bronze': 'Mystic Bronze',
                'Black': 'Mystic Black',
                'Grey': NotImplemented
            },
            'Galaxy Z Flip 4': {
                'Blue': 'Blue',
                'Purple': 'Bora Purple',
                'Rose Gold': 'Pink Gold',
                'Grey': 'Graphite',
                'Gold': 'Gold',
                'White': 'Silver'
            },
            'Galaxy Z Flip 3': {
                'Pink'  : 'Rose Pink',
                'Beige': 'Cream',
                'Black': 'Phantom Black',
                'Purple': 'Lavender',
                'Green': 'Green',
                'Grey': 'Grey',
                'Yellow': 'Yellow'
            },
            'Galaxy Z Flip 2': {
                'Black': 'Mirror Black',
                'Gold': 'Mirror Gold',
                'Purple': 'Mirror Purple',
                'Grey': 'Mystic Gray',
                'Bronze': 'Mystic Bronze'
            },
            'Galaxy Z Fold 2': {
                'Grey': NotImplemented,
                'Bronze': 'Mystic Bronze',
                'Black': 'Mystic Black',
            },
            'Galaxy Xcover 3 Value Edition': {
                'Grey': 'Gray'
            },
            'Galaxy Xcover 4': {'Black': 'Black'},
            'Galaxy Xcover 5': {'Black': 'Black'},
            'Galaxy Xcover Pro': {'Black': 'Black'},
            'Galaxy Young 2': {
                'Grey': 'Iris Charcoal',
                'White': 'White'
            },
            'Galaxy Note 10': {
                'White': 'Aura White',
                'Black': 'Aura Black',
                'Pink': 'Aura Pink',
                'Red': 'Aura Red',
                'Silver': 'Aura Glow',
            },
            'Galaxy Note 20': {
                'Black': 'Mystic Black',
                'Bronze': 'Mystic Bronze',
                'Green': 'Mystic Green',
                'White': 'Mystic White',
                'Grey': 'Mystic Gray'
            },
            'Galaxy Note 9': {
                'Blue': 'Ocean Blue',
                'Purple': 'Lavender Purple',
                'Black': 'Midnight Black',
            },
            'Galaxy Note 8': {
                'Black': 'Midnight Black',
                'Grey': 'Orchid Gray',
                'Gold': 'Maple Gold',
                'Blue': 'Deepsea Blue',
                'Pink': 'Blossom Pink',
            },
            'Galaxy Note 5': {
                'Black': None,
                'Gold': None,
                'Silver': None,
                'White': None,
            },
            'Galaxy Note 4': {
                'Black': 'Charcoal Black',
                'Gold': 'Bronze Gold',
                'Pink': 'Blossom Pink',
                'White': 'Frosted White'
            },
            'Galaxy Trend': {
                'Black': 'Black',
                'White': 'White'
            },
            'Galaxy M21': {
                'Black': None,
                'Blue': None
            },
            'Galaxy M13': {
                'Blue': 'Light Blue',
                'Green': 'Deep Green',
                'Orange': 'Orange Copper',
            },
            'Galaxy M12': {
                'Black': None,
                'Blue': None,
                'Green': None
            },
            'Galaxy M11': {
                'Black': 'Black',
                'Blue': 'Metallic Blue',
            },
            'Galaxy J7': {'Black': None},
            'Galaxy J6': {
                'Black': None,
                'Gold': None,
                'Purple': 'Lavender',
                'Blue': None
            },
            'Galaxy J5': {
                'Black': None,
                'Rose Gold': 'Rose Gold',
                'Gold': 'Sunrise Gold',
                'Pink': NotImplemented,
                'White': None
            },
            'Galaxy J4': {},
            'Galaxy J3': {
                'Black': None,
                'Blue': None,
                'Gold': 'Sunrise Gold'
            },
            'Galaxy Core Prime': {
                'White': None,
                'Black': None,
                'Grey': 'Gray'
            },
            'Galaxy Ace 3': {
                'White': 'White',
                'Black': 'Metallic Black',
                'Red': 'Wine Red'
            },
            'Galaxy A9': {
                'Pink': 'Bubblegum Pink',
                'Blue': 'Lemonade Blue',
                'Black': 'Caviar Black',
            },
            'Galaxy A80': {
                'Gold': 'Gold',
                'Grey': 'Orchid Grey',
                'Black': 'Black'
            },
            'Galaxy A8': {
                'Gold': 'Gold',
                'Grey': 'Orchid Grey',
                'Black': 'Black'
            },
            'Galaxy A73': {
                'White': 'Awesome White',
                'Green': 'Awesome Mint',
                'Grey': 'Awesome Gray',
            },
            'Galaxy A72': {
                'Black': 'Awesome Black',
                'White': 'Awesome White',
                'Purple': 'Awesome Violet',
                'Blue': 'Awesome Blue',
            },
            'Galaxy A71': {
                'Black': 'Prism Crush Black',
                'Blue': 'Prism Crush Blue',
                'Silver': 'Prism Crush Silver',
                'White': 'Prism Crush White',
            },
            'Galaxy A70': {
                'White': None,
                'Pink': None,
                'Black': None,
            },
            'Galaxy A7': {
                'Gold': 'Gold',
                'Black': None,
                'Blue': None
            },
        },
        'categories': [('1', '1')],
        'required': {
            'product.brand',
            'product.kind',
            'product.line',
            'product.model',
            'product.release.generation'
        },
        'selectable': {
            'appearance.color',
            'cellular.sim.slots',
            'computer.networking.cellular',
            'computer.storage.primary',
            'quality.functional.state',
            'quality.grade.apparent',
            'quality.packaging.state',
            'product.variant',
        }
    },
    'mobile.apple': {
        'when': lambda x: x.features['product.brand'] == 'Apple', # type: ignore
        'defaults': {
            'product.release.generation': 'N/A',
            'product.release.market': 'Worldwide',
        },
        'categories': [('1', '3')],
        'required': {
            'product.brand',
            'product.kind',
            'product.line',
            'product.model',
            'product.release.generation',
        },
        'selectable': {
            'appearance.color',
            'computer.storage.primary',
            'quality.functional.state',
            'quality.grade.apparent',
            'quality.packaging.state',
            'product.release.market',
            'computer.networking.cellular',
        }
    },
    'laptops': {
        'categories': [('11', '12')],
        'name_template': '{features[product.brand]} {features[product.model]} {features[computer.display.diameter]}"',
        'postprocess': postprocess_laptop,
        'color_mapping': {
            'IdeaPad 3 15IIL05': {'Black': 'Black', 'Grey': 'Arctic Gray'},
            'IdeaPad S145-15IWL': {'Black': 'Black', 'Grey': 'Platinum Grey'},
            'IdeaPad C340-14IML': {'Black': 'Black'},
            'IdeaPad Y700-15ISK': {'Black': 'Black'},
            'Legion 5 15IMH05H': {'Black': 'Black'},
            'ThinkPad X270': {'Black': 'Black'},
            'ThinkPad E590': {'Black': 'Black'},
            'ThinkPad L13': {'Black': 'Black'},
            'ThinkPad P51s': {'Black': 'Black'},
            'ThinkPad P52': {'Black': 'Black'},
            'ThinkPad T14': {'Black': 'Black'},
            'ThinkPad T470': {'Black': 'Black'},
            'ThinkPad T470': {'Black': 'Black'},
            'ThinkPad T490': {'Black': 'Black'},
            'ThinkPad T490s': {'Black': 'Black'},
            'ThinkPad T495': {'Black': 'Black'},
            'ThinkPad T580': {'Black': 'Black'},
            'ThinkPad X1 Carbon': {'Black': 'Black'},
            'Yoga 910-13IKB': {'Black': 'Black'},
            'Yoga Slim 7 14ARE05': {
                'Black': 'Black',
                'Grey': 'Slate Grey',
            },
        },
        'defaults': {
            'computer.os.coa': 'N/A',
            'product.release.generation': 'N/A',
            #'product.release.market': 'Worldwide',
        },
        'required': {
            'computer.display.diameter',
            'product.release.generation',
        },
        'optional': {
            'computer.display.touch',
        },
        'selectable': {
            'appearance.color',
            'computer.cpu.integrated',
            'computer.display.resolution',
            'computer.gpu.integrated',
            'computer.keyboard.layout',
            'computer.os.coa',
            'computer.ram',
            'computer.storage',
            'electronic.battery.health',
            'quality.functional.state',
            'quality.grade.apparent',
            'quality.packaging.state'
        },
    }
}

parser = argparse.ArgumentParser("Imports products from the specified source.")
parser.add_argument('-p', action='append', dest='presets')
parser.add_argument('-c', dest='catalog')
parser.add_argument('--commit', action='store_true', dest='commit')
parser.add_argument('--create', action='store_true', dest='allow_create')
parser.add_argument('--features', action='store_true', dest='show_features')


async def main():
    args = parser.parse_args()
    products = ProductCatalog.open(args.catalog)
    logger.info("Importing Foxway products")
    async with FoxwayClient() as client:
        for preset, params in CATEGORIES.items():
            if args.presets and preset not in args.presets:
                logger.info("Skipping preset %s", preset)
                continue
            logger.info("Fetching products for preset %s", preset)
            fn = 'var/catalogs/foxway_{preset}.yml'.format(preset=str.replace(preset, '.', '_'))
            catalog = ExternalCatalog.open(fn, supplier='foxway.shop')
            await foxway.catalog(
                client,
                logger,
                catalog=catalog,
                products=products,
                supplier=catalog.supplier,
                allow_create=args.allow_create,
                **params
            )
            if args.commit:
                catalog.dump(commit=True)
            if args.show_features:
                print(catalog.dump(commit=False, include={'features'}))

    if args.commit:
        if input("Type 'yes' to commit: ") == 'yes':
            products.dump(commit=True)
        raise SystemExit


if __name__ == '__main__':
    asyncio.run(application.run(main))