import {
	inviteCount,
	inviteList,
	inviteRead,
	inviteRemove,
	noticeCount,
	noticeList,
	noticeRead,
	noticeRemove
} from '@/api/news.js'
import website from '@/config/website.js'
const size = 20
const handler = {
	'0': {
		count: noticeCount,
		getList: noticeList,
		read: noticeRead,
		remove: noticeRemove
	},
	'1': {
		count: inviteCount,
		getList: inviteList,
		read: inviteRead,
		remove: inviteRemove
	}
}

function getCount(data) {
	let sum = 0
	for (let key in data) {
		sum += data[key]
	}
	return sum
}

let refreshTimer = null;

function startRefreshToken(dispatch) {
	refreshTimer = setInterval(() => {
		dispatch("newsInit");
	}, website.newsRefreshTime);
}

function stopRefreshToken() {
	clearInterval(refreshTimer)
}

const news = {
	state: {
		count: 0,
		data: {
			"0": {
				id: "0",
				title: "消息通知",
				icon: "../../static/img/notice.svg",
				page: {
					current: 0,
					size,
					total: size
				},
				data: [],
				prop: {
					title: 'title',
					desc: (value) => {
						if (value.data[0])
							return value.data[0][0]['desc']
					},
					time: 'createTime',
					isRead: 'isRead',
					listDesc: 'desc',
				},
				navigateTo(item) {
					uni.$once('getNewsDetail', (cb) => {
						cb(item)
					})
					uni.navigateTo({
						url: "/pages/news/newsDetail?type=0"
					})
				}
			},
			"1": {
				id: "1",
				title: "任务推送",
				icon: "../../static/img/renwu.png",
				page: {
					current: 0,
					size,
					total: size
				},
				data: [],
				prop: {
					title: 'missionTitle',
					desc: (value) => {
						if (value.data[0])
							return `您收到${value.data[0][0]['companyName']}的任务`
					},
					time: 'createTime',
					isRead: 'status',
					listDesc: 'companyName',
				},
				navigateTo(item, dispatch) {
					dispatch('readNew', {
						key: '1',
						id: item.id
					})
					uni.navigateTo({
						url: `/pages/projectInfo/projectInfo?type=1&missionNo=${encodeURIComponent(item.missionNo)}&id=${encodeURIComponent(item.id)}`
					})
				}
			},
			"2": {
				id: "1",
				title: "岗位推送",
				icon: "../../static/img/gangwei.svg",
				page: {
					current: 0,
					size,
					total: size
				},
				data: [],
				prop: {
					title: 'missionTitle',
					desc: (value) => {
						if (value.data[0])
							return `您收到${value.data[0][0]['companyName']}的任务`
					},
					time: 'createTime',
					isRead: 'status',
					listDesc: 'companyName',
				},
				navigateTo(item, dispatch) {
					dispatch('readNew', {
						key: '1',
						id: item.id
					})
					uni.navigateTo({
						url: `/pages/projectInfo/projectInfo?type=1&missionNo=${encodeURIComponent(item.missionNo)}&id=${encodeURIComponent(item.id)}`
					})
				}
			},
			"3": {
				id: "1",
				title: "技能推送",
				icon: "../../static/img/jineng.svg",
				page: {
					current: 0,
					size,
					total: size
				},
				data: [],
				prop: {
					title: 'missionTitle',
					desc: (value) => {
						if (value.data[0])
							return `您收到${value.data[0][0]['companyName']}的任务`
					},
					time: 'createTime',
					isRead: 'status',
					listDesc: 'companyName',
				},
				navigateTo(item, dispatch) {
					dispatch('readNew', {
						key: '1',
						id: item.id
					})
					uni.navigateTo({
						url: `/pages/projectInfo/projectInfo?type=1&missionNo=${encodeURIComponent(item.missionNo)}&id=${encodeURIComponent(item.id)}`
					})
				}
			},
			"4": {
				id: "1",
				title: "政策推送",
				icon: "../../static/img/zhengce.svg",
				page: {
					current: 0,
					size,
					total: size
				},
				data: [],
				prop: {
					title: 'missionTitle',
					desc: (value) => {
						if (value.data[0])
							return `您收到${value.data[0][0]['companyName']}的任务`
					},
					time: 'createTime',
					isRead: 'status',
					listDesc: 'companyName',
				},
				navigateTo(item, dispatch) {
					dispatch('readNew', {
						key: '1',
						id: item.id
					})
					uni.navigateTo({
						url: `/pages/projectInfo/projectInfo?type=1&missionNo=${encodeURIComponent(item.missionNo)}&id=${encodeURIComponent(item.id)}`
					})
				}
			}
		},
		unread: {
			"0": 0,
			"1": 0
		},
		allUnread: 0,
		dic: {}
	},
	actions: {
		newsInit({
			dispatch
		}) {
			const ps = []
			ps.push(dispatch('getUnreadNum'))

			for (let key in handler) {
				ps.push(dispatch('newsReload', key))
			}
			return Promise.all(ps)
		},
		newsReload({
			dispatch,
			commit
		}, id) {
			commit('CLEAR_NEWS_DATA', id)
			return dispatch('newsGetNextPage', id)
		},
		getUnreadNum({
			commit
		}) {
			for (let key in handler) {
				handler[key].count().then((result) => {
					commit('SET_UNREADNUM', {
						id: key,
						count: result
					})
				})
			}
		},
		newsGetNextPage({
			state,
			commit
		}, id) {
			const item = state.data[id]
			const page = item.page
			const current = page.current + 1
			return new Promise((resolve, reject) => {
				if (parseInt(page.total / page.size) + 1 < current) {
					resolve()
					return
				}
				handler[id].getList(current, page.size).then(res => {
					const data = res.data.data;
					commit('ADD_NEWS_DATA', {
						id,
						data: data.records,
						current,
						total: data.total
					})
					resolve(res)
				}).catch(reject)
			})
		},
		removeNew({
			commit
		}, {
			id,
			key,
			group,
			index
		}) {
			return new Promise((resolve, reject) => {
				commit('REMOVE_NEWS', {
					id,
					key,
					group,
					index
				})
				handler[key].remove(id).then(resolve, reject)
			})
		},
		readNew({
			commit
		}, {
			key,
			id
		}) {
			return new Promise((resolve, reject) => {
				commit('READ_NEWS', {
					key,
					id
				})
				handler[key].read(id).then(resolve, reject)
			})
		},
		endRefreshNewsTimer(){
			stopRefreshToken()
		},
		startRefreshNewsTimer({
			state,
			commit,
			dispatch
		}) {
			dispatch('newsInit').then(() => {
				startRefreshToken(dispatch);
			})
		}
	},
	mutations: {
		SET_UNREADNUM: (state, {
			id,
			count
		}) => {
			state.unread[id] = count
			state.allUnread = getCount(state.unread)
		},
		CLEAR_NEWS_DATA: (state, id) => {
			state.data[id].page = {
				current: 0,
				size
			}
			state.data[id].data = []
		},
		ADD_NEWS_DATA: (state, {
			id,
			data,
			current,
			total
		}) => {
			data.forEach(item => {
				state.dic[item.id] = item
			})
			state.data[id].page.total = total
			state.data[id].page.current = current
			if (data.length > 0) {
				state.data[id].data.push(data)
			}
		},
		REMOVE_NEWS: (state, {
			id,
			key,
			group,
			index
		}) => {
			const readProp = state.data[key].prop['isRead']
			if (state.dic[id][readProp] === 0) {
				state.unread[key]--
				state.allUnread--
			}
			state.dic[id] = undefined
			const data = state.data[key].data
			data[group].splice(index, 1)
			if (data[group].length === 0) {
				data.splice(group, 1)
			}
		},
		READ_NEWS: (state, {
			key,
			id
		}) => {
			const readProp = state.data[key].prop['isRead']
			if (state.dic[id][readProp] === 0) {
				state.dic[id][readProp] = 1
				state.unread[key]--
				state.allUnread--
			}
		}
	},
	getters: {

	}
};
export default news;
