'use strict'

import { Component }   from 'react'
import { Link }        from 'react-router'
import DocumentTitle   from 'react-document-title'
//import { decodeToken, SECP256K1Client, TokenSigner } from 'jsontokens'
import { makeAuthResponse, makeECPrivateKey, getAuthRequestFromURL, redirectUserToApp } from 'blockstack'
//import { Person, fetchAppManifest, publicKeyToAddress, makeDIDFromAddress, makeUUID4, nextMonth } from 'blockstack'

class AuthPage extends Component {

  constructor(props) {
    super(props)

    this.state = {
      authRequest: null,
      appURI: null
    }

    this.signIn = this.signIn.bind(this)
    this.signInWebAppUrl = this.signInWebAppUrl.bind(this)
    this.getAuthRequest = this.getAuthRequest.bind(this)
  }

  componentWillMount() {
    this.getAuthRequest()
  }

  getAuthRequest() {
    const authRequest = getAuthRequestFromURL()
    this.setState({
      authRequest: authRequest
    })
    /*
    fetchAppManifest(authRequest).then(appManifest => {
    }).catch((e) => {
      console.log(e.stack)
    })*/
  }

  signIn() {
    const privateKey = makeECPrivateKey()
    const profile = {
      '@type': 'Person',
      'name': 'Anonymous',
      'image': [
        {
          '@type': 'ImageObject',
          'name': 'avatar',
          'contentUrl': 'https://s3.amazonaws.com/onename/avatar-placeholder.png'
        }
      ]
    }
    const authResponse = makeAuthResponse(privateKey, profile)
    if (this.state.authRequest) {
      redirectUserToApp(this.state.authRequest, authResponse)
    }
  }

  signInWebAppUrl() {
    let suffix = `/auth?authRequest=${this.state.authRequest}`
    if (this.state.authRequest === null || this.state.authRequest === undefined) {
      suffix = '/'
    }
    return `https://browser.blockstack.com${suffix}`
  }

  render() {
    window.location = this.signInWebAppUrl()

    return (
      <DocumentTitle title="Sign In with Blockstack">
        <div>
          <section className="container-fluid spacing-container">
            <div className="container col-centered">
              <div className="container col-centered m-b-5" style={{ minHeight: '800px' }}>
                <h1>
                  Redirecting to Blockstack Browser...
                </h1>
              </div>
            </div>
          </section>
        </div>
      </DocumentTitle>
    )
  }

}

export default AuthPage

@@ -1577,291 +1577,108 @@
     
-let suffix = %60/auth?authRequest=$%7Bthis.state.authRequest%7D%60%0A    if (this.state.authRequest === null %7C%7C this.state.authRequest === undefined) %7B%0A      suffix = '/'%0A    %7D%0A    return %60https://browser.blockstack.com$%7Bsuffix%7D%60%0A  %7D%0A%0A  render() %7B%0A    window.location = this.signInWebAppUrl()%0A
+return %60https://browser.blockstack.com/auth?authRequest=$%7Bthis.state.authRequest%7D%60%0A  %7D%0A%0A  render() %7B
 %0A   
@@ -2009,65 +2009,914 @@
     
-Redirecting to Blockstack Browser...%0A                %3C/h1
+Sign in with Blockstack%0A                %3C/h1%3E%0A                %3Cdiv%3E%0A                  %3Ch5%3E%0A                    You're here because you clicked %22Sign in with%0A                    Blockstack%22 and you don't have the Blockstack app installed.%0A                  %3C/h5%3E%0A                  %3Cp%3E%0A                    %3CLink to=%22/install%22 className=%22btn btn-secondary%22%3E%0A                      Install Blockstack%0A                    %3C/Link%3E%0A                  %3C/p%3E%0A                  %7Bthis.state.authRequest ?%0A                    %3Cdiv%3E%0A                      %3Ch4%3E%0A                        - or -%0A                      %3C/h4%3E%0A                      %3Cp%3E%0A                        %3Ca className=%22btn btn-secondary%22 href=%7Bthis.signInWebAppUrl()%7D%3E%0A                          Sign In With Blockstack Web App%0A                        %3C/a%3E%0A                      %3C/p%3E%0A                    %3C/div%3E%0A                  : null %7D%0A                %3C/div
 %3E%0A  

Revert "redirect directly to browser.blockstack.com instead of giving buttons"