import { useEffect, useState } from 'react';
import { timeElapsed } from '../utils/dateUtils';
import { useSelector } from 'react-redux';
import {THRESH_HOLD} from '../utils/constants';


export const ACTIONS = {
  CHECK_IN: 0,
  CHECK_OUT: 1,
  REGULARIZATION: 2,
  REQUESTED: 3,
  APPROVED: 4,
  REJECTED: 5
};


const useAttendanceStatus = (attendanceData) => {
  
  const [status, setStatus] = useState({
    status: "Unknown",
    action: null,
    disabled: true,
    text: "Unknown",
    color: 'btn-secondary',
  });

 
  useEffect(() => {
  
    const { checkInTime, checkOutTime, activity } = attendanceData;

      // if (activity === 0 && checkInTime === null && checkOutTime === null) {
      //   setStatus({
      //     status: "Check-In",
      //     action: ACTIONS.CHECK_IN,
      //     disabled: false,
      //     text: "Check In",
      //     color: 'btn-primary',
      //   });
      // } else if (activity === 0 && checkInTime !== null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)) {
      //   setStatus({
      //     status: "Check-Out",
      //     action: ACTIONS.CHECK_OUT,
      //     disabled: false,
      //     text: "Check Out",
      //     color: 'btn-primary',
      //   });

      // }else if (activity === 0 && checkInTime !== null && checkOutTime === null &&  timeElapsed(checkInTime,THRESH_HOLD)) {
      //   setStatus({
      //     status: "Request Regularize",
      //     action: ACTIONS.REGULARIZATION,
      //     disabled: false,
      //     text: "Request Regularize",
      //     color: 'btn-warning',
      //   });

      // } else if (activity === 1 && checkInTime !== null && checkOutTime === null || checkOutTime !== null && !timeElapsed(checkInTime,THRESH_HOLD)) {
      //   setStatus({
      //     status: "Check-In",
      //     action: ACTIONS.CHECK_IN,
      //     disabled: false,
      //     text: "Check-In",
      //     color: 'btn-primary',
      //   })
      
      // }else if (activity === 2 && checkInTime !== null && (checkOutTime !== null || checkOutTime === null  ) ) {
      //   setStatus({
      //     status: "Requested",
      //     action: ACTIONS.REQUESTED,
      //     disabled: true,
      //     text: "Requested",
      //     color: 'btn-info',
      //   });
      // } else if(activity === 4 && checkInTime !== null && checkOutTime !== null && !timeElapsed(checkInTime)){
      //   setStatus({
      //     status: "Approved",
      //     action: ACTIONS.APPROVED,
      //     disabled: true,
      //     text: "Approved",
      //     color: 'btn-success',
      //   });
      // }else{
      //   setStatus({
      //     status: "Rejected",
      //     action: ACTIONS.REJECTED,
      //     disabled: true,
      //     text: "Rejected",
      //     color: 'btn-danger',
      //   });
      // }

     
      if(activity === 0 && checkInTime === null && checkOutTime === null){
        setStatus({
          status: "Check-In",
          action: ACTIONS.CHECK_IN,
          disabled: false,
          text: "Check In",
          color: 'btn-primary',
        })
      }else if(activity === 0&& checkInTime === null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)){
        setStatus({
          status: "Check-In",
          action: ACTIONS.CHECK_IN,
          disabled: false,
          text: "Check In",
          color: 'btn-primary',
        })

      } else if(activity === 0&& checkInTime !== null && checkOutTime === null && timeElapsed(checkInTime,THRESH_HOLD)){
        setStatus({
          status: "Request Regularize",
          action: ACTIONS.REGULARIZATION,
          disabled: false,
          text: "Regularizes",
          color: 'btn-warning',
        });

      } 
      
      else if(activity === 1 && checkInTime !== null && checkOutTime === null && !timeElapsed(checkInTime,THRESH_HOLD)){
        setStatus({
          status: "Check-Out",
          action: ACTIONS.CHECK_OUT,
          disabled: false,
          text: "Check Out",
          color: 'btn-primary',
        });
      }else if(activity === 1 && checkInTime !== null && checkOutTime === null && timeElapsed(checkInTime,THRESH_HOLD)){
        setStatus({
          status: "Request Regularize",
          action: ACTIONS.REGULARIZATION,
          disabled: false,
          text: "Regularize",
          color: 'btn-warning',
        });
      }else if(activity === 4 && checkInTime !== null && checkOutTime !== null && !timeElapsed(checkInTime,THRESH_HOLD) ){
        setStatus({
          status: "Check-In",
          action: ACTIONS.CHECK_IN,
          disabled: false,
          text: "Check In",
          color: 'btn-primary',
        })
      }else if(activity === 2 && checkInTime !== null    ){
        setStatus({
          status: "Requested",
          action: ACTIONS.REQUESTED,
          disabled: true,
          text: "Requested",
          color: 'btn-info',
        });
      }else if(activity === 5 && checkInTime !== null  ){


        setStatus({
          status: "Rejected",
          action: ACTIONS.REJECTED,
          disabled: true,
          text: "Rejected",
          color: 'btn-danger',
        });
       
      }else {
        setStatus({
          status: "Approved",
          action: ACTIONS.APPROVED,
          disabled: true,
          text: "Approved",
          color: 'btn-success',
        });
      }








   
  }, [attendanceData]);

  return status;
};

export default useAttendanceStatus;
