import { remote } from 'electron';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { inject, observer } from 'mobx-react';

import PaymentStore from '../../stores/PaymentStore';
import UserStore from '../../stores/UserStore';
import AppStore from '../../stores/AppStore';

import AccountDashboard from '../../components/settings/account/AccountDashboard';
import ErrorBoundary from '../../components/util/ErrorBoundary';
import { WEBSITE } from '../../environment';

const { BrowserWindow } = remote;

export default @inject('stores', 'actions') @observer class AccountScreen extends Component {
  componentWillMount() {
    const {
      user,
    } = this.props.stores;

    user.getUserInfoRequest.invalidate({ immediately: true });
  }

  onCloseWindow() {
    const { user, payment } = this.props.stores;
    user.getUserInfoRequest.invalidate({ immediately: true });
  }

  reloadData() {
    const { user, payment } = this.props.stores;

    user.getUserInfoRequest.reload();
    payment.plansRequest.reload();
  }

  async handlePaymentDashboard() {
    const { actions, stores } = this.props;

    // actions.payment.createDashboardUrl();

    // const dashboard = await stores.payment.createDashboardUrlRequest;

    // if (dashboard.url) {
    //   const paymentWindow = new BrowserWindow({
    //     title: '🔒 Franz Subscription Dashboard',
    //     parent: remote.getCurrentWindow(),
    //     modal: false,
    //     width: 900,
    //     minWidth: 600,
    //     webPreferences: {
    //       nodeIntegration: false,
    //     },
    //   });
    //   paymentWindow.loadURL(dashboard.url);

    //   paymentWindow.on('closed', () => {
    //     this.onCloseWindow();
    //   });
    // }

    const url = `${WEBSITE}/user/billing?token=${stores.user.authToken}&utm_source=app&utm_medium=edit_profile`;

    actions.app.openExternalUrl({ url });
  }

  handleWebsiteLink(route) {
    const { actions, stores } = this.props;

    const url = `${WEBSITE}${route}?authToken=${stores.user.authToken}&utm_source=app&utm_medium=account_dashboard`;
    console.log(url);

    actions.app.openExternalUrl({ url });
  }

  render() {
    const { user, payment } = this.props.stores;
    const { user: userActions } = this.props.actions;

    const isLoadingUserInfo = user.getUserInfoRequest.isExecuting;
    const isLoadingPlans = payment.plansRequest.isExecuting;

    return (
      <ErrorBoundary>
        <AccountDashboard
          user={user.data}
          orders={payment.orders} // das muss raus
          isLoading={isLoadingUserInfo}
          isLoadingPlans={isLoadingPlans}
          userInfoRequestFailed={user.getUserInfoRequest.wasExecuted && user.getUserInfoRequest.isError}
          retryUserInfoRequest={() => this.reloadData()}
          onCloseSubscriptionWindow={() => this.onCloseWindow()}
          deleteAccount={userActions.delete}
          isLoadingDeleteAccount={user.deleteAccountRequest.isExecuting}
          isDeleteAccountSuccessful={user.deleteAccountRequest.wasExecuted && !user.deleteAccountRequest.isError}
          openEditAccount={() => this.handleWebsiteLink('/user/profile')}
          openBilling={() => this.handleWebsiteLink('/user/billing')}
          openInvoices={() => this.handleWebsiteLink('/user/invoices')}
        />
      </ErrorBoundary>
    );
  }
}

AccountScreen.wrappedComponent.propTypes = {
  stores: PropTypes.shape({
    user: PropTypes.instanceOf(UserStore).isRequired,
    payment: PropTypes.instanceOf(PaymentStore).isRequired,
    app: PropTypes.instanceOf(AppStore).isRequired,
  }).isRequired,
  actions: PropTypes.shape({
    payment: PropTypes.shape({
      createDashboardUrl: PropTypes.func.isRequired,
    }).isRequired,
    app: PropTypes.shape({
      openExternalUrl: PropTypes.func.isRequired,
    }).isRequired,
    user: PropTypes.shape({
      update: PropTypes.func.isRequired,
      delete: PropTypes.func.isRequired,
    }).isRequired,
  }).isRequired,
};

@@ -1,39 +1,4 @@
-import %7B remote %7D from 'electron';%0A
 impo
@@ -35,16 +35,16 @@
 react';%0A
+
 import P
@@ -466,43 +466,8 @@
 ';%0A%0A
-const %7B BrowserWindow %7D = remote;%0A%0A
 expo
@@ -733,33 +733,24 @@
 const %7B user
-, payment
  %7D = this.pr
@@ -978,861 +978,8 @@
  %7D%0A%0A
-  async handlePaymentDashboard() %7B%0A    const %7B actions, stores %7D = this.props;%0A%0A    // actions.payment.createDashboardUrl();%0A%0A    // const dashboard = await stores.payment.createDashboardUrlRequest;%0A%0A    // if (dashboard.url) %7B%0A    //   const paymentWindow = new BrowserWindow(%7B%0A    //     title: '%F0%9F%94%92 Franz Subscription Dashboard',%0A    //     parent: remote.getCurrentWindow(),%0A    //     modal: false,%0A    //     width: 900,%0A    //     minWidth: 600,%0A    //     webPreferences: %7B%0A    //       nodeIntegration: false,%0A    //     %7D,%0A    //   %7D);%0A    //   paymentWindow.loadURL(dashboard.url);%0A%0A    //   paymentWindow.on('closed', () =%3E %7B%0A    //     this.onCloseWindow();%0A    //   %7D);%0A    // %7D%0A%0A    const url = %60$%7BWEBSITE%7D/user/billing?token=$%7Bstores.user.authToken%7D&utm_source=app&utm_medium=edit_profile%60;%0A%0A    actions.app.openExternalUrl(%7B url %7D);%0A  %7D%0A%0A
   ha
@@ -1542,16 +1542,16 @@
 shboard%0A
+
         
@@ -1573,59 +1573,8 @@
 ta%7D%0A
-          orders=%7Bpayment.orders%7D // das muss raus%0A
     

Clean up