import React, { useState } from "react";
import moment from "moment";

export const useTimeFrameComponent = (timeFrame: any, defaultFrame = "today") => {
  const [option, setOption] = useState<{ id: string, label: string }>()
  const [startDate, setStartDate] = useState<moment.Moment | undefined>();
  const [endDate, setEndDate] = useState<moment.Moment | undefined>();
  const [advancedFilter, setAdvancedFilter] = useState<boolean>(false)
  const [isValid, setIsValid] = useState<boolean>(true)

  const onChangeSelect = (value: any, newStart?: any, newEnd?: any) => {
    let start, end
    switch (value?.id) {
      case 'today':
        start = moment().startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'tomorrow':
        start = moment().add(1, 'days').startOf('day')
        end = moment.max(moment().add(1, 'days')).endOf('day')
        break;
      case 'yesterday':
        start = moment().subtract(1, 'days').startOf('day')
        end = moment.max(moment().subtract(1, 'days')).endOf('day')
        break;
      case 'last24hours':
        start = moment().subtract(24, 'hours').startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'week':
        start = moment().day(0).startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'lastWeek':
        start = moment().day(-7).startOf('day')
        end = moment().day(-1).endOf('day')
        break;
      case 'last7days':
        start = moment().subtract(7, 'days').startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'last15days':
        start = moment().subtract(15, 'days').startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'thisMonth':
        start = moment().startOf('month')
        end = moment.max(moment()).endOf('day')
        break;
      case 'last30Days':
        start = moment().subtract(30, 'days').startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'last90Days':
        start = moment().subtract(90, 'days').startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'lastMonth':
        start = moment(moment()).subtract(1, 'months').startOf('month')
        end = moment(moment()).subtract(1, 'months').endOf('month')
        break;
      case 'quarter':
        start = moment().startOf('quarter')
        end = moment.max(moment()).endOf('day')
        break;
      case 'last3month':
        start = moment().subtract(3, 'months').startOf('day')
        end = moment.max(moment()).endOf('day')
        break;
      case 'lastQuarter':
        const quarter = moment().quarter();
        start = moment().quarter(quarter - 1).startOf('quarter')
        end = moment().quarter(quarter - 1).endOf('quarter')
        break;
      case 'next7days':
        start = moment().startOf('day')
        end = moment.max(moment().add(7, 'days')).endOf('day')
        break;
      case 'next15days':
        start = moment().startOf('day')
        end = moment.max(moment().add(15, 'days')).endOf('day')
        break;
      case 'nextMonth':
        start = moment(moment()).add(1, 'months').startOf('month')
        end = moment(moment()).add(1, 'months').endOf('month')
        break;
      case 'next30Days':
        start = moment().startOf('day')
        end = moment.max(moment().add(30, 'days')).endOf('day')
        break;
      case 'next90Days':
        start = moment().startOf('day')
        end = moment.max(moment().add(90, 'days')).endOf('day')
        break;
      case 'custom':
        start = newStart ? moment(newStart) : undefined
        end = newEnd ? moment(newEnd) : undefined
        break;
      default:
        start = undefined
        end = undefined
        break;
    }
    setOption(value)
    setStartDate(start)
    setEndDate(end)
  }

  const reset = () => {
    onChangeSelect(timeFrame.find((x: any) => x.id === defaultFrame));
    //onChangeSelect(null)
  }

  const defaultvalue = (value: any) => {
    value && onChangeSelect(timeFrame.find((x: any) => x.id === value));
  }

  if (!option) {
    onChangeSelect(timeFrame.find((x: any) => x.id === defaultFrame))
  }

  const onChangeDateRange = (startValue: moment.Moment, endValue: moment.Moment) => {
    /*       const { start, end } = value;
        const newStart = start && new Date(start);
        const newEnd = end && new Date(end); */
    // Set New Date Range
    setStartDate(startValue)
    setEndDate(endValue)
  };

  const onChangeTime = (startTime: moment.Moment | null, endTime: moment.Moment | null) => {
    if (startTime)
      setStartDate(startTime)
    if (endTime)
      setEndDate(endTime)
  }

  const isInvalid = () => {
    if (option) {
      if ((startDate?.format("DD/MM/YYYY hh:mm:ss A") == endDate?.format("DD/MM/YYYY hh:mm:ss A")) || startDate?.isSameOrAfter(endDate)) {
        setIsValid(false)
        return true
      } else {
        setIsValid(true)
        return false
      }

    } else {
      setIsValid(false)
      return true
    }
  }

  return {
    onChangeSelect,
    onChangeDateRange,
    reset,
    option,
    startDate,
    endDate,
    advancedFilter,
    options: timeFrame,
    defaultvalue,
    onChangeTime,
    setAdvancedFilter,
    isValid,
    setIsValid,
    isInvalid,
    //handleCloseModal
  }

}