export type ChannelProduct = {
	id: string
	name: string
	shortName: string
	image: string
	price: number
	marketPrice: number
	tag: string
}

export type MarketingChannel = {
	id: string
	title: string
	subtitle: string
	badge: string
	themeColor: string
	bgColor: string
	routeType: string
	layoutType: string
	products: ChannelProduct[]
	moreProducts: ChannelProduct[]
}

export type SimpleCategoryChannel = {
	id: string
	title: string
	subtitle: string
	routeType: string
	icon: string
	coverImages: string[]
	categoryId: string
}

const DEFAULT_CHANNEL_IMAGE = '/static/images/default.png'

function createChannelProduct(
	id: string,
	name: string,
	shortName: string,
	price: number,
	marketPrice: number,
	tag: string
): ChannelProduct {
	return {
		id,
		name,
		shortName,
		image: DEFAULT_CHANNEL_IMAGE,
		price,
		marketPrice,
		tag
	}
}

function cloneProducts(products: ChannelProduct[]): ChannelProduct[] {
	const result: ChannelProduct[] = []
	for (let i = 0; i < products.length; i++) {
		const item = products[i]
		result.push({
			id: item.id,
			name: item.name,
			shortName: item.shortName,
			image: item.image,
			price: item.price,
			marketPrice: item.marketPrice,
			tag: item.tag
		})
	}
	return result
}

function createMarketingChannel(
	id: string,
	title: string,
	subtitle: string,
	badge: string,
	themeColor: string,
	bgColor: string,
	routeType: string,
	layoutType: string,
	products: ChannelProduct[],
	moreProducts: ChannelProduct[]
): MarketingChannel {
	return {
		id,
		title,
		subtitle,
		badge,
		themeColor,
		bgColor,
		routeType,
		layoutType,
		products: cloneProducts(products),
		moreProducts: cloneProducts(moreProducts)
	}
}

function buildRecommendMarketingChannels(): MarketingChannel[] {
	const subsidySeed: ChannelProduct[] = [
		createChannelProduct('subsidy-1', '智能学习平板', '学习平板', 295, 499, '补贴价'),
		createChannelProduct('subsidy-2', '遥控赛车玩具', '遥控车', 174.3, 299, '补贴价')
	]
	const subsidyMore: ChannelProduct[] = [
		createChannelProduct('subsidy-3', '家用护眼台灯', '护眼台灯', 129, 199, '直降'),
		createChannelProduct('subsidy-4', '儿童积木礼盒', '积木礼盒', 89, 139, '补贴价'),
		createChannelProduct('subsidy-5', '便携筋膜枪', '筋膜枪', 239, 399, '官方补贴'),
		createChannelProduct('subsidy-6', '智能空气炸锅', '空气炸锅', 269, 459, '限时补贴')
	]

	const qualitySeed: ChannelProduct[] = [
		createChannelProduct('quality-1', '超市配送日用品', '超市配送', 19.9, 29.9, '实惠'),
		createChannelProduct('quality-2', '美食团购套餐', '美食团购', 39.9, 59.9, '团购')
	]
	const qualityMore: ChannelProduct[] = [
		createChannelProduct('quality-3', '健康轻食组合', '轻食组合', 28.8, 39.8, '精选'),
		createChannelProduct('quality-4', '家居香氛礼盒', '香氛礼盒', 59, 89, '品质价'),
		createChannelProduct('quality-5', '鲜果早餐套餐', '鲜果套餐', 26.8, 36.8, '人气'),
		createChannelProduct('quality-6', '净味洗护套装', '洗护套装', 49.9, 69.9, '好评')
	]

	const cheapSeed: ChannelProduct[] = [
		createChannelProduct('cheap-1', '夏季西瓜鲜果', '新鲜西瓜', 9.8, 19.9, '9.9包邮'),
		createChannelProduct('cheap-2', '厨房小工具套装', '厨房工具', 10.8, 18.8, '特价')
	]
	const cheapMore: ChannelProduct[] = [
		createChannelProduct('cheap-3', '旅行压缩毛巾', '压缩毛巾', 9.9, 15.9, '包邮'),
		createChannelProduct('cheap-4', '桌面收纳盒', '收纳盒', 9.9, 16.8, '限量'),
		createChannelProduct('cheap-5', '抹布海绵组合', '清洁抹布', 8.8, 12.8, '超省'),
		createChannelProduct('cheap-6', '便携数据线', '数据线', 9.9, 14.9, '补贴')
	]

	const liveSeed: ChannelProduct[] = [
		createChannelProduct('live-1', '智能风扇套装', '智能风扇', 1099, 1399, '直播价'),
		createChannelProduct('live-2', '智能门锁套装', '智能门锁', 2699, 3299, '直播价')
	]
	const liveMore: ChannelProduct[] = [
		createChannelProduct('live-3', '无线吸尘器', '无线吸尘器', 699, 899, '直播间'),
		createChannelProduct('live-4', '按摩椅轻享版', '按摩椅', 1999, 2599, '限时抢'),
		createChannelProduct('live-5', '智能净水器', '净水器', 1599, 1999, '主播推荐'),
		createChannelProduct('live-6', '轻薄投影仪', '投影仪', 1399, 1799, '专享价')
	]

	return [
		createMarketingChannel('subsidy', '百亿补贴', '大牌直降补贴价', '国家补贴', '#e02e24', '#fff6f4', 'subsidy', 'recommend-large', subsidySeed, subsidyMore),
		createMarketingChannel('quality-life', '品质生活', '本地好物精选', '品质严选', '#16a34a', '#f3fff7', 'quality', 'recommend-large', qualitySeed, qualityMore),
		createMarketingChannel('cheap-mail', '9.9包邮', '低价好物随心买', '低价包邮', '#ff4d00', '#fff8ed', 'cheap', 'recommend-large', cheapSeed, cheapMore),
		createMarketingChannel('live-low-price', '直播低价', '直播间好价精选', '直播好价', '#e11d48', '#fff1f5', 'live', 'recommend-large', liveSeed, liveMore)
	]
}

function buildCategoryRankChannel(categoryId: string): MarketingChannel {
	const seedProducts: ChannelProduct[] = [
		createChannelProduct(categoryId + '-rank-1', '热销榜单精选好物', '榜单好物', 69.9, 99.9, '热销'),
		createChannelProduct(categoryId + '-rank-2', '高回购口碑单品', '口碑单品', 49.9, 79.9, '回购')
	]
	const moreProducts: ChannelProduct[] = [
		createChannelProduct(categoryId + '-rank-3', '每日热度上升款', '热度上升', 59.9, 89.9, '榜单'),
		createChannelProduct(categoryId + '-rank-4', '用户收藏热门款', '收藏热门', 45.9, 69.9, '精选'),
		createChannelProduct(categoryId + '-rank-5', '爆款囤货套装', '囤货套装', 79.9, 109.9, '热卖'),
		createChannelProduct(categoryId + '-rank-6', '销量冠军单品', '销量冠军', 99.9, 139.9, '冠军')
	]
	return createMarketingChannel('rank', '排行榜', '每日更新', '热销榜单', '#e02e24', '#fff6f4', 'rank', 'detail-list', seedProducts, moreProducts)
}

function buildCategoryQualityChannel(categoryId: string): MarketingChannel {
	const seedProducts: ChannelProduct[] = [
		createChannelProduct(categoryId + '-quality-1', '品质优选套装组合', '精选套装', 88, 129, '严选'),
		createChannelProduct(categoryId + '-quality-2', '高分口碑精品单品', '高分精品', 56, 89, '口碑')
	]
	const moreProducts: ChannelProduct[] = [
		createChannelProduct(categoryId + '-quality-3', '质价比家用好物', '家用好物', 66, 99, '品质'),
		createChannelProduct(categoryId + '-quality-4', '长期复购推荐款', '复购推荐', 45, 69, '精选'),
		createChannelProduct(categoryId + '-quality-5', '舒适实用组合装', '组合装', 105, 149, '推荐'),
		createChannelProduct(categoryId + '-quality-6', '高满意度口碑款', '高满意度', 39.9, 59.9, '好评')
	]
	return createMarketingChannel('quality', '品质优选', '精选好货', '品质严选', '#16a34a', '#f3fff7', 'quality', 'detail-list', seedProducts, moreProducts)
}

export function getRecommendMarketingChannels(): MarketingChannel[] {
	return buildRecommendMarketingChannels()
}

export function getChannelDetailData(channelId: string, routeType: string, categoryId: string): MarketingChannel | null {
	if (categoryId === 'recommend') {
		const channels = buildRecommendMarketingChannels()
		for (let i = 0; i < channels.length; i++) {
			const channel = channels[i]
			if (channel.id === channelId || channel.routeType === routeType) {
				return channel
			}
		}
		if (channels.length > 0) {
			return channels[0]
		}
		return null
	}

	if (routeType === 'rank') {
		return buildCategoryRankChannel(categoryId)
	}
	if (routeType === 'quality') {
		return buildCategoryQualityChannel(categoryId)
	}
	return buildCategoryRankChannel(categoryId)
}
