import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import re
# import bs4
from bs4 import BeautifulSoup as b
import requests
import time

# Initial Search Query URL to Find ItemCondition Code
while True:
    print("Title Search:")

    start = time.time()

    SQ = str(input())
    SQ_1 = SQ.replace(' ', '+').replace('NEW',
                                        '').replace('men', '').replace('women', '')
    gender = ['Men', 'Women']

    # &color[]=color
    posh_colors = ['Red', 'Pink', 'Orange', 'Yellow', 'Green', 'Blue', 'Purple',
                   'Gold', 'Silver', 'Black', 'Gray', 'White', 'Cream', 'Brown', 'Tan']

    for i in range(0, len(posh_colors)):
        if posh_colors[i] in SQ:
            url_color = '&color[]=' + posh_colors[i]
            url_separator = "&availability=sold_out" + url_color + "&department=All&max_id="
            url_separator1 = "&availability=sold_out" + url_color + \
                "&condition=nwt_and_ret&department=All&max_id="
            SQ_1 = SQ_1.replace(posh_colors[i], '')
            break
        else:
            url_separator = "&availability=sold_out&department=All&max_id="
            url_separator1 = "&availability=sold_out&condition=nwt_and_ret&department=All&max_id="

    prices = []
    prices1 = []
    base_url = "https://poshmark.com/search?query="

    pg = 1
    url = base_url + SQ_1 + url_separator + str(pg)
    url_1 = base_url + SQ_1 + url_separator1 + str(pg)
    url_a = base_url + SQ_1 + url_separator + str(pg)
    url_1b = base_url + SQ_1 + url_separator1 + str(pg)

    html = requests.get(url).text
    html1 = requests.get(url_1).text
    soup = b(html, "lxml")
    soup1 = b(html1, 'lxml')

    # Begin new and used condition items price list:

    for listing in soup.findAll("div", {"class": "item__details"}):
        price = listing.findAll("span", {"class": "p--t--1 fw--bold"})[0].text
        indices = price.find('$')
        price = price[indices + 1:]
        space = price.find(' ')
        price = int(price[:space - 1])
        prices.append(price)

    while True:
        last_page = soup.find_all(string=re.compile('No Listings Found'))
        if last_page:
            break
        pg = pg + 1
        url = base_url + SQ_1 + url_separator + str(pg)
        html = requests.get(url).text
        soup = b(html, "lxml")

        for listing in soup.findAll("div", {"class": "item__details"}):
            price = listing.findAll(
                "span", {"class": "p--t--1 fw--bold"})[0].text
            # indices = [i for i, dollars in enumerate(price) if dollars == '$']
            # price = int(price[1:indices[1]-1])
            indices = price.find('$')
            price = price[indices + 1:]
            space = price.find(' ')
            price = int(price[:space - 1])
            prices.append(price)

    # Begin new condition item prices list:

    for listing in soup1.findAll("div", {"class": "item__details"}):
        price1 = listing.findAll("span", {"class": "p--t--1 fw--bold"})[0].text
        # indices = [i for i, dollars in enumerate(price1) if dollars == '$']
        # price1 = int(price1[1:indices[1]-1])
        indices = price1.find('$')
        price1 = price1[indices + 1:]
        space = price1.find(' ')
        price1 = int(price1[:space - 1])
        prices1.append(price1)

    while True:

        last_page = soup1.find_all(string=re.compile('No Listings Found'))
        if last_page:
            break
        pg = pg + 1
        url_1 = base_url + SQ_1 + url_separator1 + str(pg)
        html1 = requests.get(url_1).text
        soup1 = b(html1, "lxml")

        for listing in soup1.findAll("div", {"class": "item__details"}):
            price1 = listing.findAll(
                "span", {"class": "p--t--1 fw--bold"})[0].text
            # indices = [i for i, dollars in enumerate(price1) if dollars == '$']
            # price1 = int(price1[1:indices[1]-1])
            indices = price1.find('$')
            price1 = price1[indices + 1:]
            space = price1.find(' ')
            price1 = int(price1[:space - 1])
            prices1.append(price1)

    # Begin Element-wise substraction of new condition items price list from new&used items price list:
    print(len(prices), 'NEW & USED')
    print(len(prices1), 'NEW')

    end = time.time()

    print(end - start)

    for element in prices1:
        prices.remove(element)

    if 'NEW' in SQ:
        kde_datapoints = sns.kdeplot(prices1, shade=True).get_lines()[
            0].get_data()
        sns.rugplot(prices1)
        print(str(len(prices1)) + " Results" + "\n")
        print("Average Price Sold New = $" + str(np.mean(prices1)) + "\n")
        total_price = np.mean(prices1) + 6.79
        print("Average Total Price New = $" + str(total_price) + "\n")
        print("Flat Rate Shipping = $6.79" + "\n")

        kde_x = kde_datapoints[0]
        kde_y = kde_datapoints[1]
        optimal_price = kde_x[np.argmax(kde_y)]
        print("Optimal Price New = $" + str(optimal_price) + "\n")
        print("Optimal Price Including Shipping New = $" +
              str(optimal_price + 6.79) + "\n")
        print("URL Link (New): " + url_1b + "\n")
        plt.ylabel('KDE')
        plt.xlabel('Price ($)')
        plt.show()
    else:
        try:

            kde_datapoints = sns.kdeplot(prices, shade=True).get_lines()[
                0].get_data()
            sns.rugplot(prices)
            print(str(len(prices)) + " Results" + "\n")
            print("Average Price Sold Used = $" + str(np.mean(prices)) + "\n")
            total_price = np.mean(prices) + 6.79
            print("Average Total Price Used = $" + str(total_price) + "\n")
            print("Flat Rate Shipping = $6.79" + "\n")
            import winsound
            winsound.Beep(440, 300)

            kde_x = kde_datapoints[0]
            kde_y = kde_datapoints[1]
            optimal_price = kde_x[np.argmax(kde_y)]
            print("Optimal Price Used = $" + str(optimal_price) + "\n")
            print("Optimal Price Including Shipping Used = $" +
                  str(optimal_price + 6.79) + "\n")
            print("URL Link: " + url_a + "\n")
            plt.ylabel('KDE')
            plt.xlabel('Price ($)')
            plt.show()
        except IndexError:
            print('\n' + '0 results' + '\n')
            pass
