import type { ScreenData, TimeRange } from './screen';
import { ITEM_COLORS, WASTE_COLORS, buildScreenAxis, structureScreen } from './screen-structure';

function wave(len: number, base: number, amp: number, seed = 0) {
	return Array.from({ length: len }, (_, i) => {
		const v = base + amp * Math.sin((i + seed) * 0.85) + amp * 0.35 * Math.cos((i + seed) * 0.4);
		return Math.max(0, Math.round(v * 10) / 10);
	});
}

function intWave(len: number, base: number, amp: number, seed = 0) {
	return wave(len, base, amp, seed).map(v => Math.round(v));
}

function scaleByRange(range: TimeRange) {
	const map: Record<TimeRange, number> = {
		today: 0.08,
		week: 0.28,
		month: 1,
		quarter: 2.8,
		year: 1
	};
	return map[range] ?? 1;
}

function buildRank(name: string, org: string | undefined, people: number, weight: number, points: number) {
	return {
		name,
		org,
		weight: Math.round(weight * 10) / 10,
		people,
		rate: 0,
		points: Math.round(points),
		carbon: Math.round(weight * 10) / 10,
		size: people,
		weightPerCapita: Math.round((weight / people) * 100) / 100,
		pointPerCapita: Math.round((points / people) * 10) / 10
	};
}

/** 饱满演示数据，口径与后端结构一致 */
export function demoScreen(range: TimeRange = 'month'): ScreenData {
	const base = structureScreen(range);
	const axis = base.axis.length ? base.axis : buildScreenAxis(range);
	const s = scaleByRange(range);

	const periodWeightTon = Math.round(18.6 * s * 100) / 100;
	const recyclableTon = Math.round(7.9 * s * 100) / 100;
	const periodCarbonTon = Math.round(2.14 * s * 100) / 100;
	const periodValue = Math.round(12800 * s);
	const totalWeightTon = 126.8;
	const totalCarbonTon = 14.6;
	const totalParticipants = 3842;
	const totalPoints = 96500;

	const wasteWeights = [4200, 3100, 980, 420, 2860].map(v => Math.round(v * s));
	const wasteCounts = [860, 720, 96, 38, 540].map(v => Math.round(v * s));
	const wasteMix = base.wasteMix.map((item, i) => ({
		...item,
		weight: wasteWeights[i],
		count: wasteCounts[i],
		value: i === 0 ? periodValue : 0,
		color: WASTE_COLORS[item.code || ''] || item.color
	}));

	const recycleWeights = [1820, 960, 540, 680, 320, 410, 270].map(v => Math.round(v * s));
	const recycleValues = [4200, 2100, 1800, 960, 480, 3200, 180].map(v => Math.round(v * s));
	const recycleCarbons = recycleWeights.map(w => Math.round(w * 0.42 * 10) / 10);
	const recycleItems = base.recycleItems.map((item, i) => ({
		...item,
		weight: recycleWeights[i],
		value: recycleValues[i],
		carbon: recycleCarbons[i],
		color: ITEM_COLORS[i]
	}));

	const trendWeight = wave(axis.length, 620 * s, 180 * s, 1);
	const trendValue = intWave(axis.length, 420 * s, 120 * s, 2);
	const trendCarbon = wave(axis.length, 860 * s, 220 * s, 3);

	const yearCarbon = [0.8, 0.9, 1.1, 1.3, 1.5, 1.8, 2.0, 2.2, 1.9, 1.6, 1.2, 0.9];

	const deptRank = [
		buildRank('环境科学与工程学院', undefined, 486, 2860, 12800),
		buildRank('材料科学与工程学院', undefined, 412, 2410, 10600),
		buildRank('经济管理学院', undefined, 368, 1980, 9200),
		buildRank('计算机学院', undefined, 352, 1760, 8800),
		buildRank('外国语学院', undefined, 286, 1420, 6400),
		buildRank('土木工程学院', undefined, 248, 1180, 5200),
		buildRank('生命科学学院', undefined, 216, 960, 4600),
		buildRank('马克思主义学院', undefined, 168, 720, 3200)
	];

	const classRank = [
		buildRank('环科2201', '环境科学与工程学院', 68, 420, 1860),
		buildRank('材料2102', '材料科学与工程学院', 62, 386, 1720),
		buildRank('计科2303', '计算机学院', 58, 352, 1580),
		buildRank('经管2204', '经济管理学院', 54, 318, 1460),
		buildRank('土木2101', '土木工程学院', 48, 286, 1280),
		buildRank('外语2202', '外国语学院', 44, 248, 1120),
		buildRank('生科2301', '生命科学学院', 40, 216, 980),
		buildRank('环工2103', '环境科学与工程学院', 36, 198, 860)
	];

	const majorRank = [
		buildRank('环境工程', undefined, 286, 1680, 7600),
		buildRank('材料化学', undefined, 248, 1420, 6400),
		buildRank('软件工程', undefined, 236, 1280, 5800),
		buildRank('工商管理', undefined, 218, 1160, 5200),
		buildRank('英语', undefined, 186, 960, 4200),
		buildRank('土木工程', undefined, 168, 860, 3800),
		buildRank('生物技术', undefined, 142, 720, 3200),
		buildRank('环境科学', undefined, 128, 640, 2800)
	];

	const issued = Math.round(186000 * Math.max(s, 0.2));
	const transfer = Math.round(issued * 0.12);
	const donate = Math.round(issued * 0.08);
	const mall = Math.round(issued * 0.46);
	const balance = totalPoints;

	const orderAxis = base.ordersByType.axis;
	const ordersByType = {
		axis: orderAxis,
		现场回收: intWave(orderAxis.length, 18 * s, 8 * s, 4),
		预约上门: intWave(orderAxis.length, 12 * s, 5 * s, 5),
		大屏投递: intWave(orderAxis.length, 26 * s, 10 * s, 6),
		活动回收: intWave(orderAxis.length, 8 * s, 4 * s, 7)
	};

	return {
		...base,
		axis,
		kpis: {
			todayWeightKg: Math.round(286.5 * Math.max(s, 0.15) * 10) / 10,
			periodWeightTon,
			resourceRate: 42.3,
			periodValue,
			periodCarbonTon,
			totalWeightTon,
			totalParticipants,
			totalPoints,
			recyclableTon,
			bulkTon: Math.round(1.8 * s * 100) / 100,
			elecTon: Math.round(0.96 * s * 100) / 100,
			fabricTon: Math.round(1.24 * s * 100) / 100,
			yearCarbonTon: 18.6,
			monthCarbonTon: periodCarbonTon,
			totalCarbonTon,
			greenActions: Math.round(8620 * Math.max(s, 0.2)),
			newParticipants: Math.round(286 * s),
			participateRate: 31.6,
			pointsIssued: issued,
			pointsConsumed: Math.round(issued * 0.54),
			pointsBalance: balance,
			sorterOnline: 12,
			sorterTotal: 16,
			vehicleOnline: 5,
			vehicleTotal: 7,
			siteOnline: 18,
			siteTotal: 22,
			todayOrders: Math.round(46 * Math.max(s, 0.2)),
			todayAppointments: Math.round(28 * Math.max(s, 0.2)),
			pendingAppointments: Math.round(6 * Math.max(s, 0.2)),
			alerts: 2,
			healthAlerts: 1
		},
		wasteMix,
		recycleItems,
		trend: {
			weight: trendWeight,
			value: trendValue,
			carbon: trendCarbon
		},
		yearCarbon,
		deptRank,
		classRank,
		majorRank,
		pointFlow: { issued, transfer, donate, mall, balance },
		participateTrend: intWave(axis.length, 86 * s, 28 * s, 8),
		deliverTrend: intWave(axis.length, 168 * s, 42 * s, 9),
		ordersByType,
		appointmentFlow: [
			{ name: '预约', value: Math.round(18 * Math.max(s, 0.2)) },
			{ name: '接单', value: Math.round(14 * Math.max(s, 0.2)) },
			{ name: '完成', value: Math.round(11 * Math.max(s, 0.2)) }
		],
		sorterStatus: [
			{ name: '工作中', value: 12, color: '#22c55e' },
			{ name: '休息', value: 2, color: '#f59e0b' },
			{ name: '异常', value: 1, color: '#ef4444' },
			{ name: '离线', value: 1, color: '#94a3b8' }
		],
		vehicles: [
			{ no: '沪A·8K126', km: 86.4, idle: 12, overspeed: 0, status: '在线' },
			{ no: '沪A·6D882', km: 72.1, idle: 8, overspeed: 1, status: '在线' },
			{ no: '沪A·3F559', km: 64.8, idle: 18, overspeed: 0, status: '在线' },
			{ no: '沪A·2H903', km: 58.2, idle: 6, overspeed: 0, status: '在线' },
			{ no: '沪A·9M217', km: 46.5, idle: 22, overspeed: 2, status: '在线' },
			{ no: '沪A·5P668', km: 38.6, idle: 0, overspeed: 0, status: '离线' },
			{ no: '沪A·1R441', km: 24.3, idle: 0, overspeed: 0, status: '离线' }
		],
		sites: [
			{ name: '学生公寓一号站', weight: 286.5, online: true },
			{ name: '图书馆回收点', weight: 248.2, online: true },
			{ name: '第一食堂收集点', weight: 216.8, online: true },
			{ name: '体育馆回收站', weight: 186.4, online: true },
			{ name: '行政楼收集点', weight: 162.6, online: true },
			{ name: '实验楼B区', weight: 148.2, online: true },
			{ name: '研究生公寓站', weight: 126.8, online: false },
			{ name: '南门临时点', weight: 98.4, online: true }
		],
		unavailable: []
	};
}
