import json
import re
import time
import urllib

import unidecode
from bs4 import BeautifulSoup
from django.db.models import Sum
from django.utils import timezone

from accounts.models import PlayerCard
from sbc.models import SBCType, SBCTarget, SBCSolvationModel
from sbc.public_methods import get_player_fifa_name_by_id
from sbc.sbc_solver import SBCSolver
from utils.realy_public_methods import new_print


class FutbinTools(SBCSolver):
    def update_sbc_from_futbin(self, sbc_type: SBCType, must_change=False):
        sbc_number = sbc_type.sbc_number
        new_print(self.fifa_account, 'check for a new sbc soloution')
        # TODO : fix this
        # if os.name == 'nt':
        #     return ''
        # sbc_type = SBCType.objects.filter(sbc_number=sbc_number).exclude(must_done=2).last()
        sbc_id = sbc_type.id
        sbc_link = sbc_type.sbc_link
        # if sbc_type.console_cant_use:
        #     must_change = True
        # id_list = self.id_list_dic[sbc_number]
        minimum_likes = -100
        # resp = self.se2.get('https://www.futbin.com/squad-building-challenges/ALL/%s/page=1?page=1&loyalty=off&pos=off&version=gold'%sbc_number)

        # link = ''
        # squad_price = 0
        if sbc_type.sbc_special_link:
            update_result = self.update_sbc_from_futbin_link(
                link=sbc_type.sbc_special_link, sbc_number=sbc_number, must_change=must_change, check_bad_player=False)
            return update_result
        ps4_max_price = 0
        platforms = ['ps4', 'xone', 'pc']
        for platform in platforms:
            new_print(self.fifa_account, 'check platform = ', platform)
            if sbc_number == 18 or sbc_number == 36:
                resp = self.use_cloud_scraper(
                    'https://www.futbin.com/squad-building-challenges/ALL/%s/page=1?page=1&lowest=%s&pos=off' % (
                        sbc_number, platform)).text
                # minimum_likes = 0
            else:
                resp = self.use_cloud_scraper(
                    'https://www.futbin.com/squad-building-challenges/ALL/%s/page=1?page=1&lowest=%s&loyalty=off&pos=off' % (
                        sbc_number, platform)).text

            # soup = BeautifulSoup(resp.text, "html.parser")
            soup = BeautifulSoup(resp, "html.parser")
            squads = soup.find_all(class_="squad_url")
            if not ps4_max_price:
                temp_squad = squads[-1]
                likes = temp_squad.parent.findNext('td').findNext('td').findNext('td')
                ps4_max_price = int(likes.findNext('td').text.replace(',', ''))

            for squad in squads:
                bad_player = 0
                likes = squad.parent.findNext('td').findNext('td').findNext('td')
                # loyalty = likes.findNext('td').findNext('td').findNext('td').findNext('td').findNext('td')
                # if sbc_number == 20 and int(str(loyalty.text)) > 5:
                #     continue
                # if sbc_number == 36 and int(str(loyalty.text)) > 6:
                #     continue

                # if int(str(likes.text).replace(',', '')) >= minimum_likes or sbc_type.sbc_special_link:
                if sbc_type.sbc_special_link:
                    link = squad['href']
                    squad_price = int(likes.findNext('td').text.replace(',', ''))
                    if squad_price > ps4_max_price + 2000:
                        continue
                    if squad_price > 40000:
                        continue
                    new_print(self.fifa_account, 'new squad price = ', squad_price)
                    # break

                    new_print(self.fifa_account, 'link = ', link)
                    # print 'squad_price = ',squad_price
                    if (link and squad_price) or sbc_type.sbc_special_link:
                        targets_list = []
                        link = 'https://www.futbin.com' + link
                        if (link == sbc_link) or (sbc_type.sbc_special_link == sbc_link):
                            if must_change:
                                new_print(self.fifa_account, 'squad link is the same but must change the solution')
                                continue
                            new_print(self.fifa_account, 'squad still the same')
                            sbc_type.refresh_from_db()
                            sbc_type.price = squad_price + 2000
                            sbc_type.save()
                            # cursor2.execute('update sbc set sbc_price = %s where id = %s ', (squad_price + 1500, sbc_id))
                            new_print(self.fifa_account, f'sbc id  {sbc_id} price updated')
                            return 'updated'
                        return self.update_sbc_from_futbin_link(
                            link=link, sbc_number=sbc_number, must_change=must_change)
                        # resp = self.se2.get(link)
                        # resp = self.use_cloud_scraper(link)
                        # # soup = BeautifulSoup(resp.content, "html.parser")
                        # soup = BeautifulSoup(resp, "html.parser")
                        # page_info = soup.find(id='page-info')
                        # new_print(self.fifa_account, 'formation', page_info['data-formation'])
                        # id_list = self.squad_positions_in_futbin[page_info['data-formation']]
                        # for per_id in id_list:
                        #     target = soup.find(id=per_id)
                        #     target_link_a = target.find(class_="get-tp")
                        #     target_link = 'https://www.futbin.com' + target_link_a['href']
                        #     new_print(self.fifa_account, 'target_link = ', target_link)
                        #     target_div = target.find(class_="pcdisplay")
                        #     player_asset_id = target_div['data-base-id']
                        #     player_club = target_div['data-player-club']
                        #     # player_position = target_div['data-formpos-original']
                        #     player_position = target.find(class_='pcdisplay-pos').text
                        #     player_rare = target_div['data-rare']
                        #     player_rare_type = target_div['data-rare-type']
                        #     player_rating = target_div['data-rating']
                        #     original_pos = target_div['data-formpos-original']
                        #     if original_pos != player_position:
                        #         new_print(self.fifa_account, 'current pos is not the same of original pos ...')
                        #         new_print(self.fifa_account, 'current_pos = ', player_position, 'original_pos = ',
                        #                   original_pos)
                        #         bad_player = 1
                        #         break
                        #     # if int(player_rare) > 1 or int(player_rare_type) > 1:
                        #     #     new_print(self.fifa_account, 'rare player found')
                        #     #     # resp2 = self.use_cloud_scraper(target_link)
                        #     #     # # soup = BeautifulSoup(resp.content, "html.parser")
                        #     #     # soup2 = BeautifulSoup(resp2, "html.parser")
                        #     #     # player_versions = soup2.find(class_="player-versions")
                        #     #     # try:
                        #     #     #     pversions = player_versions.find_all(class_="pversion")
                        #     #     #     if pversions:
                        #     #     #         new_print(self.fifa_account,'####### another card founded')
                        #     #     #         bad_player = 1
                        #     #     #         break
                        #     #     #     else:
                        #     #     #         new_print(self.fifa_account,'player hasnt any other card')
                        #     #     # except:
                        #     #     #     new_print(self.fifa_account,'player hasnt any other card')
                        #     #     bad_player = 1
                        #     #     break
                        #
                        #     # return ''
                        #     # if sbc_number != 20 and sbc_number != 36:
                        #     if True:
                        #         resp2 = self.use_cloud_scraper(target_link)
                        #         # soup = BeautifulSoup(resp.content, "html.parser")
                        #         soup2 = BeautifulSoup(resp2, "html.parser")
                        #         player_versions = soup2.find(class_="player-versions")
                        #         # try:
                        #         #     pversions = player_versions.find_all(class_="pversion")
                        #         #     if pversions:
                        #         #         for pversion in pversions:
                        #         #             a_elem = pversion.find('a')
                        #         #             new_print(self.fifa_account, a_elem['data-level'], a_elem['data-rare-type'])
                        #         #             if a_elem['data-level'] == 'gold' and int(a_elem['data-rare-type']) <= 1:
                        #         #                 new_print(self.fifa_account, '####### another gold card founded')
                        #         #                 bad_player = 1
                        #         #                 break
                        #         #     else:
                        #         #         new_print(self.fifa_account, 'player hasnt any other card')
                        #         # except:
                        #         #     new_print(self.fifa_account, 'player hasnt any other card')
                        #         player_club_tag = soup2.find(class_="info_club")
                        #         player_club_name = player_club_tag.parent.text[1:]
                        #
                        #     if bad_player:
                        #         break
                        #     player_price, updated_time, first_price, futbin_min_price, futbin_max_price = self.get_price_from_futbin_and_futwiz(
                        #         player_asset_id)
                        #     update_time_is_ok = 1
                        #     # update_time_is_ok = 0
                        #     # try:
                        #     #     if updated_time.find('secs') != -1:
                        #     #         update_time_is_ok = 1
                        #     #     elif updated_time.find('mins') != -1:
                        #     #         updated_time_min = updated_time.split()
                        #     #         if int(updated_time_min[0]) < 31:
                        #     #             update_time_is_ok = 1
                        #     # except:
                        #     #     print traceback.format_exc()
                        #     if not update_time_is_ok:
                        #         new_print(self.fifa_account, 'updated time is more than defined , ', updated_time)
                        #         bad_player = 1
                        #         break
                        #     targets_list.append(
                        #         [player_asset_id, player_position, player_price, player_club, player_rare_type,
                        #          player_rating, player_club_name])
                        #     time.sleep(1)
                        # if bad_player:
                        #     new_print(self.fifa_account, 'its not a good solution')
                        #     continue
                        # new_print(self.fifa_account, 'len(targets_list) = ', len(targets_list))
                        # # if len(targets_list) == 11:
                        # if True:
                        #     count = 0
                        #     for target in targets_list:
                        #         player_name = get_player_fifa_name_by_id(target[0])
                        #         new_print(self.fifa_account, 'player name = ', player_name)
                        #         need_to_create = 0
                        #         try:
                        #             sbc_target, created = SBCTarget.objects.get_or_create(sbc_type=sbc_type,
                        #                                                                   position=count)
                        #         except:
                        #             need_to_create = 1
                        #
                        #         player_card, created = PlayerCard.objects.get_or_create(
                        #             asset_id=target[0], rare=target[4], rating=target[5]
                        #         )
                        #         player_card.position = target[1]
                        #         player_card.team = target[3]
                        #         player_card.team_name = target[6]
                        #         player_card.name = player_name
                        #         player_card.unicode_name = unidecode.unidecode(player_name.lower())
                        #         player_card.update_time = timezone.localtime() - timedelta(hours=1)
                        #         player_card.price = target[2]
                        #         player_card.save()
                        #
                        #         if need_to_create:
                        #             sbc_target, created = SBCTarget.objects.get_or_create(
                        #                 sbc_type=sbc_type, position=count, player=player_card)
                        #         sbc_target.asset_id = target[0]
                        #         sbc_target.price = target[2]
                        #         sbc_target.pos = target[1]
                        #         sbc_target.team = target[3]
                        #         sbc_target.rare = target[4]
                        #         sbc_target.rating = target[5]
                        #         sbc_target.team_name = target[6]
                        #         sbc_target.player = player_card
                        #         sbc_target.name = player_name
                        #         sbc_target.save()
                        #         sum_sbc_price = SBCTarget.objects.filter(sbc_type=sbc_type).aggregate(
                        #             sum_price=Sum('price')).get('sum_price')
                        #         new_print(self.fifa_account, 'sum actual price =', sum_sbc_price)
                        #         sbc_type.refresh_from_db()
                        #         sbc_type.sbc_actual_price = sum_sbc_price
                        #         sbc_type.save()
                        #         # cursor2.execute('update sbc_targets set asset_id = %s , price = %s , pos = %s , club_id = %s where \
                        #         #  sbc_id = %s and position = %s',
                        #         #                 (target[0], target[2], target[1], target[3], sbc_id, count))
                        #         count += 1
                        #     sbc_type.refresh_from_db()
                        #     sbc_type.sbc_link = link
                        #     sbc_type.sbc_price = squad_price + 2000
                        #     # sbc_type.console_cant_use = False
                        #     # sbc_type.console_can_use = False
                        #     sbc_type.save()
                        #     new_print(self.fifa_account, 'new squad price = ', squad_price + 2000,
                        #               'sbc id ', sbc_id, ' updated')
                        #     # new_print(self.fifa_account, 'sbc id %s updated' % sbc_id)
                        #     return 'updated'
                        # else:
                        #     new_print(self.fifa_account, 'some thing wrong')
                        #     new_print(self.fifa_account, 'targets_list = ', targets_list)
                    else:
                        new_print(self.fifa_account, 'no enough likes or no price')
                else:
                    new_print(self.fifa_account, 'no special link found. please set futbin link')
        return ''

    def update_sbc_from_futbin_link(self, link, sbc_number, must_change=False, squad_price=0, check_bad_player=True):
        sbc_type = SBCType.objects.filter(sbc_number=sbc_number).exclude(must_done=2).last()
        sbc_id = sbc_type.id
        sbc_link = sbc_type.sbc_link
        minimum_likes = -100
        resp = self.use_cloud_scraper(link)
        new_print(self.fifa_account, 'scrapper result status : ', resp, ' text : ', resp.text[:10000])
        react_data = re.search(r'data-react-data="">(.+)</script>', resp.text).group(1)
        react_data = json.loads(str(react_data.replace('&quot;', '"')))
        bad_players = 0
        targets_list = []
        fifa_player_position = -1
        squad_data = react_data.get('squadData').get('squad')
        for iii in self.squad_positions_in_futbin[react_data.get('sbcChallengeRequirementData').get('formation').get('displayName')]:
            data_position_idx = None
            for data_position, player_data in enumerate(squad_data):
                if data_position == 0 or data_position % 2 == 0:
                    if player_data['value'] == iii:
                        data_position_idx = data_position+1
                        break
            fifa_player_position += 1
            if not data_position_idx:
                continue
            player_data = squad_data[data_position_idx]
            player_name = player_data.get('playerName')
            player_asset_id = int(re.search(
                r'img/players/(.\d+)\.png',
                str(urllib.parse.unquote(player_data.get('playerImage').get('fixed').get('url').get('image1x')))
            ).group(1).replace('p', ''))
            player_futbin_id = player_data.get('id')
            player_club = player_data.get('clubId').get('value')
            player_club_name = player_data.get('chemistryCard').get('chemData').get('clubName')
            player_rating = player_data.get('statsCard').get('rating')
            player_position = player_data.get('position').get('value')
            player_rare_type = player_data.get('rareType')
            club_id = player_data.get('clubId')
            league_id = player_data.get('leagueId')
            nation_id = player_data.get('nationId')
        # soup = BeautifulSoup(resp, "html.parser")
        # page_info = soup.find(id='page-info')
        # id_list = self.squad_positions_in_futbin[page_info['data-formation']]
        # bad_players = 0
        # targets_list = []
        # fifa_player_position = -1
        # for per_id in id_list:
        #     fifa_player_position += 1
        #     target = soup.find(id=per_id)
        #     target_link_a = target.find(class_="get-tp")
        #     if not target_link_a:
        #         continue
        #     target_link = 'https://www.futbin.com' + target_link_a['href']
        #     new_print(self.fifa_account, 'target_link = ', target_link)
        #     target_div = target.find(class_="pcdisplay")
        #     player_asset_id = target_div['data-base-id']
        #     player_club = target_div['data-player-club']
        #     # player_position = target_div['data-formpos-original']
        #     player_position = target.find(class_='pcdisplay-pos').text
        #     player_rare = target_div['data-rare']
        #     player_rare_type = target_div['data-rare-type']
        #     player_rating = target_div['data-rating']
        #     original_pos = target_div['data-formpos-original']
        #     if original_pos != player_position and check_bad_player:
        #         new_print(self.fifa_account, 'current pos is not the same of original pos ...',
        #                   'current_pos = ', player_position, 'original_pos = ',
        #                   original_pos)
        #         return 'bad player'
            # if int(player_rare) > 1 or int(player_rare_type) > 1:
            #     new_print(self.fifa_account, 'rare player found')
            #     # resp2 = self.use_cloud_scraper(target_link)
            #     # # soup = BeautifulSoup(resp.content, "html.parser")
            #     # soup2 = BeautifulSoup(resp2, "html.parser")
            #     # player_versions = soup2.find(class_="player-versions")
            #     # try:
            #     #     pversions = player_versions.find_all(class_="pversion")
            #     #     if pversions:
            #     #         new_print(self.fifa_account,'####### another card founded')
            #     #         bad_player = 1
            #     #         break
            #     #     else:
            #     #         new_print(self.fifa_account,'player hasnt any other card')
            #     # except:
            #     #     new_print(self.fifa_account,'player hasnt any other card')
            #     bad_player = 1
            #     break

            # return ''
            # if sbc_number != 20 and sbc_number != 36:
            # if True:
            #     resp2 = self.use_cloud_scraper(target_link).text
            #     # soup = BeautifulSoup(resp.content, "html.parser")
            #     soup2 = BeautifulSoup(resp2, "html.parser")
            #     player_versions = soup2.find(class_="player-versions")
            #     # try:
            #     #     pversions = player_versions.find_all(class_="pversion")
            #     #     if pversions:
            #     #         for pversion in pversions:
            #     #             a_elem = pversion.find('a')
            #     #             new_print(self.fifa_account, a_elem['data-level'], a_elem['data-rare-type'])
            #     #             if a_elem['data-level'] == 'gold' and int(a_elem['data-rare-type']) <= 1:
            #     #                 new_print(self.fifa_account, '####### another gold card founded')
            #     #                 bad_player = 1
            #     #                 break
            #     #     else:
            #     #         new_print(self.fifa_account, 'player hasnt any other card')
            #     # except:
            #     #     new_print(self.fifa_account, 'player hasnt any other card')
            #     player_club_tag = soup2.find(class_="info_club")
            #     player_club_name = player_club_tag.parent.text[1:]

            try:
                futbin_result = self.get_price_from_futbin_and_futwiz(player_futbin_id)
            except AttributeError:
                futbin_result = {}
            player_price = futbin_result.get('price', 0)
            updated_time = futbin_result.get('update_time')
            # first_price = futbin_result.get('first_price')
            # futbin_min_price = futbin_result.get('futbin_min_price')
            # futbin_max_price = futbin_result.get('futbin_max_price')
            # player_price, updated_time, first_price, futbin_min_price, futbin_max_price = self.get_price_from_futbin_and_futwiz(
            #     player_asset_id)
            update_time_is_ok = 1
            # update_time_is_ok = 0
            # try:
            #     if updated_time.find('secs') != -1:
            #         update_time_is_ok = 1
            #     elif updated_time.find('mins') != -1:
            #         updated_time_min = updated_time.split()
            #         if int(updated_time_min[0]) < 31:
            #             update_time_is_ok = 1
            # except:
            #     print traceback.format_exc()
            if not update_time_is_ok and check_bad_player:
                new_print(self.fifa_account, 'updated time is more than defined , ', updated_time)
                return 'not good solution'
            targets_list.append(
                [player_asset_id, player_position, player_price, player_club, player_rare_type,
                 player_rating, player_club_name, fifa_player_position])
            time.sleep(1)
        new_print(self.fifa_account, 'len(targets_list) = ', len(targets_list))
        # if len(targets_list) == 11:
        if True:
            count = 0
            for target in targets_list:
                player_name = get_player_fifa_name_by_id(target[0])
                new_print(self.fifa_account, 'player name = ', player_name)
                need_to_create = 0
                try:
                    sbc_target, created = SBCTarget.objects.get_or_create(sbc_type=sbc_type,
                                                                          position=target[7])
                except:
                    need_to_create = 1

                player_card, created = PlayerCard.objects.get_or_create(
                    asset_id=target[0], rare=target[4], rating=target[5]
                )
                player_card.position = target[1]
                player_card.team = target[3]
                player_card.team_name = target[6]
                player_card.name = player_name
                player_card.unicode_name = unidecode.unidecode(player_name.lower())
                player_card.update_time = timezone.localtime() - timezone.timedelta(hours=1)
                player_card.price = target[2]
                player_card.save()

                if need_to_create:
                    sbc_target, created = SBCTarget.objects.get_or_create(
                        sbc_type=sbc_type, position=target[7], player=player_card)
                sbc_target.asset_id = target[0]
                sbc_target.price = target[2]
                sbc_target.pos = target[1]
                sbc_target.team = target[3]
                sbc_target.rare = target[4]
                sbc_target.rating = target[5]
                sbc_target.team_name = target[6]
                sbc_target.player = player_card
                sbc_target.name = player_name
                sbc_target.save()
                sum_sbc_price = SBCTarget.objects.filter(sbc_type=sbc_type).aggregate(
                    sum_price=Sum('price')).get('sum_price')
                new_print(self.fifa_account, 'sum actual price =', sum_sbc_price)
                # sbc_type.refresh_from_db()
                sbc_type.sbc_actual_price = sum_sbc_price or (squad_price + 2000)
                sbc_type.save(update_fields=['sbc_actual_price'])
                # cursor2.execute('update sbc_targets set asset_id = %s , price = %s , pos = %s , club_id = %s where \
                #  sbc_id = %s and position = %s',
                #                 (target[0], target[2], target[1], target[3], sbc_id, count))
                count += 1
            sbc_type.refresh_from_db()
            sbc_type.sbc_link = link
            sbc_type.sbc_price = squad_price + 2000
            # sbc_type.console_cant_use = False
            # sbc_type.console_can_use = False
            sbc_type.save()
            new_print(self.fifa_account, 'new squad price = ', squad_price + 2000,
                      'sbc id ', sbc_id, ' updated')
            # new_print(self.fifa_account, 'sbc id %s updated' % sbc_id)
            return 'updated'
        else:
            new_print(self.fifa_account, 'some thing wrong')
            new_print(self.fifa_account, 'targets_list = ', targets_list)


    def update_solvation_from_futbin_link(self, link, solvation_object:SBCSolvationModel, fifa_account):
        # link = 'https://www.futbin.com/squad-building-challenges/ALL/2007/losc-lille-v-olympique-lyonnais' # todo : remove me after test
        resp = self.use_cloud_scraper(link).text
        soup = BeautifulSoup(resp, "html.parser")
        formation = soup.find('div', text='Formation').parent.find_all('div')[-1]
        solvation_object.formation = formation.text.strip().replace('(', '[').replace(')', ']')
        challenge_require = soup.find(class_='sbc-requirements')
        countries_special_names = []
        countries_special_count = []
        leagues_special_names = []
        leagues_special_count = []
        clubs_special_names = []
        clubs_special_count = []
        rarity_2 = []
        num_rarity_2 = []
        min_overall = []
        num_min_overall = []
        founded_texts = []
        for challenge in challenge_require.find_all('div'):
            challenge_text = challenge.text.strip()
            if challenge_text in founded_texts:
                continue
            else:
                founded_texts.append(challenge_text)
            text_parts = challenge_text.split(' ')
            if challenge_text.startswith('# of players in squad:'):
                solvation_object.num_players = int(text_parts[-1])
            elif challenge_text.startswith('# of players from'):
                if '/nation/' in (urllib.parse.unquote(challenge.find('img')['src'])):
                    new_print(fifa_account, 'find nation')
                    countries_id_list = []
                    for country in challenge.findAll('img'):
                        country_id = re.search(
                            r'nation/(\d+)\.png',
                            urllib.parse.unquote(country['src'])).group(1)
                        countries_id_list.append(str(country_id))
                    countries_special_names.append(countries_id_list)
                    if text_parts[-1].isnumeric():
                        countries_special_count.append(int(text_parts[-1]))
                    else:
                        countries_special_count.append(1)
                    new_print(fifa_account, 'players from : ', countries_special_names, countries_special_count)
                elif '/league/' in (urllib.parse.unquote(challenge.find('img')['src'])):
                    new_print(fifa_account, 'find league')
                    leagues_id_list = []
                    for league in challenge.findAll('img'):
                        league_id = re.search(r'league/dark/(\d+)\.png', urllib.parse.unquote(league['src'])).group(1)
                        leagues_id_list.append(str(league_id))
                    leagues_special_names.append(leagues_id_list)
                    if text_parts[-1].isnumeric():
                        leagues_special_count.append(int(text_parts[-1]))
                    else:
                        leagues_special_count.append(1)
                elif '/clubs/' in (urllib.parse.unquote(challenge.find('img')['src'])):
                    new_print(fifa_account, 'find club')
                    clubs_id_list = []
                    for club in challenge.findAll('img'):
                        club_id = re.search(r'clubs/dark/(\d+)\.png', urllib.parse.unquote(club['src'])).group(1)
                        similar_club_ids = self.teams_dict_data.get(int(club_id))['similar_team_ids']
                        for iie in similar_club_ids:
                            clubs_id_list.append(str(iie))
                    clubs_special_names.append(clubs_id_list)
                    if text_parts[-1].isnumeric():
                        clubs_special_count.append(int(text_parts[-1]))
                    else:
                        clubs_special_count.append(1)
            elif challenge_text.startswith('Nationalities: '):
                solvation_object.num_unique_country = str([int(text_parts[2]), text_parts[1]])
            elif challenge_text.startswith('Leagues: '):
                solvation_object.num_unique_league = str([int(text_parts[2]), text_parts[1]])
            elif challenge_text.startswith('Clubs: '):
                solvation_object.num_unique_club = str([int(text_parts[2]), text_parts[1]])
            elif challenge_text.startswith('Same Nation Count'):
                if text_parts[-2] == 'Min':
                    solvation_object.min_num_country = int(text_parts[-1])
                elif text_parts[-2] == 'Max':
                    solvation_object.max_num_country = int(text_parts[-1])
                else:
                    new_print(fifa_account, 'error : what the faz?')
            elif challenge_text.startswith('Same League Count'):
                if text_parts[-2] == 'Min':
                    solvation_object.min_num_league = int(text_parts[-1])
                elif text_parts[-2] == 'Max':
                    solvation_object.max_num_league = int(text_parts[-1])
                else:
                    new_print(fifa_account, 'error : what the faz?')
            elif challenge_text.startswith('Same Club Count'):
                if text_parts[-2] == 'Min':
                    solvation_object.min_num_club = int(text_parts[-1])
                elif text_parts[-2] == 'Max':
                    solvation_object.max_num_club = int(text_parts[-1])
                else:
                    new_print(fifa_account, 'error : what the faz?')
            elif challenge_text.startswith('Player Level:'):
                if text_parts[-2] == 'Exactly':
                    rarity_2.append([text_parts[-1], 'Exactly'])
                    num_rarity_2.append(11)
                elif text_parts[-2] == 'Min':
                    if text_parts[-1] == 'Bronze':
                        min_overall.append(55)
                        num_min_overall.append(11)
                    elif text_parts[-1] == 'Silver':
                        min_overall.append(65)
                        num_min_overall.append(11)
                    elif text_parts[-1] == 'Gold':
                        min_overall.append(75)
                        num_min_overall.append(11)
                    else:
                        new_print(fifa_account, 'error : unknown player level')
                else:
                    new_print(fifa_account, 'error : unknown player level 2')
                new_print(fifa_account, 'player level : ', text_parts[-2], text_parts[-1])
            elif challenge_text.startswith('Bronze Players:'):
                if text_parts[-2] == 'Exactly':
                    rarity_2.append(['Bronze', 'Exactly'])
                    num_rarity_2.append(int(text_parts[-1]))
                elif text_parts[-2] == 'Min':
                    rarity_2.append(['Bronze', 'Min'])
                    num_rarity_2.append(int(text_parts[-1]))
                else:
                    new_print(fifa_account, 'error : unknown bronze player level')
                new_print(fifa_account, 'player bronze : ', text_parts[-2], int(text_parts[-1]))
            elif challenge_text.startswith('Silver Players:'):
                if text_parts[-2] == 'Exactly':
                    rarity_2.append(['Silver', 'Exactly'])
                    num_rarity_2.append(int(text_parts[-1]))
                elif text_parts[-2] == 'Min':
                    rarity_2.append(['Silver', 'Min'])
                    num_rarity_2.append(int(text_parts[-1]))
                else:
                    new_print(fifa_account, 'error : unknown silver player level')
                new_print(fifa_account, 'player silver : ', text_parts[-2], int(text_parts[-1]))
            elif challenge_text.startswith('Gold Players:'):
                if text_parts[-2] == 'Exactly':
                    rarity_2.append(['Gold', 'Exactly'])
                    num_rarity_2.append(int(text_parts[-1]))
                elif text_parts[-2] == 'Min':
                    rarity_2.append(['Gold', 'Min'])
                    num_rarity_2.append(int(text_parts[-1]))
                else:
                    new_print(fifa_account, 'error : unknown Gold player level')
                new_print(fifa_account, 'player Gold : ', text_parts[-2], text_parts[-1])
            elif challenge_text.startswith('Team Chemistry:'):
                solvation_object.chemistry = int(text_parts[-1])
                new_print(fifa_account, 'chemistry points : ', text_parts[-1])
            elif challenge_text.startswith('Squad Rating:'):
                solvation_object.squad_rating = int(text_parts[-1])
                new_print(fifa_account, 'challenge rating : ', text_parts[-1])
            elif challenge_text.startswith('Rare:') or challenge_text.startswith('RARE Players:') or challenge_text.startswith('Gold Rare or Silver Rare or Bronze Rare Players'):
                rarity_2.append(['Rare', 'Min'])
                num_rarity_2.append(int(text_parts[-1]))
            elif challenge_text.startswith('Any TOTS') or challenge_text.startswith('Any TOTW'):
                num_rarity_2.append(int(text_parts[-1]))
                rarities_id_list = []
                for rarities_id in challenge.findAll('img'):
                    rare_id = re.search(r'cards/(\d+)_', urllib.parse.unquote(rarities_id['src'])).group(1)
                    rarities_id_list.append(str(rare_id))
                if text_parts[-2] == 'Min':
                    rarity_2.append([item_1 for ii in rarities_id_list for item_1 in (ii, 'Exactly')])
                elif text_parts[-2] == 'Exactly':
                    rarity_2.append([item_1 for ii in rarities_id_list for item_1 in (ii, 'Exactly')])
            elif challenge_text.startswith('Gold Players:'):
                rarity_2.append(['Gold', 'Min'])
                num_rarity_2.append(int(text_parts[-1]))
                print('rarity 2: ', rarity_2, num_rarity_2)
            else:
                print('error : i cant find any match : ', challenge_text)
        if countries_special_names:
            solvation_object.country = str(countries_special_names)
            solvation_object.num_country = str(countries_special_count)
        if leagues_special_names:
            solvation_object.league = str(leagues_special_names)
            solvation_object.num_league = str(leagues_special_count)
        if clubs_special_names:
            solvation_object.club = str(clubs_special_names)
            solvation_object.num_club = str(clubs_special_count)
        if rarity_2:
            solvation_object.rarity_2 = str(rarity_2)
            solvation_object.num_rarity_2 = str(num_rarity_2)
        if min_overall:
            solvation_object.min_overall = str(min_overall)
            solvation_object.num_min_overall = str(num_min_overall)
        solvation_object.save()
        return 'updated'



