import json
import logging

from persianBrokers import utils

logger = logging.getLogger()


def get_stocks():
    url = "https://core.tadbirrlc.com//StocksHandler.ashx?%7B%22Type%22:%22ALL21%22,%22la%22:%22Fa%22%7D&jsoncallback="
    response = utils.request(method="GET", url=url)
    response = utils.convert_json(response)
    clean_data = []
    for item in response:
        data = {'symbol': item['sf'], 'company': item['cn'], 'isin': item['nc']}
        clean_data.append(data)
    return clean_data


def buy(isin, order_count, order_price):
    response = None
    try:
        url = "https://api3.mobinsb.ir/web/v1/Order/Post"

        payload = json.dumps({
            "orderCount": order_count,
            "orderPrice": order_price,
            "FinancialProviderId": 1,
            "isin": isin,
            "orderSide": 65,
            "orderValidity": 74,
            "orderValiditydate": "",
            "maxShow": 0,
            "orderId": 0
        })
        headers = {
            'Authorization': F'BasicAuthentication {utils.give_token()}',
            'Content-Type': 'application/json',
        }

        response = utils.request(method="POST", url=url, headers=headers, data=payload)
        if response.status_code == 200:
            response = utils.convert_json(response)
            logger.info(
                f"buy request sent successfully, {isin}, order price {order_price}, order count {order_count}: "
                f"{response}")
            return response['IsSuccessfull']
        else:
            logger.warning(
                f"buy request failed, {isin}, order price {order_price}, order count {order_count}: "
                f"status_code:{response.status_code}, response_content={response.text}")
            return False

    except Exception:
        logger.error(
            f"error during sell {isin}, order price {order_price}, order count {order_count}, response={response.text if response is not None else None}"
            f" {response.status_code if response is not None else None}",
            exc_info=True, )
        return False


def sell(isin, order_count, order_price):
    response = None
    try:
        url = "https://api3.mobinsb.ir/web/v1/Order/Post"

        payload = json.dumps({
            "orderCount": order_count,
            "orderPrice": order_price,
            "FinancialProviderId": 1,
            "isin": isin,
            "orderSide": 86,
            "orderValidity": 74,
            "orderValiditydate": "",
            "maxShow": 0,
            "orderId": 0
        })
        headers = {

            'Authorization': F'BasicAuthentication {utils.give_token()}',
            'Content-Type': 'application/json',
        }
        response = utils.request(method="POST", url=url, headers=headers, data=payload)
        if response.status_code == 200:  # 200 series mean ok!
            response = utils.convert_json(response)
            logger.info(
                f"sell request sent successfully, {isin}, order price {order_price}, order count {order_count}: {response}")
            return response['IsSuccessfull']
        else:
            logger.warning(
                f"sell request failed, {isin}, order price {order_price}, order count {order_count}: "
                f"status_code:{response.status_code}, response_content={response.text}")
            return False
    except Exception:
        logger.error(
            f"error during sell {isin}, order price {order_price}, order count {order_count}, response={response.text if response is not None else None}"
            f" {response.status_code if response is not None else None}",
            exc_info=True, )
        return False


def get_detail(isin):
    url = F"https://core.tadbirrlc.com//StockFutureInfoHandler.ashx?%7B%22Type%22:%22getLightSymbolInfoAndQueue%22,%22la%22:%22Fa%22,%22nscCode%22:%22{isin}%22%7D&jsoncallback="

    response = utils.request(method="GET", url=url)
    response = utils.convert_json(response)
    data = {'symbol': response['symbolinfo']['ect'], 'company': response['symbolinfo']['est'],
            'isin': response['symbolinfo']['nc'],
            'tomorrow-high': response['symbolinfo']['th'], 'tomorrow-low': response['symbolinfo']['tl']}

    return data


def open_order():
    url = "https://api3.mobinsb.ir/Web/V1/Order/GetOpenOrder/OpenOrder"

    headers = {

        'Authorization': F'BasicAuthentication {utils.give_token()}',
    }
    response = utils.request(method="GET", url=url, headers=headers)
    response = utils.convert_json(response)
    clean_data = []
    for item in response['Data']:
        data = {'symbol': item['symbol'], 'company': item['cn'], 'isin': item['nsccode'], 'time': item['time'],
                'date': item['dtime'], 'order price': item['orderprice'], 'orderid': item['orderid'],
                'orderside': item['orderside'],
                'qunatity': item['qunatity'], 'status': item['status']}
        clean_data.append(data)
    return clean_data
