/*
 * This file is part of OsmInEdit, released under ISC license (see LICENSE.md)
 *
 * Copyright (c) Adrien Pavie 2019
 * Copyright (c) Daimler AG 2019
 *
 * Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

import React, { Component } from 'react';
import Button from 'react-bootstrap/Button';
// import I18n from '../../config/locales/ui';
import Modal from 'react-bootstrap/Modal';
import InputGroup from 'react-bootstrap/InputGroup';
import Form from 'react-bootstrap/Form';
import Nav from 'react-bootstrap/Nav';

/**
 * Login dialog asks user to login or create account on OSM.org before editing.
 */
class LoginDialog extends Component {
	state = {
		loginMode: true,
		form: undefined,
		userData: {
			account: "",
			password: "",
		},
	}

	loginForm = <Form>
			<Form.Group>
			<br />
				<InputGroup >
					<InputGroup.Text id="textAccount">账号</InputGroup.Text>
					<Form.Control
						type="text"
						placeholder=""
						aria-label="textAccount"
						aria-describedby="textAccount"
						onChange={e => {this.handleAccountInput(e)}}
					/>
				</InputGroup>
				<br />
				<InputGroup>
					<InputGroup.Text id="textPassword">密码</InputGroup.Text>
					<Form.Control
						type="password"
						placeholder=""
						aria-label="textPassword"
						aria-describedby="textPassword"
						onChange={e => {this.handlePasswordInput(e)}}
					/>
				</InputGroup>
				<br />
				{window.CONFIG.always_authenticated ?
					/*I18n.t("To view and edit map data, you need to connect first using an OpenStreetMap account.")*/
					"要查看和编辑地图数据，您需要先使用帐户进行连接。"
					: /*I18n.t("To edit map data, you need to connect first using an OpenStreetMap account.")*/
					"要编辑地图数据，您需要先使用帐户进行连接。"
				}
			</Form.Group>
		</Form>;

	registerForm = <Form >
			<Form.Group>
			<br />
				<InputGroup >
					<InputGroup.Text id="textAccount">账号</InputGroup.Text>
					<Form.Control
						type="text"
						placeholder=""
						aria-label="textAccount"
						aria-describedby="textAccount"
						onChange={e => {this.handleAccountInput(e)}}
					/>
				</InputGroup>
				<br />
				<InputGroup>
					<InputGroup.Text id="textPassword">密码</InputGroup.Text>
					<Form.Control
						type="password"
						placeholder=""
						aria-label="textPassword"
						aria-describedby="textPassword"
						onChange={e => {this.handlePasswordInput(e)}}
					/>
				</InputGroup>
				<br />
				<InputGroup>
					<InputGroup.Text id="textConfirm">确认密码</InputGroup.Text>
					<Form.Control
						type="password"
						placeholder=""
						aria-label="textConfirm"
						aria-describedby="textConfirm"
						onChange={e => {this.handleConfirmInput(e)}}
					/>
				</InputGroup>
			</Form.Group>
		</Form>;
	
	componentDidMount(){
		this.setState({form: this.loginForm});
	}

	selectMode = (key, event) => {
		if(key === "login"){
			this.setState({form: this.loginForm, loginMode: true});
		} else {
			this.setState({form: this.registerForm, loginMode: false});
		}
	}

	handleAccountInput = (event) => {
		this.setState({userData: {...this.state.userData, account: event.target.value}});
	}

	handlePasswordInput = (event) => {
		this.setState({userData: {...this.state.userData, password: event.target.value}});
	}

	handleConfirmInput = (event) => {		
		this.setState({userData: {...this.state.userData, confirm: event.target.value}});
	}

	clickOk = () => {



		this.props.onLogin(this.state.userData)
		// this.props.onClose();
	}

	render() {
		const canClose = !window.CONFIG.always_authenticated;

		return <Modal show={this.props.show} onHide={this.props.onClose} style={{zIndex: 20010}}>
			<Modal.Header closeButton={canClose}>
				<Modal.Title>登录或者注册账号{/*I18n.t("Login or create account")*/}</Modal.Title>
			</Modal.Header>

			<Modal.Body>
				<Nav variant="tabs" defaultActiveKey="login" onSelect={this.selectMode}>
					<Nav.Item>
						<Nav.Link eventKey="login">登录</Nav.Link>
					</Nav.Item>
					<Nav.Item>
						<Nav.Link eventKey="register" disabled>注册</Nav.Link>
					</Nav.Item>
				</Nav>
				{this.state.form}
				</Modal.Body>

			<Modal.Footer>
				{canClose &&
					<Button variant="secondary" onClick={this.props.onClose}>
						取消{/*I18n.t("Cancel")*/}
					</Button>
				}

				<Button variant="primary" onClick={this.clickOk}>
					{/*I18n.t("Login or create account")*/}
					{this.state.loginMode ? "登录" : "注册"}
				</Button>
			</Modal.Footer>
		</Modal>;
	}
}

export default LoginDialog;
