text: # Copyright 2019 Mycroft AI Inc.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #    http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  from flask import redirect, url_for, render_template, request, \
      session, flash, send_from_directory
  import json
  import time
  from os.path import join
  from personal_mycroft_backend.frontend import utils
  from personal_mycroft_backend.frontend.forms import LoginForm, PairingForm
  from personal_mycroft_backend.frontend.decorators import noindex, donation, \
      check_confirmed, requires_auth
  
  
  def get_routes(app, mail_sender):
      @app.route('/favicon.ico')
      def favicon():
          return send_from_directory(join(app.root_path, 'static'),
                                     'favicon.ico',
                                     mimetype='image/vnd.microsoft.icon')
  
      @app.route('/', methods=['GET', 'POST'])
      @noindex
      @donation
      def login():
          if not session.get('logged_in'):
              form = LoginForm(request.form)
              if request.method == 'POST':
                  username = request.form['username'].lower()
                  password = request.form['password']
                  if form.validate():
                      if utils.credentials_valid(username, password):
                          session['logged_in'] = True
                          session['username'] = username
                          return json.dumps({'status': 'Login successful'})
                      return json.dumps({'status': 'Invalid user/pass'})
                  return json.dumps({'status': 'Both fields required'})
              return render_template('login.html', form=form)
          user = utils.get_user()
          if user and user and user.confirmed:
              return render_template('home.html', user=user)
          return redirect(url_for('unconfirmed'))
  
      @app.route("/logout")
      @noindex
      @donation
      def logout():
          session['logged_in'] = False
          return redirect(url_for('login'))
  
      @app.route('/signup', methods=['GET', 'POST'])
      @noindex
      @donation
      def signup():
          if not session.get('logged_in'):
              form = LoginForm(request.form)
              if request.method == 'POST':
                  username = request.form['username'].lower()
                  password = utils.hash_password(request.form['password'])
                  email = request.form['email']
                  if form.validate():
                      if utils.username_taken(username):
                          flash("Username taken")
                          return json.dumps({'status': 'Username taken'})
                      if utils.mail_taken(email):
                          flash("Email taken")
                          return json.dumps({'status': 'Email taken'})
                      if utils.add_user(username, password, email, mail_sender):
                          session['logged_in'] = True
                          session['username'] = username
                          flash("Signup successful")
                          return json.dumps({'status': 'Signup successful'})
                      flash("Signup failed")
                      return json.dumps({'status': 'Signup failed'})
                  flash('All fields required')
                  return json.dumps({'status': 'All fields required'})
          return redirect(url_for('unconfirmed'))
  
      @app.route('/confirm/<token>')
      @noindex
      @donation
      @requires_auth
      def confirm(token):
          try:
              email = utils.confirm_token(token)
          except:
              flash('The confirmation link is invalid or has expired.')
              return redirect(url_for('login'))
  
          user = utils.get_user()
          if user and user.mail == email:
              if user.confirmed:
                  flash('Account already confirmed. Please login.')
              else:
                  utils.change_user(confirmed=True, confirmed_on=time.time())
                  flash('You have confirmed your account. Thanks!')
          return redirect(url_for('login'))
  
      @app.route('/unconfirmed', methods=['GET', 'POST'])
      @noindex
      @donation
      @requires_auth
      def unconfirmed():
          user = utils.get_user()
          if user and user.confirmed:
              return redirect(url_for('login'))
          flash('Please confirm your account!')
          return render_template('unconfirmed.html')
  
      @app.route('/resend', methods=['GET', 'POST'])
      @noindex
      @donation
      @requires_auth
      def resend():
          user = utils.get_user()
          if user and user.confirmed:
              flash('Already confirmed.')
              return redirect(url_for('login'))
          if request.method == 'POST' and user:
              token = utils.generate_confirmation_token(user.mail)
              confirm_url = url_for('confirm', token=token, _external=True)
              html = render_template('activate.html', confirm_url=confirm_url)
              subject = "Please confirm your email"
              utils.send_confirmation_mail(user.mail, subject, html, mail_sender)
              flash('A new confirmation email has been sent.')
              return render_template('resent.html')
          return render_template('unconfirmed.html')
  
      @app.route('/settings', methods=['GET', 'POST'])
      @noindex
      @donation
      @requires_auth
      @check_confirmed
      def settings():
          if request.method == 'POST':
              password = request.form['password']
              if password != "":
                  password = utils.hash_password(password)
              email = request.form['email']
              utils.change_user(password=password, mail=email)
              return json.dumps({'status': 'Saved'})
          user = utils.get_user()
          return render_template('settings.html', user=user)
  
      @app.route('/pair', methods=['GET', 'POST'])
      @noindex
      @donation
      @requires_auth
      @check_confirmed
      def pair():
          form = PairingForm(request.form)
          if request.method == 'POST':
              status = "NOT Paired"
              if form.validate():
                  code = request.form['code']
                  if utils.pair(code, mail_sender):
                      status = "Paired"
              return json.dumps({'status': status})
          return render_template('pair.html', form=form)
  
      @app.route('/devices', methods=['GET'])
      @noindex
      @donation
      @requires_auth
      @check_confirmed
      def devices():
          devices = utils.get_devices_json()
          return render_template('devices.html', devices=devices)
  
      @app.route('/config', methods=['GET'])
      @noindex
      @donation
      @requires_auth
      @check_confirmed
      def all_configs():
          configs = utils.get_configs_json()
          return render_template('config.html', configs=configs)
  
      @app.route('/config/<uuid>', methods=['GET'])
      @noindex
      @donation
      @requires_auth
      @check_confirmed
      def config(uuid):
          configs = utils.get_configs_json(uuid)
          return render_template('config.html', configs=configs)
  
      @app.route('/location/<uuid>', methods=['GET'])
      @noindex
      @donation
      @requires_auth
      @check_confirmed
      def location(uuid):
          locations = utils.get_location_json(uuid)
          return render_template('location.html', locations=locations)
  
      return app
  