from typing import Dict
from typing import List


def parse_result(transaction_json: Dict) -> List:
	"""Parsed the required information from the response of Fundrich

	Args:
		transaction_json (Dict): The orignial response of Fundrich

	Returns:
		List: List of fund informaiton, each element is a dictionary, 
		containing the following fileds:
			+ name: The name of the fund
			+ bal_cost: Total principle (本金總額)
			+ amt: Total profit and loss (損益總額)
			+ rate: Rate of return (獲益率)

	"""	
	assert type(transaction_json) is dict
	result = list()
	res_detail = transaction_json["Data"]["TRADE_LISTS"]
	for fund in res_detail:
		fund_name = fund["FUND_SH_NM"] # 基金名稱
		etd_bal_cost = fund["ETD_BAL_COST"] # 單筆申購本金
		rsp_etd_bal_cost = fund["RSP_ETD_BAL_COST"] # 定期定額本金
		gl_amt = fund["GL_AMT"] # 單筆申購參考損益
		rsp_gl_amt = fund["RSP_GL_AMT"] # 定期定額參考損益

		bal_cost = etd_bal_cost+rsp_etd_bal_cost # 總本金
		amt = gl_amt + rsp_gl_amt # 總損益
		rate = amt / bal_cost

		result.append({"name": fund_name, "bal_cost": bal_cost, "amt": amt, "rate":round(rate*100, 2)})

	return result