import React from 'react'
import PropTypes from 'prop-types'
import {Provider, withActivity} from './context'
import alertTypes from 'containers/alert-manager/alert-types'
import modalTypes from 'containers/modal-manager/modal-types'
import withPortfolioPage from '../portfolio-page'

import Header from './partials/header'
import Layout from 'components/layout'
import PortfolioActivityOrderStatus from './partials/portfolio-activity-order-status'
import PortfolioPayouts from './partials/portfolio-payouts'
import PortfolioRationaleLog from './partials/portfolio-rationale-log'
import TableTabs from 'components/table-tabs'

import styles from './styles.scss'
import BEMModule from 'utils/bem'
const bem = new BEMModule(styles)

class PortfolioActivityPage extends React.Component {
    static pageTitle = 'Activity'

    static tabViews = [
        {
            label: 'Orders',
            value: 'orders-tab',
            view: withActivity(PortfolioActivityOrderStatus),
        },
        {
            label: 'Payouts',
            value: 'payouts-tab',
            view: withActivity(PortfolioPayouts),
        },
        {
            label: 'Rationale Log',
            value: 'rationale-log',
            view: withActivity(PortfolioRationaleLog),
        },
    ]

    state = {
        activeIndex: 0,
    }

    exportData = () => {
        this.props.exportOrderHistory?.({
            portfolioId: this.props.portfolio.id,
        })
    }

    onTabSelected = ({index: activeIndex}) => {
        this.setState({activeIndex})
    }

    openCancelConfirmation = (orderId) => {
        this.props.showAlert &&
            this.props.showAlert({
                orderId,
                portfolioId: this.props.portfolio.id,
                type: alertTypes.CONFIRM_ORDER_CANCEL,
            })
    }

    openStatusInfo = (info) => {
        this.props.showAlert &&
            this.props.showAlert({
                body: info,
                type: alertTypes.ORDER_STATUS_INFO,
            })
    }

    openRationaleSummary = ({
        action,
        currency,
        price,
        quantity,
        security,
        status,
        strategy,
        summary,
        timestamp,
        type,
    }) => {
        this.props.showModal &&
            this.props.showModal({
                type: modalTypes.MODAL_ORDER_RATIONALE_LOG,
                modal: {
                    action,
                    currency,
                    price,
                    quantity,
                    security,
                    status,
                    strategy,
                    summary,
                    timestamp,
                    type,
                },
            })
    }

    get childContext() {
        return {
            exportData: this.exportData,
            getOrders: this.props.getOrders,
            getPayouts: this.props.getPayouts,
            getUser: this.props.getUser,
            openCancelConfirmation: this.openCancelConfirmation,
            openRationaleSummary: this.openRationaleSummary,
            openStatusInfo: this.openStatusInfo,
            orders: this.props.activity,
            payouts: this.props.payouts,
            portfolio: this.props.portfolio,
        }
    }

    render() {
        const pageClassNames = bem.classNames('c-portfolio-activity-page')
        const tabViewClassNames = bem.classNames(
            'c-portfolio-activity-page__tab-view'
        )

        const TabView =
            PortfolioActivityPage.tabViews[this.state.activeIndex].view

        return (
            <Layout.Main className={pageClassNames}>
                <Header exportData={this.exportData} />

                <Provider value={this.childContext}>
                    <TableTabs
                        tabIndex={this.state.activeIndex}
                        tabs={PortfolioActivityPage.tabViews}
                        setTab={this.onTabSelected}
                    />

                    <div className={tabViewClassNames}>
                        <TabView />
                    </div>
                </Provider>
            </Layout.Main>
        )
    }
}

PortfolioActivityPage.propTypes = {
    activity: PropTypes.shape({
        docs: PropTypes.array,
        limit: PropTypes.number,
        page: PropTypes.number,
        pages: PropTypes.number,
        total: PropTypes.number,
    }),
    exportOrderHistory: PropTypes.func,
    getOrders: PropTypes.func,
    getPayouts: PropTypes.func,
    getUser: PropTypes.func,
    payouts: PropTypes.shape({
        docs: PropTypes.array,
        limit: PropTypes.number,
        page: PropTypes.number,
        pages: PropTypes.number,
        total: PropTypes.number,
    }),
    portfolio: PropTypes.object,
    showAlert: PropTypes.func,
    showModal: PropTypes.func,
    users: PropTypes.object,
}

PortfolioActivityPage.defaultProps = {
    activity: {
        docs: [],
        limit: 10,
        page: 1,
        pages: 1,
        total: 0,
    },
    payouts: {
        docs: [],
        limit: 10,
        page: 1,
        pages: 1,
        total: 0,
    },
}

export default withPortfolioPage(PortfolioActivityPage)
