from rest_framework import generics
from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
import json
from snow_flake.models import *
from snow_flake.serializers import *
from snow_flake_external_links.models import *
from snow_flake_external_links.serializers import *
from datetime import datetime
from html.parser import HTMLParser
import uuid

class ParseRedbubbleProductURL:
    def __init__(self, url):
        self.url = url

    def getProductURL(self):
        stringList1 = self.url.split("https://www.redbubble.com/i/")
        stringList2 = stringList1[1].split("/")
        productMap = {}
        productMap["category"] = stringList2[0]
        productMap["productId"] = stringList2[2]
        productMap["productURL"] = self.url
        return productMap

class RedbubbleDepartmentList(generics.ListCreateAPIView):
    # authentication_classes = [APIKeyAuthentication,TokenAuthentication]
    queryset = RedbubbleDepartment.objects.all()
    serializer_class = RedbubbleDepartmentSerializer


class RedbubbleProductListParser(HTMLParser):

    attrList = []
    productUrlMapList = []

    def checkProductListLink(self, attrs):
        count = 0
        for i in attrs:
            if "class" in i and "styles__link--3QJ5N" in i:
                count += 1
            if "element" in i and "a" in i:
                count += 1
            if count == 2:
                self.attrList.append(attrs)

    def getProductURLFromList(self):
        for i in self.attrList:
            for j in i:
                if "href" in j:
                    parseUrl = ParseRedbubbleProductURL(j[1])
                    self.productUrlMapList.append(parseUrl.getProductURL())

    def handle_starttag(self, tag, attrs):
        if tag == "a":
            self.checkProductListLink(attrs)

    def handle_endtag(self, tag):
        pass
        # print("Encountered an end tag :", tag)

    def handle_data(self, data):
        pass
        # print("Encountered some data  :", data)


class GetRedbubbleProductListPageView(View):
    def post(self, request, *args, **kwargs):
        jsonData = json.loads(request.body)
        urlMapList = {}
        if "htmlPage" in jsonData:
            parser = RedbubbleProductListParser()
            parser.productUrlMapList.clear()
            parser.attrList.clear()
            parser.feed(jsonData["htmlPage"])
            parser.getProductURLFromList()
            urlMapList["productUrlList"] = parser.productUrlMapList
        urlMapList["isSuccess"] = False
        return JsonResponse(
            urlMapList,
            safe=False,
        )


class ConvertData():

    def saveDepartment(self,departmentName,selectedLanguage):
        isValid = False
        departmentMap={}
        stringData = departmentName.lower()
        stringData = stringData.replace("-","_")
        stringData = stringData.replace(" ","_")
        stringLen = len(stringData)
        keyName = stringData + "_" + str(stringLen)

        tikTokDepartmentMap = {}
        tikTokDepartmentMap["keyName"] = keyName
        tikTokDepartmentMap["name"] = departmentName
        tikTokDepartmentMap["isClothes"] = False
        tikTokDepartmentMap["isAccessories"] = False

        redbubbleDepartmentList = RedbubbleDepartment.objects.filter(keyName = keyName)
        if redbubbleDepartmentList.__len__() == 0:
            redbubbleDepartmentSerializer = RedbubbleDepartmentSerializer(data = tikTokDepartmentMap)
            if redbubbleDepartmentSerializer is not None:
                if redbubbleDepartmentSerializer.is_valid():
                    redbubbleDepartmentSerializer.save()

        departmentMap["keyName"] = keyName
        departmentMap["isVisible"] = True
        departmentMap["createdDate"] = datetime.now()
        departmentMap["updatedDate"] = datetime.now()

        departmentList = Department.objects.filter(keyName = keyName)
        selectedDepartment = None
        if departmentList.__len__() > 0:
            selectedDepartment = departmentList[0]
        
        departmentSerializer = None
        if selectedDepartment is not None:
            departmentSerializer = DepartmentSerializer(
                    selectedDepartment,
                    data=departmentMap,
                    context={"languageId": selectedLanguage.id},
                )
        else:
            departmentSerializer = DepartmentSerializer(data=departmentMap)
        
        depId = None
        if departmentSerializer is not None:
            if departmentSerializer.is_valid():
                departmentSerializer.save()
            
            depId = departmentSerializer.data["id"]

            departmentLanguageMap = {}

            if selectedLanguage is not None:
                departmentLanguageMap["name"] = departmentName
                departmentLanguageMap["languageId"] = selectedLanguage.id
                departmentLanguageMap["departmentId"] = depId

                selectedLangList = DepartmentLanguage.objects.filter(departmentId = depId).filter(languageId = selectedLanguage.id)
                departmentLanguage = None
                if selectedLangList.__len__() > 0:
                    departmentLanguage = selectedLangList[0]
                    departmentLanguageMap["id"] = departmentLanguage.id
                    
                else:
                    departmentLanguageMap["id"] = -1
                
                depLangId = departmentLanguageMap["id"]
                departmentLanguageSerializer = None
                if depLangId > 0:
                    departmentLanguageSerializer = DepartmentLanguageSerializer(
                        departmentLanguage,data = departmentLanguageMap
                    )
                else:
                    departmentLanguageSerializer = DepartmentLanguageSerializer(
                        data = departmentLanguageMap
                    )
                
                if departmentLanguageSerializer is not None:
                    if departmentLanguageSerializer.is_valid():
                        departmentLanguageSerializer.save()
                        isValid = True 

        if isValid:
            return depId

        return None

    def saveBrand(self,brandName,selectedLanguage):
        isValid = False
        brandMap={}
        stringData = brandName.lower()
        stringData = stringData.replace("-","_")
        stringData = stringData.replace(" ","_")
        stringLen = len(stringData)
        keyName = stringData + "_" + str(stringLen)

        brandMap["keyName"] = keyName
        brandMap["isVisible"] = True
        brandMap["createdDate"] = datetime.now()
        brandMap["updatedDate"] = datetime.now()

        brandList = Brand.objects.filter(keyName = keyName)
        selectedBrand = None
        if brandList.__len__() > 0:
            selectedBrand = brandList[0]
        
        brandSerializer = None
        if selectedBrand is not None:
            brandSerializer = BrandSerializer(
                    selectedBrand,
                    data=brandMap,
                    context={"languageId": selectedLanguage.id},
                )
        else:
            brandSerializer = BrandSerializer(data=brandMap)
        
        brandId = None
        if brandSerializer is not None:
            if brandSerializer.is_valid():
                brandSerializer.save()
            
            brandId = brandSerializer.data["id"]

            brandLanguageMap = {}

            if selectedLanguage is not None:
                brandLanguageMap["name"] = brandName
                brandLanguageMap["languageId"] = selectedLanguage.id
                brandLanguageMap["brandId"] = brandId

                selectedLangList = BrandLanguage.objects.filter(brandId = brandId).filter(languageId = selectedLanguage.id)
                brandLanguage = None
                if selectedLangList.__len__() > 0:
                    brandLanguage = selectedLangList[0]
                    brandLanguageMap["id"] = brandLanguage.id
                    
                else:
                    brandLanguageMap["id"] = -1
                
                brandLangId = brandLanguageMap["id"]
                brandLanguageSerializer = None
                if brandLangId > 0:
                    brandLanguageSerializer = BrandLanguageSerializer(
                        brandLanguage,data = brandLanguageMap
                    )
                else:
                    brandLanguageSerializer = BrandLanguageSerializer(
                        data = brandLanguageMap
                    )
                
                if brandLanguageSerializer is not None:
                    if brandLanguageSerializer.is_valid():
                        brandLanguageSerializer.save()
                        isValid = True 

        if isValid:
            return brandId

        return None

    def saveSupplier(self,name,email,selectedLanguage):
        
        supplierList = Supplier.objects.filter(email = email)
        selectedSupplier = None
        if supplierList.__len__() > 0:
            selectedSupplier = supplierList[0]
        
        supplierMap = {}
        supplierMap["name"] = name
        supplierMap["email"] = email
        supplierMap["password"] = ''
        supplierMap["isVerified"] = True
        supplierMap["isFreeSubscriptionUsed"] = True 
        supplierMap["createdDate"] = datetime.now()
        supplierMap["updatedDate"] = datetime.now()
        supplierMap["selectedLanguageId"] = selectedLanguage.id
        supplierSerializer = None
        if selectedSupplier is not None:
            supplierMap["id"] = selectedSupplier.id
            supplierSerializer = SupplierSerializer(selectedSupplier,data = supplierMap)
        else:
            supplierSerializer = SupplierSerializer(data = supplierMap)
        
        if supplierSerializer is not None:
            if supplierSerializer.is_valid():
                supplierSerializer.save()
            
            return supplierSerializer.data["id"]
    
    def getNewArrival(self):
        selected = NewArrival.objects.last()
        if selected is not None:
            return selected.id

    def saveItem(self,json,selectedLanguage):
        selectedItemList = Item.objects.filter(keyName=json["keyName"])
        selectedItem = None
        if selectedItemList.__len__() > 0:
                selectedItem = selectedItemList[0]

        itemSerializer = None
        itemMap = {}
        if selectedItem is not None:
            itemMap["keyName"] = selectedItem.keyName
            itemMap["isApproved"] = selectedItem.isApproved
            itemMap["isTop"] = selectedItem.isTop
            itemMap["isFavorite"] = selectedItem.isFavorite
            itemMap["isElectronic"] = selectedItem.isElectronic
            itemMap["totalViews"] = selectedItem.totalViews
            itemMap["createdDate"] = selectedItem.createdDate
            itemMap["updatedDate"] = selectedItem.updatedDate
            itemMap["departmentId"] = json["departmentId"]
            itemMap["brandId"] = json["brandId"]
            itemMap["supplierId"] = json["supplierId"]
            itemMap["newArrivalId"] = json["newArrivalId"]
            itemSerializer = ItemSerializer(selectedItem, data=itemMap)
        else:
            itemMap["keyName"] = json["keyName"]
            itemMap["isApproved"] = True
            itemMap["isTop"] = False
            itemMap["isFavorite"] = False
            itemMap["isElectronic"] = False
            itemMap["totalViews"] = 0
            itemMap["createdDate"] = datetime.now()
            itemMap["updatedDate"] = datetime.now()
            itemMap["departmentId"] = json["departmentId"]
            itemMap["brandId"] = json["brandId"]
            itemMap["supplierId"] = json["supplierId"]
            itemMap["newArrivalId"] = json["newArrivalId"]
            itemSerializer = ItemSerializer(data=itemMap)
        
        if itemSerializer is not None:
            if itemSerializer.is_valid():
                itemSerializer.save()
            
                self.saveItemLanguageList(itemSerializer.data["id"],json["title"],selectedLanguage)

                self.saveFeatureBulletList(itemSerializer.data["id"],json["featureBulletListMap"],selectedLanguage)

                self.saveSKUList(itemSerializer.data["id"],json["skuListMap"],selectedLanguage)

    def sentence_to_uuid(self,sentence):
        return uuid.uuid5(uuid.NAMESPACE_URL, sentence)

    def saveItemLanguageList(self, itemId, itemName,selectedLanguage):
        selectedItemLanguageList = ItemLanguage.objects.filter(itemId=itemId)
        selected = None
        for current in selectedItemLanguageList:
            if current.languageId.id == selectedLanguage.id:
                selected = current
                break

        selectedMap = {}
        selectedMap["name"] = itemName
        selectedMap["description"] = itemName
        selectedMap["itemId"] = itemId
        selectedMap["languageId"] = selectedLanguage.id
        itemLanguageSerializer = None
        if selected is not None:  
            selectedMap["id"] = selected.id  
            itemLanguageSerializer = ItemLanguageSerializer(
                    selected, data=selectedMap
                )
        else:
            itemLanguageSerializer = ItemLanguageSerializer(
                    data=selectedMap
                )
        
        if itemLanguageSerializer is not None:
            if itemLanguageSerializer.is_valid():
                itemLanguageSerializer.save()
    
    def saveFeatureBulletList(self,itemId,featureBulletListMap,selectedLanguage):
        featureBulletList = FeatureBullet.objects.filter(itemId = itemId)
        for featureBulletMap in featureBulletListMap:
            serial_uuid = self.sentence_to_uuid(featureBulletMap["description"])
            isExists = False
            for featureBullet in featureBulletList:
                if featureBullet.keyName == serial_uuid.__str__():   
                    isExists = True 
                    break
            
            featureBulletNewMap = {}
            featureBulletSerializer = None
            if isExists:
                featureBulletNewMap["id"] = featureBullet.id
                featureBulletNewMap["createdDate"] = featureBullet.createdDate
                featureBulletNewMap["updatedDate"] = datetime.now()
                featureBulletNewMap["itemId"] = itemId
                featureBulletSerializer = FeatureBulletSerializer(featureBullet, data=featureBulletNewMap)
            else:
                featureBulletNewMap["id"] = 0
                featureBulletNewMap["keyName"] = serial_uuid.__str__()
                featureBulletNewMap["createdDate"] = datetime.now()
                featureBulletNewMap["updatedDate"] = datetime.now()
                featureBulletNewMap["itemId"] = itemId
                featureBulletSerializer = FeatureBulletSerializer(data=featureBulletNewMap)

            if featureBulletSerializer is not None:
                if featureBulletSerializer.is_valid():
                    featureBulletSerializer.save()
                
                    self.saveFeatureBulletLanguageList(featureBulletSerializer.data["id"],featureBulletMap["description"],selectedLanguage)
    
    def saveFeatureBulletLanguageList(self,featureBulletId,description,selectedLanguage):
        featureBulletLanguageList = FeatureBulletLanguage.objects.filter(featureBulletId = featureBulletId)
        selected = None
        for current in featureBulletLanguageList:
            if current.languageId.id == selectedLanguage.id:
                selected = current
                break
        
        featureBulletLanguageSerializer = None
        selectedMap = {}
        if selected is not None:
            selectedMap["id"] = selected.id
            selectedMap["description"] = description
            selectedMap["featureBulletId"] = selected.featureBulletId
            selectedMap["languageId"] = selectedLanguage.id
            featureBulletLanguageSerializer = FeatureBulletLanguageSerializer(selected,data=selectedMap)
        else:
            selectedMap["description"] = description
            selectedMap["featureBulletId"] = featureBulletId
            selectedMap["languageId"] = selectedLanguage.id
            featureBulletLanguageSerializer = FeatureBulletLanguageSerializer(data=selectedMap)
        
        if featureBulletLanguageSerializer is not None:
            if featureBulletLanguageSerializer.is_valid():
                featureBulletLanguageSerializer.save()

    def saveSKUList(self,itemId,skuNewList,selectedLanguage):
        skuOldList = SKU.objects.filter(itemId = itemId)

        for skuNew in skuNewList:
            selectedOld = None
            price = skuNew["price"]
            for skuOld in skuOldList:
                if skuOld.keyName == skuNew["id"]:
                    selectedOld = skuOld
                    break
            
            skuSerializer = None
            skuMap = {}
            if selectedOld is not None:
                skuMap["id"] = selectedOld.id
                skuMap["keyName"] = selectedOld.keyName
                skuMap["name"] = selectedOld.name
                skuMap["price"] = price["amount"]
                skuMap["discount"] = price["discount"]["amount"]
                skuMap["amount"] = selectedOld.amount
                skuMap["isTop"] = selectedOld.isTop 
                skuMap["isFavorite"] = selectedOld.isFavorite
                skuMap["isMostDiscount"] = selectedOld.isMostDiscount 
                skuMap["isNewArrival"] = selectedOld.isNewArrival 
                skuMap["createdDate"] = selectedOld.createdDate
                skuMap["updatedDate"] = selectedOld.updatedDate
                skuMap["itemId"] = itemId
                skuSerializer = SKUSerializer(selectedOld,data = skuMap)
            else:
                skuMap["keyName"] = skuNew["id"]
                skuMap["name"] = skuNew["id"]
                skuMap["price"] = price["amount"]
                skuMap["discount"] = price["discount"]["amount"]
                skuMap["amount"] = 5
                skuMap["isTop"] = False
                skuMap["isFavorite"] = False
                skuMap["isMostDiscount"] = False
                skuMap["isNewArrival"] = False
                skuMap["createdDate"] = datetime.now()
                skuMap["updatedDate"] = datetime.now()
                skuMap["itemId"] = itemId
                skuSerializer = SKUSerializer(data = skuMap)
            
            if skuSerializer is not None:
                if skuSerializer.is_valid():
                    skuSerializer.save()

                    self.saveSKUOptionList(skuSerializer.data["id"],skuNew["skuOptionList"],selectedLanguage)

                    self.saveSKUDescriptionList(skuSerializer.data["id"],skuNew["skuOptionList"],selectedLanguage)

                    self.saveSKUImageList(skuSerializer.data["id"],skuNew["skuImageList"])

    def saveSKUOptionList(self,skuId,newSKUOptionList,selectedLanguage):
        oldSKUOptionList = SKUOption.objects.filter(skuId = skuId)
        for newSKUOption in newSKUOptionList:
            selected = None
            for oldSKUOption in oldSKUOptionList:
                if oldSKUOption.keyName == newSKUOption["skuOptionKeyName"]:
                    selected = oldSKUOption
            
            skuOptionSerializer = None
            skuOptionMap = {}
            if selected is not None:
                skuOptionMap["id"] = selected.id
                skuOptionMap["keyName"] = selected.keyName 
                skuOptionMap["createdDate"] = selected.createdDate
                skuOptionMap["updatedDate"] = selected.updatedDate
                skuOptionMap["skuId"] = skuId
                if newSKUOption["skuOptionKeyName"] == "bodyColor":
                    selected.isColor = True
                skuOptionSerializer = SKUOptionSerializer(selected,data = skuOptionMap)
            else:
                skuOptionMap["keyName"] = newSKUOption["skuOptionKeyName"]
                skuOptionMap["createdDate"] = datetime.now()
                skuOptionMap["updatedDate"] = datetime.now()
                skuOptionMap["skuId"] = skuId
                if newSKUOption["skuOptionKeyName"] == "bodyColor":
                    skuOptionMap["isColor"] = True
                skuOptionSerializer = SKUOptionSerializer(data = skuOptionMap)
            
            if skuOptionSerializer is not None:
                if skuOptionSerializer.is_valid():
                    skuOptionSerializer.save()
                    colorName = ""
                    if "skuOptionColor" in newSKUOption:
                        colorName = newSKUOption["skuOptionColor"]
                    self.saveSKUOptionLanguageList(skuOptionSerializer.data["id"],newSKUOption["skuOptionTitle"],newSKUOption["skuOptionValue"],skuOptionSerializer.data["isColor"],colorName,selectedLanguage)

    def saveSKUOptionLanguageList(self,skuOptionId,title,value,isColor,colorName,selectedLanguage):
        skuOptionLanguageList = SKUOptionLanguage.objects.filter(skuOptionId = skuOptionId)
        selected = None
        for skuOptionLanguage in skuOptionLanguageList:
            if skuOptionLanguage.languageId.id == selectedLanguage.id:
                selected = skuOptionLanguage
                break
        
        skuOptionLanguageSerializer = None
        skuOptionLanguageMap = {}
        if selected is not None:
            skuOptionLanguageMap["id"] = selected.id
            skuOptionLanguageMap["title"] = title
            skuOptionLanguageMap["value"] = value
            
            if bool(isColor) == True:
                skuOptionLanguageMap["value"] = "0xFF"+colorName.upper()
                skuOptionLanguageMap["colorName"] = value
            else:
                skuOptionLanguageMap["value"] = value
                skuOptionLanguageMap["colorName"] = colorName

            skuOptionLanguageMap["languageId"] = selected.languageId.id
            skuOptionLanguageMap["skuOptionId"] = selected.skuOptionId.id
            skuOptionLanguageSerializer = SKUOptionLanguageSerializer(selected,data=skuOptionLanguageMap)
        else:
            skuOptionLanguageMap["title"] = title
            if bool(isColor) == True:
                skuOptionLanguageMap["value"] = "0xFF"+colorName.upper()
                skuOptionLanguageMap["colorName"] = value
            else:
                skuOptionLanguageMap["value"] = value
                skuOptionLanguageMap["colorName"] = colorName
            skuOptionLanguageMap["languageId"] = selectedLanguage.id
            skuOptionLanguageMap["skuOptionId"] = skuOptionId
            skuOptionLanguageSerializer = SKUOptionLanguageSerializer(data=skuOptionLanguageMap)
        
        if skuOptionLanguageSerializer is not None:
            if skuOptionLanguageSerializer.is_valid():
                skuOptionLanguageSerializer.save()

    def saveSKUDescriptionList(self,skuId,newSKUDescriptionList,selectedLanguage):
        oldSKUDescriptionList = SKUDescription.objects.filter(skuId = skuId)
        for newSKUDescription in newSKUDescriptionList:
            selected = None
            for oldSKUDescription in oldSKUDescriptionList:
                if oldSKUDescription.keyName == newSKUDescription["skuOptionKeyName"]:
                    selected = oldSKUDescription
            
            skuDescriptionSerializer = None
            skuDescriptionMap = {}
            if selected is not None:
                skuDescriptionMap["id"] = selected.id
                skuDescriptionMap["keyName"] = selected.keyName 
                skuDescriptionMap["createdDate"] = selected.createdDate
                skuDescriptionMap["updatedDate"] = selected.updatedDate
                skuDescriptionMap["skuId"] = skuId
                if newSKUDescription["skuOptionKeyName"] == "bodyColor":
                    selected.isColor = True
                skuDescriptionSerializer = SKUDescriptionSerializer(selected,data = skuDescriptionMap)
            else:
                skuDescriptionMap["keyName"] = newSKUDescription["skuOptionKeyName"]
                skuDescriptionMap["createdDate"] = datetime.now()
                skuDescriptionMap["updatedDate"] = datetime.now()
                skuDescriptionMap["skuId"] = skuId
                if newSKUDescription["skuOptionKeyName"] == "bodyColor":
                    skuDescriptionMap["isColor"] = True
                skuDescriptionSerializer = SKUDescriptionSerializer(data = skuDescriptionMap)
            
            if skuDescriptionSerializer is not None:
                if skuDescriptionSerializer.is_valid():
                    skuDescriptionSerializer.save()
                    colorName = ""
                    if "skuOptionColor" in newSKUDescription:
                        colorName = newSKUDescription["skuOptionColor"]

                    self.saveSKUDescriptionLanguageList(skuDescriptionSerializer.data["id"],newSKUDescription["skuOptionTitle"],newSKUDescription["skuOptionValue"],colorName,selectedLanguage)

    def saveSKUDescriptionLanguageList(self,skuDescriptionId,title,value,colorName,selectedLanguage):
        skuDescriptionLanguageList = SKUDescriptionLanguage.objects.filter(skuDescriptionId = skuDescriptionId)
        selected = None
        for skuDescriptionLanguage in skuDescriptionLanguageList:
            if skuDescriptionLanguage.languageId.id == selectedLanguage.id:
                selected = skuDescriptionLanguage
                break
        
        skuDescriptionLanguageSerializer = None
        skuDescriptionLanguageMap = {}
        if selected is not None:
            skuDescriptionLanguageMap["id"] = selected.id
            skuDescriptionLanguageMap["title"] = title
            skuDescriptionLanguageMap["value"] = value
            skuDescriptionLanguageMap["colorName"] = colorName,
            skuDescriptionLanguageMap["languageId"] = selected.languageId.id
            skuDescriptionLanguageMap["skuDescriptionId"] = selected.skuDescriptionId.id
            skuDescriptionLanguageSerializer = SKUDescriptionLanguageSerializer(selected,data=skuDescriptionLanguageMap)
        else:
            skuDescriptionLanguageMap["title"] = title
            skuDescriptionLanguageMap["value"] = value
            skuDescriptionLanguageMap["colorName"] = colorName
            skuDescriptionLanguageMap["languageId"] = selectedLanguage.id
            skuDescriptionLanguageMap["skuDescriptionId"] = skuDescriptionId
            skuDescriptionLanguageSerializer = SKUDescriptionLanguageSerializer(data=skuDescriptionLanguageMap)
        
        if skuDescriptionLanguageSerializer is not None:
            if skuDescriptionLanguageSerializer.is_valid():
                skuDescriptionLanguageSerializer.save()

    def saveSKUImageList(self,skuId,newSKUImageList):
        oldSKUImageList = SKUImage.objects.filter(skuId = skuId)
        for newSKUImage in newSKUImageList:
            selected = None
            for oldSKUImage in oldSKUImageList:
                if oldSKUImage.keyName == newSKUImage["keyName"]:
                    selected = oldSKUImage
                    break
            
            skuImageSerializer = None
            skuImageMap = {}
            if selected is not None:
                skuImageMap["id"]=selected.id
                skuImageMap["keyName"]=selected.keyName
                skuImageMap["fileName"]=selected.fileName
                skuImageMap["extension"]=selected.extension
                skuImageMap["source"]=newSKUImage["image"]
                skuImageMap["image"]=selected.image
                skuImageMap["skuId"]=selected.skuId
                skuImageSerializer = SKUImageSerializer(selected,skuImageMap)
            else:
                skuImageMap["keyName"]=newSKUImage["keyName"]
                skuImageMap["fileName"]=None
                skuImageMap["extension"]=None
                skuImageMap["source"]=newSKUImage["image"]
                skuImageMap["image"]=None
                skuImageMap["skuId"]=skuId
                skuImageSerializer = SKUImageSerializer(selected,skuImageMap)
            
            if skuImageSerializer.is_valid():
                skuImageSerializer.save()

    def convert(self,json):
        departmentId = None
        brandId = None
        supplierId = None
        newArrivalId = None
        languageList = Language.objects.filter(languageCode = 'en')
        selectedLanguage = None
        if languageList.__len__() > 0:
            selectedLanguage = languageList[0]

        if "category" in json:
            departmentId = self.saveDepartment(json["category"],selectedLanguage)
            brandId = self.saveBrand("Snowflake Redbubble",selectedLanguage)
        
        supplierId = self.saveSupplier("Redbubble Snowflake","snowflakecodev12022@gmail.com",selectedLanguage)
        newArrivalId = self.getNewArrival()
        
        if departmentId is not None and brandId is not None and supplierId is not None and newArrivalId is not None:
            json["departmentId"] = departmentId
            json["brandId"] = brandId
            json["supplierId"] = supplierId
            json["newArrivalId"] = newArrivalId
            self.saveItem(json,selectedLanguage)
        pass

class RedbubbleProductDetailData:

    def checkIfSKUOptionExists(self, skuOptionMapList, skuOptionMap):
        for current in skuOptionMapList:
            if current["skuOptionKeyName"] == skuOptionMap["skuOptionKeyName"]:
                return True
        return False

    def getSKUOptionTitle(self, configurationSetList, skuOptionTitle):
        for current in configurationSetList:
            if "name" in current and "value" in current and "attributes" in current:
                if current["name"] == "configuration":
                    if current["value"] == skuOptionTitle:
                        subAttributeList = current["attributes"]
                        for subAttribute in subAttributeList:
                            if "name" in subAttribute and "value" in subAttribute:
                                if subAttribute["name"] == "defaultText":
                                    return subAttribute["value"]

        return ""

    def getItemFromInventoryCategorySet(self, item, configSetList):
        id = None
        categoryMap = {}
        priceMap = {}
        skuOptionMapList = []

        if "id" in item:
            id = item["id"]

        if "description" in item:
            categoryMap = {"name": item["description"]}

        if "price" in item:
            priceMap = item["price"]
        
        if "previewSet" in item:
            if "previews" in item["previewSet"]:
                previews = item["previewSet"]["previews"]
                skuImageList = []
                for image in previews:
                    skuImage = {}
                    if "previewTypeId" in image:
                        if image["previewTypeId"] == "primary":
                            skuImage["keyName"] = image["previewTypeId"]
                            skuImage["image"] = image["url"]
                            skuImageList.append(skuImage)

        if "attributes" in item:
            attributesList = item["attributes"]
            for attribute in attributesList:
                skuOptionMap = {}
                if (
                    "name" in attribute
                    and "value" in attribute
                    and "attributes" in attribute
                ):
                    skuOptionMap["skuOptionKeyName"] = attribute["name"]
                    skuOptionMap["skuOptionTitle"] = attribute["value"]
                    subAttributeList = attribute["attributes"]
                    for subAttribute in subAttributeList:
                        if "name" in subAttribute and "value" in subAttribute:
                            if subAttribute["name"] == "defaultText":
                                skuOptionMap["skuOptionValue"] = subAttribute["value"]
                            if subAttribute["name"] == "hexColor":
                                skuOptionMap["skuOptionColor"] = subAttribute["value"]

                if self.checkIfSKUOptionExists(skuOptionMapList, skuOptionMap) == False:
                    title = self.getSKUOptionTitle(
                            configSetList, skuOptionMap["skuOptionKeyName"]
                    )
                    if title != "":
                        skuOptionMap["skuOptionTitle"] = title
                    skuOptionMapList.append(skuOptionMap)

        skuMap = {
            "id": id,
            "category": categoryMap,
            "price": priceMap,
            "skuOptionList": skuOptionMapList,
            "skuImageList":skuImageList
        }

        return skuMap

    def getItemDetail(self,jsonData):
        itemMap = {}

        inventoryCategorySet = None
        if "props" in jsonData and "pageProps" in jsonData["props"]:
            if "inventoryCategorySet" in jsonData["props"]["pageProps"]:
                inventoryCategorySet = jsonData["props"]["pageProps"][
                    "inventoryCategorySet"
                ]
            else:
                inventoryCategorySet = jsonData["props"]["pageProps"]

        if inventoryCategorySet is not None:
            configSetList = []

            itemKeyName = None
            itemTitle = None
            itemCategory = None
            productTypeId = None

            if "id" in inventoryCategorySet:
                itemKeyName = inventoryCategorySet["id"]

            if "categoryName" in inventoryCategorySet:
                itemCategory = inventoryCategorySet["categoryName"]

            defaultItem = None
            if "defaultItem" in inventoryCategorySet:
                defaultItem = inventoryCategorySet["defaultItem"]
                if defaultItem is not None:

                    if "productTypeId" in defaultItem:
                        productTypeId = defaultItem["productTypeId"]

                    if "work" in defaultItem:
                        work = defaultItem["work"]
                        if "title" in work:
                            itemTitle = work["title"]

            featureBulletList = []
            if "productDescriptions" in jsonData["props"]["pageProps"]:
                if productTypeId in jsonData["props"]["pageProps"]["productDescriptions"]:
                    productDescriptionList = jsonData["props"]["pageProps"]["productDescriptions"][productTypeId]
                    featureBulletList = []
                    for description in productDescriptionList:
                        featureBulletMap = {}
                        featureBulletMap["description"] = description
                        featureBulletList.append(featureBulletMap)

            if "configurationSet" in inventoryCategorySet:
                configSetList = inventoryCategorySet["configurationSet"]

            if configSetList.__len__() > 0:
                itemMap["keyName"] = itemKeyName
                itemMap["title"] = itemTitle
                itemMap["category"] = itemCategory
                itemMap["productTypeId"] = productTypeId
                itemMap["featureBulletListMap"] = featureBulletList

                if "items" in inventoryCategorySet:
                    skus = inventoryCategorySet["items"]

                skuListMap = []
                for sku in skus:
                    skuMap = self.getItemFromInventoryCategorySet(sku, configSetList)
                    skuListMap.append(skuMap)
                itemMap["skuListMap"] = skuListMap
        
        return itemMap
    
class RedbubbleProductDetailParser(HTMLParser):

    isData = False
    skuList = []

    def checkProductData(self, attrs):
        for i in attrs:
            if "id" in i and "__NEXT_DATA__" in i:
                self.isData = True

    def handle_starttag(self, tag, attrs):
        if tag == "script":
            self.checkProductData(attrs)

    def handle_endtag(self, tag):
        pass

    def handle_data(self, data):
        print(self.isData)
        if self.isData:
            try:
                jsonData = json.loads(data)
                productDetail =  RedbubbleProductDetailData()
                itemMap = productDetail.getItemDetail(jsonData)
                convertData = ConvertData()
                convertData.convert(itemMap)

            except json.JSONDecodeError:
                print("Failed to decode JSON data")


class GetRedbubbleProductDetailPageView(View):
    def post(self, request, *args, **kwargs):
        jsonData = json.loads(request.body)
        urlMapList = {}
        if "htmlPage" in jsonData:
            parser = RedbubbleProductDetailParser()
            parser.feed(jsonData["htmlPage"])

        urlMapList["isSuccess"] = False
        return JsonResponse(
            urlMapList,
            safe=False,
        )
