//지표 관리 - 유저 지표, 경제 지표 api

import { Axios } from '../utils';

// 1. 유저 지표
// 2. 경제 지표

// 1. 유저 지표

// 이용자 지표 조회
export const userIndexView = async (token, sendDate, endDate) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/user/list?start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data.user_statistics_list;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('MailView Error', e);
		}
	}
};

// 이용자 지표 총계
export const userTotalIndex = async token => {
	try {
		const res = await Axios.get(`/api/v1/indicators/user/total`, {
			headers: { Authorization: `Bearer ${token}` },
		});
		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('userTotalIndex', e);
		}
	}
};

// 유저 지표 다운로드
export const userIndexExport = async (token, filename, sendDate, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/user/excel-down?file=${filename}&start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('userIndexExport Error', e);
		}
	}
};

// Retention
export const RetentionIndexView = async (token, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/retention/list?start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('RetentionIndexView Error', e);
		}
	}
};

// Retention 다운로드
export const RetentionIndexExport = async (token, filename, sendDate, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/retention/excel-down?${filename}&start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('RetentionIndexExport Error', e);
		}
	}
};

// Segment
export const SegmentIndexView = async (token, search_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/segment/list?search_dt=${search_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('SegmentIndexView Error', e);
		}
	}
};

// Segment 다운로드
export const SegmentIndexExport = async (token, filename, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/segment/excel-down?${filename}&search_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('SegmentIndexExport Error', e);
		}
	}
};

// Playtime
export const PlaytimeIndexView = async (token, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/playtime/list?start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('PlaytimeIndexView Error', e);
		}
	}
};

// Playtime 다운로드
export const PlaytimeIndexExport = async (token, filename, sendDate, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/playtime/excel-down?file=${filename}&start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('PlaytimeIndexExport Error', e);
		}
	}
};

// 2. 경제 지표

// 재화 조회 (currency)
export const CurrencyIndexView = async (token, start_dt, end_dt, currency_type) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/currency/use?start_dt=${start_dt}&end_dt=${end_dt}&currency_type=${currency_type}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('currencyIndexView Error', e);
		}
	}
};

// 재화 지표 다운로드
export const CurrencyIndexExport = async (token, filename, sendDate, endDate, currencyType) => {
	try {
		await Axios.get(`/api/v1/indicators/currency/excel-down?file=${filename}&start_dt=${sendDate}&end_dt=${endDate}&currency_type=${currencyType}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('CurrencyIndexExport Error', e);
		}
	}
};

// VBP
export const VbpIndexView = async (token, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/currency/vbp?start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('VbpIndexView Error', e);
		}
	}
};

// VBP 다운로드
export const VBPIndexExport = async (token, filename, sendDate, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/currency/excel-down?file=${filename}&start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('VBPIndexExport Error', e);
		}
	}
};

// Item
export const ItemIndexView = async (token, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/currency/item?start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('ItemIndexView Error', e);
		}
	}
};

// Item 다운로드
export const ItemIndexExport = async (token, filename, sendDate, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/currency/excel-down?file=${filename}&start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('ItemIndexExport Error', e);
		}
	}
};

// Instance
export const InstanceIndexView = async (token, data, start_dt, end_dt) => {
	try {
		const res = await Axios.get(
			`/api/v1/indicators/currency/instance?search_key=${data ? data : ''}
		&start_dt=${start_dt}&end_dt=${end_dt}`,
			{
				headers: { Authorization: `Bearer ${token}` },
			},
		);

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('InstanceIndexView Error', e);
		}
	}
};

// Instance 다운로드
export const InstanceIndexExport = async (token, filename, data, sendDate, endDate) => {
	try {
		await Axios.get(
			`/api/v1/indicators/currency/excel-down?file=${filename}&search_key=${data ? data : ''}
		&start_dt=${sendDate}&end_dt=${endDate}`,
			{
				headers: { Authorization: `Bearer ${token}` },
				responseType: 'blob',
			},
		).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('InstanceIndexExport Error', e);
		}
	}
};

// Clothes
export const ClothesIndexView = async (token, data, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/currency/clothes?search_key=${data ? data : ''}&start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('ClothesIndexView Error', e);
		}
	}
};

// Clothes 다운로드
export const ClothesIndexExport = async (token, filename, data, sendDate, endDate) => {
	try {
		await Axios.get(
			`/api/v1/indicators/currency/excel-down?file=${filename}&search_key=${data ? data : ''}
		&start_dt=${sendDate}&end_dt=${endDate}`,
			{
				headers: { Authorization: `Bearer ${token}` },
				responseType: 'blob',
			},
		).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('ClothesIndexExport Error', e);
		}
	}
};


// DAU
export const DailyActiveUserView = async (token, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/dau/list?start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});
		
		return res.data.data.dau_list;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('DailyActiveUserView Error', e);
		}
	}
};



// DAU 다운로드
export const DailyActiveUserExport = async (token, filename, sendDate, endDate) => {
	try {
		await Axios.get(`/api/v1/indicators/dau/excel-down?file=${filename}&start_dt=${sendDate}&end_dt=${endDate}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('PlaytimeIndexExport Error', e);
		}
	}
};



// Daily Medal
export const DailyMedalView = async (token, start_dt, end_dt) => {
	try {
		const res = await Axios.get(`/api/v1/indicators/daily-medal/list?start_dt=${start_dt}&end_dt=${end_dt}`, {
			headers: { Authorization: `Bearer ${token}` },
		});
		
		return res.data.data.daily_medal_list;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('DailyMedalView Error', e);
		}
	}
};