<template>
<mk-ui>
	<span slot="header">%fa:cog%%i18n:@settings%</span>
	<main>
		<p v-html="'%i18n:!@signed-in-as%'.replace('{}', '<b>' + name + '</b>')"></p>
		<div>
			<x-profile/>

			<md-card>
				<md-card-header>
					<div class="md-title">%fa:palette% %i18n:@design%</div>
				</md-card-header>

				<md-card-content>
					<div>
						<md-switch v-model="darkmode">%i18n:@dark-mode%</md-switch>
					</div>

					<div>
						<md-switch v-model="clientSettings.circleIcons" @change="onChangeCircleIcons">%i18n:@circle-icons%</md-switch>
					</div>

					<div>
						<div class="md-body-2">%i18n:@timeline%</div>

						<div>
							<md-switch v-model="clientSettings.showReplyTarget" @change="onChangeShowReplyTarget">%i18n:@show-reply-target%</md-switch>
						</div>

						<div>
							<md-switch v-model="clientSettings.showMyRenotes" @change="onChangeShowMyRenotes">%i18n:@show-my-renotes%</md-switch>
						</div>

						<div>
							<md-switch v-model="clientSettings.showRenotedMyNotes" @change="onChangeShowRenotedMyNotes">%i18n:@show-renoted-my-notes%</md-switch>
						</div>
					</div>
				</md-card-content>
			</md-card>

			<md-card>
				<md-card-header>
					<div class="md-title">%fa:cog% %i18n:@behavior%</div>
				</md-card-header>

				<md-card-content>
					<div>
						<md-switch v-model="clientSettings.fetchOnScroll" @change="onChangeFetchOnScroll">%i18n:@fetch-on-scroll%</md-switch>
					</div>

					<div>
						<md-switch v-model="clientSettings.disableViaMobile" @change="onChangeDisableViaMobile">%i18n:@disable-via-mobile%</md-switch>
					</div>
				</md-card-content>
			</md-card>

			<md-card>
				<md-card-header>
					<div class="md-title">%fa:language% %i18n:@lang%</div>
				</md-card-header>

				<md-card-content>
					<md-field>
						<md-select v-model="lang" placeholder="%i18n:@auto%">
							<md-optgroup label="%i18n:@recommended%">
								<md-option value="">%i18n:@auto%</md-option>
							</md-optgroup>

							<md-optgroup label="%i18n:@specify-language%">
								<md-option value="ja">日本語</md-option>
								<md-option value="en">English</md-option>
								<md-option value="fr">Français</md-option>
								<md-option value="pl">Polski</md-option>
								<md-option value="de">Deutsch</md-option>
							</md-optgroup>
						</md-select>
					</md-field>
					<span class="md-helper-text">%fa:info-circle% %i18n:@lang-tip%</span>
				</md-card-content>
			</md-card>

			<md-card>
				<md-card-header>
					<div class="md-title">%fa:B twitter% %i18n:@twitter%</div>
				</md-card-header>

				<md-card-content>
					<p class="account" v-if="os.i.twitter"><a :href="`https://twitter.com/${os.i.twitter.screenName}`" target="_blank">@{{ os.i.twitter.screenName }}</a></p>
					<p>
						<a :href="`${apiUrl}/connect/twitter`" target="_blank">{{ os.i.twitter ? '%i18n:!@twitter-reconnect%' : '%i18n:!@twitter-connect%' }}</a>
						<span v-if="os.i.twitter"> or </span>
						<a :href="`${apiUrl}/disconnect/twitter`" target="_blank" v-if="os.i.twitter">%i18n:@twitter-disconnect%</a>
					</p>
				</md-card-content>
			</md-card>

			<md-card>
				<md-card-header>
					<div class="md-title">%fa:sync-alt% %i18n:@update%</div>
				</md-card-header>

				<md-card-content>
					<div>%i18n:@version% <i>{{ version }}</i></div>
					<template v-if="latestVersion !== undefined">
						<div>%i18n:@latest-version% <i>{{ latestVersion ? latestVersion : version }}</i></div>
					</template>
					<md-button class="md-raised md-primary" @click="checkForUpdate" :disabled="checkingForUpdate">
						<template v-if="checkingForUpdate">%i18n:@update-checking%<mk-ellipsis/></template>
						<template v-else>%i18n:@check-for-updates%</template>
					</md-button>
				</md-card-content>
			</md-card>
		</div>
		<p><small>ver {{ version }} ({{ codename }})</small></p>
	</main>
</mk-ui>
</template>

<script lang="ts">
import Vue from 'vue';
import { apiUrl, version, codename } from '../../../config';
import checkForUpdate from '../../../common/scripts/check-for-update';

import XProfile from './settings/settings.profile.vue';

export default Vue.extend({
	components: {
		XProfile
	},

	data() {
		return {
			apiUrl,
			version,
			codename,
			darkmode: localStorage.getItem('darkmode') == 'true',
			lang: localStorage.getItem('lang') || '',
			latestVersion: undefined,
			checkingForUpdate: false
		};
	},

	computed: {
		name(): string {
			return Vue.filter('userName')((this as any).os.i);
		}
	},

	watch: {
		darkmode() {
			(this as any)._updateDarkmode_(this.darkmode);
		},
		lang() {
			localStorage.setItem('lang', this.lang);
		}
	},

	mounted() {
		document.title = 'Misskey | %i18n:@settings%';
	},

	methods: {
		signout() {
			(this as any).os.signout();
		},

		onChangeFetchOnScroll(v) {
			this.$store.dispatch('settings/set', {
				key: 'fetchOnScroll',
				value: v
			});
		},

		onChangeDisableViaMobile(v) {
			this.$store.dispatch('settings/set', {
				key: 'disableViaMobile',
				value: v
			});
		},

		onChangeCircleIcons(v) {
			this.$store.dispatch('settings/set', {
				key: 'circleIcons',
				value: v
			});
		},

		onChangeShowReplyTarget(v) {
			this.$store.dispatch('settings/set', {
				key: 'showReplyTarget',
				value: v
			});
		},

		onChangeShowMyRenotes(v) {
			this.$store.dispatch('settings/set', {
				key: 'showMyRenotes',
				value: v
			});
		},

		onChangeShowRenotedMyNotes(v) {
			this.$store.dispatch('settings/set', {
				key: 'showRenotedMyNotes',
				value: v
			});
		},

		checkForUpdate() {
			this.checkingForUpdate = true;
			checkForUpdate((this as any).os, true, true).then(newer => {
				this.checkingForUpdate = false;
				this.latestVersion = newer;
				if (newer == null) {
					(this as any).apis.dialog({
						title: '%i18n:!@no-updates%',
						text: '%i18n:!@no-updates-desc%'
					});
				} else {
					(this as any).apis.dialog({
						title: '%i18n:!@update-available%',
						text: '%i18n:!@update-available-desc%'
					});
				}
			});
		}
	}
});
</script>

<style lang="stylus" scoped>
main
	padding 0 16px

	> div
		> *
			margin-bottom 16px

	> p
		display block
		margin 24px
		text-align center
		color #cad2da

	> ul
		$radius = 8px

		display block
		margin 16px auto
		padding 0
		max-width 500px
		width calc(100% - 32px)
		list-style none
		background #fff
		border solid 1px rgba(#000, 0.2)
		border-radius $radius

		> li
			display block
			border-bottom solid 1px #ddd

			&:hover
				background rgba(#000, 0.1)

			&:first-child
				border-top-left-radius $radius
				border-top-right-radius $radius

			&:last-child
				border-bottom-left-radius $radius
				border-bottom-right-radius $radius
				border-bottom none

			> a
				$height = 48px

				display block
				position relative
				padding 0 16px
				line-height $height
				color #4d635e

				> [data-fa]:nth-of-type(1)
					margin-right 4px

				> [data-fa]:nth-of-type(2)
					display block
					position absolute
					top 0
					right 8px
					z-index 1
					padding 0 20px
					font-size 1.2em
					line-height $height

</style>
