export default {
  name: "questionsTab",

  getReducer: () => {
    const initialData = {
      questionsTabStatus: false,
      errorList: {},
      applicationName: "",
      emailPath: ""
    };
    return (state = initialData, { type, payload }) => {
      switch (type) {
        case "PARAMETERS_SET":
        case "FORM_SUBMITTED":
        case "FORM_ERROR":
        case "API_POST_SUCCESS":
        case "API_POST_ERROR":
        case "ACTIVATE_QUESTIONS_TAB":
        case "DEACTIVATE_QUESTIONS_TAB":
        case "VALIDATION_COMPLETE":
          return Object.assign({}, state, payload);
        default:
          return state;
      }
    };
  },
  doHandleFormErrors:
    (outputs) =>
    ({ dispatch, store }) => {

    let formErrors = store.selectErrorList();
    let formValidation = true;
    const regex = /[a-zA-Z]/g;
   
    //First Name
    if (outputs.first_Name == " ") {
      formValidation = false;
      formErrors["First_Name"] =" *Field Cannot be empty ";
    }

    if (typeof outputs.first_Name !== "undefined") {
      if (!outputs.first_Name.match(regex)) {
        formValidation = false;
        formErrors["First_Name"] = " *Only letters ";
      }
    }

    //Last Name
    if (outputs.last_Name == " ") {
      formValidation = false;
      formErrors["Last_Name"] = " *Field Cannot be empty ";
    }

    if (typeof outputs.last_Name !== "undefined") {
      if (!outputs.last_Name.match(regex)) {
        formValidation = false;
        formErrors["Last_Name"] = " *Only letters ";
      }
    }

    //Email
    if (outputs.e_mail == " ") {
      formValidation = false;
      formErrors["email"] = " *Field Cannot be empty ";
    }

    if (typeof outputs.e_mail !== "undefined") {
      let lastAtPos = outputs.e_mail.lastIndexOf("@");
      let lastDotPos = outputs.e_mail.lastIndexOf(".");

      if (
        !(
          lastAtPos < lastDotPos &&
          lastAtPos > 0 &&
          outputs.e_mail.indexOf("@@") == -1 &&
          lastDotPos > 2 &&
          outputs.e_mail.length - lastDotPos > 2
        )
      ) {
        formValidation = false;
        formErrors["email"] = " *Email is not valid ";
      }
    }
    // Select issue
    if (outputs.sel_issue == " ") {
      formValidation = false;
      formErrors["issue"] = " *Please select an issue ";
    }

  // Breif Description
  if (outputs.add_comments == " ") {
    formValidation = false;
    formErrors["add_comments"] = " *Please provide a breif description of issue ";
  }
    
    dispatch({
        type: "VALIDATION_COMPLETE",
        payload: {
          errorList: formErrors,
        },
      });
    return formValidation;

      
    },
    doSetContactFormConfiguration:
    (fudsFormParams) =>
    ({ dispatch, store}) => {
      
      const app_name = fudsFormParams.application_name
      const app_path = fudsFormParams.email_url

      dispatch({
        type: "PARAMETERS_SET",
        payload: {
          applicationName:app_name,
          emailPath:app_path
        },
      });
    },
    doSetContactFormIssues:
    (issues) =>
    ({ dispatch, store, apiPost }) => {
      console.log(issues);
      
      // Here we want to POST the contents of the Form
      // (path, payload, callback)

      const path = store.selectEmailPath();

      const params = {
        application_name: store.selectApplicationName(),
        first_name: issues.first_Name,
        last_name: issues.last_Name,
        email_address: issues.e_mail,
        category: issues.sel_issue,
        comments: issues.add_comments
      };
      
      apiPost(path ,params , (err, response, body) => {
        if(err || response.statusCode !== 200){
          console.log(err, response);
          dispatch({
            type:"API_POST_ERROR",
            payload: {
              notification: {
                message: "api-error",
                level: "danger",
              },
            },
          });
        }else{
          dispatch({
            type:"API_POST_SUCCESS",
            payload: {
              notification: {
                message: "api-success",
                level: "success",
              },
            },
          });
          console.log(response)
        }
      });
    },

    doSubmitNotification:
    (bool) =>
    ({ dispatch, store }) => {
      
      if(bool){
        store.doNotificationsFire({
            title: "Form Submitted",
            message: "Form Submitted",
            level: "success",
          });
          dispatch({
            type: "FORM_SUBMITTED",
            payload: {},
          });
      }else{
        store.doNotificationsFire({
          title: "Form Error",
          message: "Form Has Errors",
          level: "danger",
        });
        dispatch({
          type: "FORM_ERROR",
          payload: {},
        });

      }
    },
  doSubmitNotification:
    (bool) =>
    ({ dispatch, store }) => {
      
      if(bool){
        store.doNotificationsFire({
            title: "Form Submitted",
            message: "Form Submitted",
            level: "success",
          });
          dispatch({
            type: "FORM_SUBMITTED",
            payload: {},
          });
      }else{
        store.doNotificationsFire({
          title: "Form Error",
          message: "Form Has Errors",
          level: "danger",
        });
        dispatch({
          type: "FORM_ERROR",
          payload: {},
        });

      }
    },
  doQuestionBarActivate:
    () =>
    ({ dispatch }) => {
      dispatch({
        type: "ACTIVATE_QUESTIONS_TAB",
        payload: {
          questionsTabStatus: true,
        },
      });
    },

  selectApplicationName: (state) => {
      return state.questionsTab.applicationName;
    },
  selectEmailPath: (state) => {
      return state.questionsTab.emailPath;
    },
  selectQuestionsTabStatus: (state) => {
    return state.questionsTab.questionsTabStatus;
  },
  selectErrorList: (state) => {
    return state.questionsTab.errorList;
  },
};
