import {
	IActivDirectSearchAssetParams,
	ILitAction,
	CONTRACT_INTERFACES,
} from '../../..'

const testPricing = async (
	params: IActivDirectSearchAssetParams,
): Promise<CONTRACT_INTERFACES.IPrice> => {
	const headers = {
		// 'Content-Type': 'application/default+json; charset=utf-8',
		'Content-Type': 'application/json',
		'Access-Control-Allow-Origin': '*',
		'User-Agent': 'PostmanRuntime/7.28.0',
		Accept: '*/*',
		Connection: 'keep-alive',
		Authorization: 'Bearer ' + params.key,
	}

	const requestRecipe = {
		url: 'https://api.tradestation.com/v2/data/quote/' + params.symbol,
		method: 'GET',
		headers,
	}
	const data = (
		await fetch(
			requestRecipe.url,
			requestRecipe as unknown as RequestInit,
		).then((res) => res.json())
	)[0]

	// must be alphabetical order
	const ask = parseFloat(data.Ask)
	const bid = parseFloat(data.Bid)
	let globalPrice = (ask + bid) / 2
	const askAfterDot = ask.toString().split('.')
	let askDecimals = 0
	if (askAfterDot.length > 1) {
		askDecimals = askAfterDot[1].length
	}
	const bidAfterDot = bid.toString().split('.')
	let bidDecimals = 0
	if (bidAfterDot.length > 1) {
		bidDecimals = bidAfterDot[1].length
	}
	const decimals = Math.max(askDecimals, bidDecimals)
	globalPrice = parseFloat(globalPrice.toFixed(decimals))
	const timestamp = Date.now()
	const priceObj: CONTRACT_INTERFACES.IPrice = {
		ask,
		bid,
		globalPrice,
		provider: 'Tradestation',
		symbol: params.symbol,
		timestamp,
	}

	return priceObj
}

/*
 Final action:
 Network: Lit Chronicle
 Onwer Account Wallet: FIRED! => Mint&Grant&Burned! => SECURED!
 PKP id: 109830394400034117116299334017301959423202407461557955731773823241132924933532
 PKP public key: 0x046364368a9b43be0ec655073608362799bb5fe84677f0dd70c55446a442fc15419113b0cdeabbfbc9170e2e776eebd0d4fb782a26bb76d7267476305fcfc5d520
 PKP ETH: 0x574e11FF32cb17fC8bD14B9C91a699e44a57525C
 Action ipfsId: QmcPQ7fbWKFnH9LK9eHMnghLViskH8eBngXH2i4Lg6Wu1W
 granted?: true
 */
export const GET_PRICE_TRADESTATION_V2: ILitAction = {
	ipfsId: 'QmcPQ7fbWKFnH9LK9eHMnghLViskH8eBngXH2i4Lg6Wu1W',
	publicKey:
		'0x046364368a9b43be0ec655073608362799bb5fe84677f0dd70c55446a442fc15419113b0cdeabbfbc9170e2e776eebd0d4fb782a26bb76d7267476305fcfc5d520',
	keyEthAddress: '0x574e11FF32cb17fC8bD14B9C91a699e44a57525C',
	testPricing,
}
