<template>
	<div class="accordion mb-3 shadow">
		<div class="accordion-item">
			<h2 class="accordion-header"><button class="accordion-button" :class="{ testing: (x_server) && (x_server.production) && (x_server.production != '1') }" type="button" @click="() => show = !show"><template v-if="user">{{user[2]}}<template v-if="server"> @ {{server.domain}}</template></template><template v-else>Login</template></button></h2>
			<div class="accordion-collapse collapse" :class="{ show }">
				<div class="accordion-body">
					<div class="card">
						<div class="card-body">
							<p class="card-text row"><code class="col" v-if="banner"><pre>{{banner.join('\n')}}</pre></code><code class="col" v-if="user"><pre>{{user.join('\n')}}</pre></code></p>
						</div>
						<button class="btn btn-danger" style="width: 100%;" type="button" v-if="user" v-on:click="logout">Logout</button>
						<div class="input-group flex-nowrap" v-if="!user">
							<span class="input-group-text">🔑</span>
							<select class="form-control" v-model="host">
								<option value="vista.bronx.med.va.gov:19201">Bronx BRX</option>
								<option value="test.bronx.med.va.gov:19001">Bronx BRX-TEST</option>
								<option value="vista.east-orange.med.va.gov:19203">East Orange NJH</option>
								<option value="test.east-orange.med.va.gov:19003">East Orange NJH-TEST</option>
								<option value="vista.hudson-valley.med.va.gov:19205">Hudson Valley NVH</option>
								<option value="test.hudson-valley.med.va.gov:19005">Hudson Valley NVH-TEST</option>
								<option value="vista.brooklyn.med.va.gov:19208">NY Harbor NYH</option>
								<option value="test.brooklyn.med.va.gov:19008">NY Harbor NYH-TEST</option>
								<option value="vista.northport.med.va.gov:19209">Northport NOP</option>
								<option value="test.northport.med.va.gov:19009">Northport NOP-TEST</option>
								<option value="vista.v02.med.va.gov:19224">VISN 2 V02</option>
								<option value="test.v02.med.va.gov:19024">VISN 2 V02-TEST</option>
							</select>
							<input type="password" class="form-control" placeholder="Access Code" v-model="accesscode" />
							<input type="password" class="form-control" placeholder="Verify Code" v-model="verifycode" />
							<button class="btn btn-primary" type="button" v-on:click="login">Login<template v-if="!(accesscode || verifycode)"> (omit AV codes for SAML)</template></button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<style scoped>
	.accordion-button.testing:not(.collapsed) {
		color: #e40c63;
		background-color: #ffe7f1;
	}
</style>

<script>
	import vistax from './vistax.mjs';

	export default {
		props: {
			secret: String,
			client: Object,
			server: {
				type: Object,
				default: null
			},
			user: {
				type: Array,
				default: null
			}
		},
		emits: {
			'update:client': Object,
			'update:server': {
				type: Object,
				default: null
			},
			'update:user': {
				type: Array,
				default: null
			}
		},
		data() {
			return {
				show: false,
				x_client: this.client,
				x_server: this.server,
				x_user: this.user,
				banner: null,
				accesscode: null,
				verifycode: null
			};
		},
		computed: {
			host: {
				get() {
					return vistax.state.host;
				},
				set(value) {
					vistax.state.host = value;
				}
			}
		},
		watch: {
			host: {
				handler(value) {
					this.connect();
				}, immediate: true
			},
			client(value) { this.x_client = value; },
			x_client(value) { this.$emit('update:client', value); if(!value) this.connect(); },
			server(value) { this.x_server = value; },
			x_server(value) { this.$emit('update:server', value); },
			user(value) { this.x_user = value; },
			x_user(value) { this.$emit('update:user', value); }
		},
		methods: {
			async connect() {
				this.logout();
				this.x_client = await (this.host ? vistax.Client.fromCookie(this.secret, this.host) : vistax.Client.fromCookie(this.secret));
				this.banner = await this.x_client.XUS_INTRO_MSG();
				if((await this.x_client.userinfo()).result) try {
					var user = await this.x_client.XUS_GET_USER_INFO();
					this.x_user = user[0] ? user : null
				} catch(ex) {
					this.x_user = null;
				}
				this.$emit('update:user', this.x_user);
				this.show = !this.x_user;
				this.$emit('update:server', this.x_server = (await this.x_client.serverinfo()).result);
				console.log('Backend secret', this.secret);
				console.log(this.banner);
			},
			async login(evt) {
				if(this.x_client) {
					var res = await ((this.accesscode && this.verifycode) ? this.x_client.authenticate(this.accesscode + ';' + this.verifycode) : this.x_client.authenticate());
					if(!!res.result[0]) {
						var user = await this.x_client.XUS_GET_USER_INFO();
						this.x_user = user[0] ? user : null
					} else this.x_user = null;
					this.$emit('update:user', this.x_user);
					this.show = !this.x_user;
					this.$emit('update:server', this.x_server = (await this.x_client.serverinfo()).result);
					console.log('Authenticate', res);
				}
			},
			async logout(evt) {
				if(this.x_client) {
					console.log('Close', await this.x_client.close());
					this.$emit('update:client', this.x_client = null);
					this.$emit('update:server', this.x_server = null);
					this.$emit('update:user', this.x_user = null);
				}
				this.banner = null;
				this.show = true;
			}
		}
	};
</script>
