// import

import React, {Component, PropTypes} from 'react';
import styles from './JobsFilter.css';
import cn from 'classnames';
import _ from 'lodash';
import {getJobNiceInterval} from '../JobsHelper/JobsHelper.js';
import {connect} from 'react-redux';

// vars

const searchFields = ['code', 'description', 'interval', 'name', 'resultQuery', 'statusEmail', 'resultEmail'];

// fns

function shouldKeep(job, textFilter, useLocalTime) {
  if (textFilter.length) {
    return textFilter.every((text) => {
      if (_.includes((getJobNiceInterval(job.cronString, useLocalTime) || '').toLowerCase(), text)) {
        return true;
      }

      return _.some(job, (val, key) => {
        if (!_.isString(val) || searchFields.indexOf(key) === -1) {
          return false;
        }

        if (Array.isArray(val)) {
          val = val.join(', ');
        }

        return _.includes(val.toLowerCase(), text);
      });
    });
  }

  return true;
}

function filterSomeJobs(jobs = [], textFilter, useLocalTime) {
  return (jobs || []).reduce((keep, job) => {
    job.shouldKeep = shouldKeep(job, textFilter, useLocalTime);

    if (job.children) {
      job.children = filterSomeJobs(job.children, textFilter, useLocalTime);
    }

    if (job.shouldKeep || job.children.length) {
      keep.push(job);
    }

    return keep;
  }, []);
}

// export

@connect((state) => {
  return {
    useLocalTime: state.localStorage.useLocalTime === 'true',
  };
})
export default class JobsFilter extends Component {
  static propTypes = {
    className: PropTypes.string,
    jobs: PropTypes.array,
    onFilter: PropTypes.func.isRequired,
    runs: PropTypes.array,
    useLocalTime: PropTypes.bool.isRequired,
  };

  state = {
    text: [],
  };

  componentDidMount() {
    this.filterJobs();
  }

  componentDidUpdate(prevProps, prevState) {
    if (this.props.jobs !== prevProps.jobs || this.props.runs !== prevProps.runs || this.state.text !== prevState.text) {
      this.filterJobs();
    }
  }

  className() {
    return cn(styles.JobsFilter, this.props.className);
  }

  onChange(event) {
    this.setState({
      text: _.compact(event.target.value.toLowerCase().split(' ')),
    });
  }

  filterJobs() {
    const {useLocalTime, jobs} = this.props;
    const textFilter = this.state.text;

    this.props.onFilter(filterSomeJobs(_.cloneDeep(jobs), textFilter, useLocalTime));
  }

  render() {
    return (
      <input className={this.className()} type="text" placeholder="Search" onChange={::this.onChange}/>
    );
  }
}
