<template>
	<div class="content">
		<h1>Import your data from Wunderlist to Vikunja</h1>
		<p>Vikunja will import all folders, lists, tasks, notes, reminders and files you have access to.</p>
		<template v-if="isMigrating === false && message === '' && lastMigrationDate === 0">
			<p>To authorize Vikunja to access your Wunderlist Account, click the button below.</p>
			<a :href="authUrl" class="button is-primary" :class="{'is-loading': migrationService.loading}" :disabled="migrationService.loading">Get Started</a>
		</template>
		<div class="migration-in-progress-container" v-else-if="isMigrating === true && message === '' && lastMigrationDate === 0">
			<div class="migration-in-progress">
				<img src="/images/migration/wunderlist.png" alt="Wunderlist Logo"/>
				<div class="progress-dots">
					<span></span>
					<span></span>
					<span></span>
					<span></span>
					<span></span>
					<span></span>
					<span></span>
					<span></span>
				</div>
				<img src="/images/logo.svg" alt="Vikunja Logo">
			</div>
			<p>Importing in progress, hang tight...</p>
		</div>
		<div v-else-if="lastMigrationDate > 0">
			<p>
				It looks like you've already imported your stuff from wunderlist at {{ new Date(lastMigrationDate * 1000) }}.<br/>
				Importing again is possible, but might create duplicates.
				Are you sure?
			</p>
			<div class="buttons">
				<button class="button is-primary" @click="migrate">I am sure, please start migrating now!</button>
				<router-link :to="{name: 'home'}" class="button is-danger is-outlined">Cancel</router-link>
			</div>
		</div>
		<div v-else>
			<div class="message is-primary">
				<div class="message-body">
					{{ message }}
				</div>
			</div>
			<router-link :to="{name: 'home'}" class="button is-primary">Refresh</router-link>
		</div>
	</div>
</template>

<script>
	import WunderlistMigrationService from '../../services/migrator/wunderlist'

	export default {
		name: 'wunderlist',
		data() {
			return {
				migrationService: WunderlistMigrationService,
				authUrl: '',
				isMigrating: false,
				lastMigrationDate: 0,
				message: '',
				wunderlistCode: '',
			}
		},
		created() {
			this.migrationService = new WunderlistMigrationService()
			this.getAuthUrl()
			this.message = ''

			if(typeof this.$route.query.code !== 'undefined') {
				this.wunderlistCode = this.$route.query.code
				this.migrationService.getStatus()
					.then(r => {
						if(r.time_unix > 0) {
							this.lastMigrationDate = r.time_unix
							return
						}
						this.migrate()
					})
					.catch(e => {
						this.error(e, this)
					})
			}
		},
		methods: {
			getAuthUrl() {
				this.migrationService.getAuthUrl()
					.then(r => {
						this.authUrl = r.url
					})
					.catch(e => {
						this.error(e, this)
					})
			},
			migrate() {
				this.isMigrating = true
				this.lastMigrationDate = 0
				this.migrationService.migrate({code: this.wunderlistCode})
					.then(r => {
						this.message = r.message
					})
					.catch(e => {
						this.error(e, this)
					})
					.finally(() => {
						this.isMigrating = false
					})
			},
		},
	}
</script>
