import { useReducer } from "react";
import { ON_SET_DATE, ON_SET_YEAR, ON_SET_MONTH } from "./Datepicker.actionConstants";

function stateReducer() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
    hour: 0,
    minute: 0,
    noon: 'AM',
    year: 1,
    month: 1,
    date: 1
  };
  let action = arguments.length > 1 ? arguments[1] : undefined;
  console.log(action.data, 'ad');

  switch (action.type) {
    case ON_SET_YEAR:
      return { ...state,
        year: action.data
      };
    //return Object.assign({},state,{year:action.data})

    case ON_SET_MONTH:
      return { ...state,
        month: action.data
      };
    //return Object.assign({},state,{month:action.data})

    case ON_SET_DATE:
      return { ...state,
        year: action.data.year,
        date: action.data.date,
        month: action.data.month,
        day: action.data.day
      };
    //return Object.assign({},state,{date:action.data,month:action.data.month,year:action.data.year})
  }
}

export function handleTimeChange(initialValue, applyHook) {
  let [state, dispatchVal] = useReducer(stateReducer, initialValue);

  function setHours(e, type, action) {
    let modifiedState = e;
    dispatchVal({
      type,
      data: modifiedState
    }, action && action(modifiedState));

    if (applyHook) {
      let newState = applyHook(state.hour, modifiedState, type);
      dispatchVal({
        type,
        data: newState
      }, action && action(newState));
    }
  }

  function setMinutes(e, type, action) {
    let modifiedState = e;
    dispatchVal({
      type,
      data: modifiedState
    }, action && action(modifiedState));

    if (applyHook) {
      let newState = applyHook(state.minute, modifiedState, type);
      dispatchVal({
        type,
        data: newState
      }, action && action(newState));
    }
  }

  function setNoon(e, type, action) {
    let modifiedState = e;
    dispatchVal({
      type,
      data: modifiedState
    }, action && action(modifiedState));

    if (applyHook) {
      let newState = applyHook(state.noon, modifiedState, type);
      dispatchVal({
        type,
        data: newState
      }, action && action(newState));
    }
  }

  return [state.hour, state.minute, state.noon, setHours, setMinutes, setNoon];
}
export function handleDateAction(initialValue, applyHook) {
  let [state, dispatchVal] = useReducer(stateReducer, initialValue);

  function setYear(e, type, action) {
    let modifiedState = e;
    dispatchVal({
      type,
      data: modifiedState
    }, action && action(modifiedState));

    if (applyHook) {
      let newState = applyHook(state.year, modifiedState, type);
      dispatchVal({
        type,
        data: newState
      }, action && action(newState));
    }
  }

  function setMonth(e, type, action) {
    let modifiedState = e;
    dispatchVal({
      type,
      data: modifiedState
    }, action && action(modifiedState));

    if (applyHook) {
      let newState = applyHook(state.month, modifiedState, type);
      dispatchVal({
        type,
        data: newState
      }, action && action(newState));
    }
  }

  function setDate(e, type, action) {
    let modifiedState = e; //console.log(modifiedState,'ms',e,type,action);

    dispatchVal({
      type,
      data: modifiedState
    }, action && action(modifiedState));

    if (applyHook) {
      let newState = applyHook(state.date, modifiedState, type);
      dispatchVal({
        type,
        data: newState
      }, action && action(newState));
    }
  }

  return [state.year, state.month, state.date, setYear, setMonth, setDate];
}