<mk-home-page>
	<mk-ui ref="ui" page={ page }>
		<mk-home ref="home" mode={ parent.opts.mode }></mk-home>
	</mk-ui>
	<style>
		:scope
			display block
	</style>
	<script>
		import Progress from '../../../common/scripts/loading';
		import getPostSummary from '../../../common/scripts/get-post-summary';

		this.mixin('i');
		this.mixin('api');
		this.mixin('stream');

		const stream = this.stream.event;

		this.unreadCount = 0;

		this.page = this.opts.mode || 'timeline';

		this.on('mount', () => {
			this.refs.ui.refs.home.on('loaded', () => {
				Progress.done();
			});
			document.title = 'Misskey';
			Progress.start();
			stream.on('post', this.onStreamPost);
			document.addEventListener('visibilitychange', this.windowOnVisibilitychange, false);
		});

		this.on('unmount', () => {
			stream.off('post', this.onStreamPost);
			document.removeEventListener('visibilitychange', this.windowOnVisibilitychange);
		});

		this.onStreamPost = post => {
			if (document.hidden && post.user_id != this.I.id) {
				this.unreadCount++;
				document.title = `(${this.unreadCount}) ${getPostSummary(post)}`;
			}
		};

		this.windowOnVisibilitychange = () => {
			if (!document.hidden) {
				this.unreadCount = 0;
				document.title = 'Misskey';
			}
		};
	</script>
</mk-home-page>
