// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

import React, { Component } from 'react';
import { observer } from 'mobx-react';

import { api } from '../parity';
import DappsStore from '../dappsStore';

import Input from '../Input';
import SelectAccount from '../SelectAccount';
import styles from './dapp.css';

@observer
export default class Dapp extends Component {
  dappsStore = DappsStore.instance();

  render () {
    const app = this.dappsStore.isNew || this.dappsStore.isEditing
      ? this.dappsStore.wipApp
      : this.dappsStore.currentApp;

    if (!app) {
      return null;
    }

    return (
      <div className={ styles.app }>
        { this.dappsStore.isNew ? this.renderOwnerSelect(app) : this.renderOwnerStatic(app) }
        { this.renderInputs(app) }
      </div>
    );
  }

  renderInputs (app) {
    if (this.dappsStore.isNew) {
      return null;
    }

    return [
      this.renderHashInput(app, 'image', 'Image hash, as generated by Githubhint', true),
      this.renderHashInput(app, 'manifest', 'Manifest hash, as generated by Githubhint'),
      this.renderHashInput(app, 'content', 'Content hash, as generated by Githubhint')
    ];
  }

  renderOwnerSelect (app) {
    const overlayImage = (
      <img
        className={ styles.overlayImage }
        src={ api.util.createIdentityImg(this.dappsStore.currentAccount.address, 4) }
      />
    );

    return (
      <Input
        hint={ this.dappsStore.currentAccount.address }
        label='Owner, select the application owner and editor'
        overlay={ overlayImage }
      >
        <SelectAccount />
      </Input>
    );
  }

  renderOwnerStatic (app) {
    const overlayImage = (
      <img
        className={ styles.overlayImage }
        src={ api.util.createIdentityImg(app.owner, 4) }
      />
    );

    return (
      <Input
        hint={ app.owner }
        label='Owner, the application owner and editor'
        overlay={ overlayImage }
      >
        <input value={ app.ownerName } readOnly />
      </Input>
    );
  }

  renderHashInput (app, type, label, withImage = false) {
    const onChange = (event) => this.onChangeHash(event, type);
    const hash = app[`${type}Hash`];

    let overlayImage = null;

    if (withImage && hash) {
      overlayImage = (
        <img
          className={ styles.overlayImage }
          src={ `/api/content/${hash.substr(2)}` }
        />
      );
    }

    return (
      <Input
        hint={ app[`${type}Error`] || app[`${type}Url`] || '...' }
        label={ label }
        key={ `${type}Edit` }
        overlay={ overlayImage }
      >
        <input
          value={ app[`${type}Hash`] || '' }
          data-dirty={ app[`${type}Changed`] }
          data-error={ !!app[`${type}Error`] }
          readOnly={ !this.dappsStore.isEditing && !this.dappsStore.isNew }
          onChange={ onChange }
        />
      </Input>
    );
  }

  onChangeHash (event, type) {
    if (!this.dappsStore.isNew && !this.dappsStore.isEditing) {
      return;
    }

    const hash = event.target.value;
    let changed = false;
    let url = null;

    if (this.dappsStore.isNew) {
      if (hash && hash.length) {
        changed = true;
      }
    } else {
      if (this.dappsStore.currentApp[`${type}Hash`] !== hash) {
        changed = true;
      } else {
        url = this.dappsStore.currentApp[`${type}Url`];
      }
    }

    this.dappsStore.editWip({
      [`${type}Changed`]: changed,
      [`${type}Error`]: null,
      [`${type}Hash`]: hash,
      [`${type}Url`]: changed ? 'Resolving url from hash' : url
    });

    if (changed) {
      if (hash.length) {
        this.dappsStore
          .lookupHash(hash)
          .then((url) => {
            this.dappsStore.editWip({
              [`${type}Error`]: url ? null : 'Unable to resolve url',
              [`${type}Url`]: url
            });
          });
      } else {
        this.dappsStore.editWip({ [`${type}Url`]: null });
      }
    }
  }
}
