import React, { Component, Fragment } from 'react';
import {__} from '@wordpress/i18n';
import {Link} from "@woocommerce/components";
import ProductsTable from "../product-table";
import DashboardFilters from "../dashboard-filters";
import {filters, charts} from "./config";

import { updateQueryString } from '@woocommerce/navigation';
import SubscriptionChart from "../subscription-chart";
import SubscriptionSummary from "../subscription-summary";
import {getSelectedChart} from "../../lib/general";
import{getProductsChartData} from "../../packages/data/products-stats";
import {isEqual} from "lodash";


class DashboardProducts extends Component {

	constructor() {
		super( ...arguments );

		this.state = {
			stats       : {},
			isRequesting: true,
		};
	}

	componentDidMount() {
		const { query } = this.props;
		this.update( query );
	}

	getQueryWithoutChart = ( query ) => {
		const { chart, chartType, ...newQuery } = query;
		return { ...newQuery };
	};

	componentDidUpdate( prevProps, prevState ) {

		if ( !isEqual( prevProps.query, this.props.query ) ) {
			const prevQuery    = this.getQueryWithoutChart( prevProps.query );
			const currentQuery = this.getQueryWithoutChart( this.props.query );

			if ( !isEqual( prevQuery, currentQuery ) ) {
				this.update(this.props.query);
			} else {
				this.setState( {} );
			}
		}
	}

	update = async ( query ) => {

		this.setState( { isRequesting: true } );

		const stats = {
			primary  : await getProductsChartData( { query, dataType: 'primary' } ),
			secondary: await getProductsChartData( { query, dataType: 'secondary' } )
		};

		this.setState( { stats: stats, isRequesting: false } );
	};


	onFilterSelect(data) {
		const {query} = this.props;
		//remove products and filter from query.

		const { products, filter, ...newQuery } = query;

		updateQueryString({...newQuery, ...data});
	}


	render() {
		const { query,path} = this.props;
		const { isRequesting, stats } = this.state;
		const selectedChart = getSelectedChart(query.chart, charts);

		return <Fragment>

			<Link href="admin.php?page=yith_woocommerce_subscription&tab=dashboard&path=/" type="wp-admin"
			>{__('< back to main report', 'yith-woocommerce-subscription')}</Link>
			<h2>{__('Products Dashboard','yith-woocommerce-subscription')}</h2>

			<DashboardFilters path={path} query={query} report="products" filters={filters}  onFilterSelect={(data) => this.onFilterSelect(data)} onDataSelect={(date)=>this.update( date )}/>

			<SubscriptionSummary
				path={path}
				query={query}
				charts={charts}
				selectedChart={selectedChart}
				stats={stats}
				isRequesting={isRequesting}/>

			<SubscriptionChart path={path}
				query={query}
				selectedChart={selectedChart}
				stats={stats}
				filters={filters}
				isRequesting={isRequesting}/>

			<ProductsTable
				query={ query }
				isRequesting={isRequesting}
			/>

		</Fragment>
	}
}

export default DashboardProducts;

