#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import unicorn_binance_websocket_api

# create instance of BinanceWebSocketApiManager
binance_websocket_api_manager = unicorn_binance_websocket_api.BinanceWebSocketApiManager(
)

# get version of the used UNICORN Binance WebSocket API
if binance_websocket_api_manager.is_update_availabe():
    print(
        "Please upgrade to " +
        binance_websocket_api_manager.get_latest_version() + ", you are on",
        binance_websocket_api_manager.get_version())

    latest_release_info = binance_websocket_api_manager.get_latest_release_info(
    )
    if latest_release_info:
        print(
            "Please download the latest release or run `pip install unicorn-binance-websocket-api --upgrade`: "
        )
        print("\ttar: " + latest_release_info["tarball_url"])
    while True:
        if binance_websocket_api_manager.is_manager_stopping():
            exit(0)
        oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
        if oldest_stream_data_from_stream_buffer is not False:
            pass
        else:
            time.sleep(0.01)

try:
    ubra = unicorn_binance_rest_api.BinanceRestApiManager(binance_api_key, binance_api_secret)
except requests.exceptions.ConnectionError:
    print("No internet connection?")
    sys.exit(1)

ubwa = unicorn_binance_websocket_api.BinanceWebSocketApiManager(high_performance=True, debug=True)

# start a worker process to move the received stream_data from the stream_buffer to a print function
worker_thread = threading.Thread(target=print_stream_data_from_stream_buffer, args=(ubwa,))
worker_thread.start()

markets = []
data = ubra.get_all_tickers()
for item in data:
    markets.append(item['symbol'])

private_stream_id_alice = ubwa.create_stream(["!userData"],
                                             ["arr"],
                                             api_key=binance_api_key,
                                             api_secret=binance_api_secret,
                                             stream_label="userData Alice")