﻿CREATE PROCEDURE [BO_GetAliveKnoks]
	@KnokId BIGINT = NULL,
	@UserId BIGINT,
	@SortOrderAsc BIT = 0,
    @SortField INT = 1,
    @Published BIT = 0,
	@ExchangesIdsString NVARCHAR(1024) = NULL,
    @TradingPairsString NVARCHAR(1024) = NULL,
	@Gaining BIT = 0,
	@Losing BIT = 0,
	@RangeSettings [DurationCRTAggrItemsList] readonly
	AS     

	declare @ExchangesIds TABLE(id int NULL);
	declare @TradingPairs TABLE(code nvarchar(255) NULL);

	insert into @ExchangesIds(id) SELECT cast(value as int) 
		FROM STRING_SPLIT(@ExchangesIdsString, ';')

	insert into @TradingPairs(code) SELECT value 
		FROM STRING_SPLIT(@TradingPairsString, ';')

	--Get active knoks
	SELECT Duration, CreateDate, KnokId, Currency1, Currency2, Currency, MarketDisplayName, ExchangeId, UserId, 
		EntryPriceFrom, EntryPriceTo, ExitPriceFrom,  ExitPriceTo, StopLoss, ExchangeName,
		PotentialProfitValue, PotentialProfit, Price, DollarPrice, UserFollowers, [Rank],
	    HasAvatar, UserName, FirstName, LastName, CurrentPrice, Purchases, CurrentProfit, --DistanceFromTarget,
		CAST(CASE WHEN EXISTS(SELECT 1 FROM TraderFeedbacks fb where fb.KnokId = k.KnokId AND fb.UserId = @UserId AND fb.FeedbackType = 0) THEN 1 ELSE 0 END AS BIT) AS FeedbackExists,
		k.Precision
	
	FROM [dbo].[VW_ActiveKnoks] k with(nolock) 
	Where ISNULL(@KnokId, k.KnokId) = k.KnokId 
		AND (EXISTS (SELECT 1 FROM UserKnoks uk WHERE uk.UserId = @UserId AND uk.KnokId = k.KnokId))
		AND (EXISTS(SELECT 1 FROM @ExchangesIds HAVING COUNT(1) = 0) OR EXISTS (SELECT 1 FROM @ExchangesIds e WHERE e.id = k.ExchangeId))
		AND (EXISTS(SELECT 1 FROM @TradingPairs HAVING COUNT(1) = 0) OR EXISTS (SELECT 1 FROM @TradingPairs e WHERE e.code = k.Currency2))
		AND (ISNULL(@Published, 0) = 0 OR (@Published = 1 AND NOT k.PublishedDate IS NULL AND k.KnokStatusId = 3))
		AND ((@Gaining = 1 AND @Losing = 1) OR 
			(ISNULL(@Gaining, 0) = 0 OR (@Gaining = 1 AND CurrentPrice > ((EntryPriceFrom + EntryPriceTo) / 2)))
				AND (ISNULL(@Losing, 0) = 0 OR (@Losing = 1 AND CurrentPrice < ((EntryPriceFrom + EntryPriceTo) / 2)))
		)
	ORDER BY
		--CASE @SortOrderAsc WHEN 1 THEN
		--	CASE WHEN ISNULL(@SortField, 1) = 1 THEN
		--		CASE @SortField 
		--			WHEN NULL THEN FinishTime -- TimeLeft
		--			WHEN 1 THEN FinishTime -- TimeLeft
		--		END
		--	END
		--END ASC,

		--CASE @SortOrderAsc WHEN 0 THEN
		--	CASE WHEN ISNULL(@SortField, 1) = 1 THEN
		--		CASE @SortField 
		--			WHEN NULL THEN TimeLeft -- TimeLeft
		--			WHEN 1 THEN TimeLeft -- TimeLeft
		--		END
		--	END
		--END DESC,
		CASE @SortOrderAsc WHEN 1 THEN
			--CASE WHEN ISNULL(@SortField, 1) > 1 THEN
				CASE @SortField 
					WHEN 1 THEN TimeLeft -- TimeLeft
					WHEN 2 THEN DistanceFromTarget -- Distance from target
					WHEN 3 THEN CurrentProfit -- Current profit
					WHEN 4 THEN Purchases -- Purchases
					--WHEN 5 THEN PricePoolData -- PricePool
					--WHEN 6 THEN UserReview -- BuyerReview
				END
			--END
		END ASC,

		CASE @SortOrderAsc WHEN 0 THEN
			--CASE WHEN ISNULL(@SortField, 1) > 1 THEN
				CASE @SortField 
					WHEN 1 THEN TimeLeft -- TimeLeft
					WHEN 2 THEN DistanceFromTarget -- Distance from target
					WHEN 3 THEN CurrentProfit -- Current profit
					WHEN 4 THEN Purchases -- Purchases
					--WHEN 5 THEN PricePoolData -- PricePool
					--WHEN 6 THEN UserReview -- BuyerReview
				END
			--END
		END DESC

	EXEC [BO_GetKnokChartData] @KnokId, NULL, @RangeSettings

RETURN 0 