import ChangeLogModal from './change-log-modal';
const url = `${process.env.REACT_APP_FUDS_CHANGE_LOG_URL}`;

const testData = [
  {
    "id": 4713,
    "environment": "Public and Internal GIS",
    "district_code": "MVN",
    "change_type": "Data Change",
    "description": "Update locations for project at Pyote AAF Ground Gunnery Range (K06TX0146)\r\n\r\nOLD COORDINATES:\r\nBombing Range (01) 31.5575 -102.879444\r\nGround Gunnery Range (02) 31.585556 -102.898056\r\nSkeet Range (03) 31.553056 -102.875278\r\n\r\nNEW COORDINATES:\r\nBOMBING RANGE (01) 31.501163 -103.137657\r\nGROUND GUNNERY RANGE (02) 31.586237 -103.101082\r\nSKEET RANGE (03) 31.553096 -103.124618",
    "create_date": "2023-08-23T15:41:24Z",
    "change_date": "2023-08-23T15:41:24Z"
   },
   {
    "id": 4714,
    "environment": "Public and Internal GIS",
    "district_code": "MVD",
    "change_type": "Data Change",
    "description": "Update locations for project at Pyote AAF Ground Gunnery Range (K07TX0146)\r\n\r\nOLD COORDINATES:\r\nBombing Range (01) 31.5575 -102.879444\r\nGround Gunnery Range (02) 31.585556 -102.898056\r\nSkeet Range (03) 31.553056 -102.875278\r\n\r\nNEW COORDINATES:\r\nBOMBING RANGE (01) 31.501163 -103.137657\r\nGROUND GUNNERY RANGE (02) 31.586237 -103.101082\r\nSKEET RANGE (03) 31.553096 -103.124618",
    "create_date": "2023-07-23T15:41:24Z",
    "change_date": "2023-07-23T15:41:24Z"
    },
    {
    "id": 4715,
    "environment": "Public and Internal GIS",
    "district_code": "MVQ",
    "change_type": "Data Change",
    "description": "Update locations for project at Pyote AAF Ground Gunnery Range (K08TX0146)\r\n\r\nOLD COORDINATES:\r\nBombing Range (01) 31.5575 -102.879444\r\nGround Gunnery Range (02) 31.585556 -102.898056\r\nSkeet Range (03) 31.553056 -102.875278\r\n\r\nNEW COORDINATES:\r\nBOMBING RANGE (01) 31.501163 -103.137657\r\nGROUND GUNNERY RANGE (02) 31.586237 -103.101082\r\nSKEET RANGE (03) 31.553096 -103.124618",
    "create_date": "2023-07-13T15:41:24Z",
    "change_date": "2023-07-13T15:41:24Z"
  },
];

export default {
  name: 'changeLog',
  
  getReducer: () => {
    const initialData = {
      items: testData,
      renderModal: false,
    };

    return (state = initialData, { type, payload }) => {
      switch (type) {
        case "CHANGE_LOG_LOAD_TEST_DATA":
        case "CHANGE_LOG_LOAD_PRODUCTION_DATA":
        case "CHANGE_LOG_MODAL_WINDOW_OPENED":
          return Object.assign({}, state, payload);
        default:
          return state;
      }
    };
  },
  
  doSearchChangeLogData: (evt) => ({dispatch, store}) => {
    const targetValue = evt.target.value
    let logData =  store.selectChangeLogItems();
    if(targetValue.length > 2){
      const wkLogData = []
      logData.forEach((item) =>{
        let wkKey = {}
        let displayRecord = null;
        for (const [key, value] of Object.entries(item)){
          if (typeof value == "string"){
            let newObj = value.includes(targetValue) ? item: false; 
            if (newObj){
              displayRecord = true;
            }
          }
        }
        if(displayRecord){
          wkLogData.push(item);  
        }
        
      });

      logData = wkLogData;

      dispatch({
        type: "CHANGE_LOG_LOAD_TEST_DATA",
        payload: { items: logData },
      });

    } else{
      store.doGetChangeLogData();
    }
  },

  doGetChangeLogData: () => ({dispatch, store, anonGet}) => {
    anonGet(url, (err, response, body) => {
      if (err || response.statusCode !== 200) {
        // On Error Load Test Data
        dispatch({
          type: "CHANGE_LOG_LOAD_TEST_DATA",
          payload: { items: testData, renderModal: true },
        });

      } else {
      try {
           console.log("Production Change Log Data")
           console.log(body)
           let data = JSON.parse(body);
           console.log('data')
           console.log(data)
           let items = data.items
           console.log("ITEMS");
           console.log(items)
           dispatch({
            type: "CHANGE_LOG_LOAD_PRODUCTION_DATA",
            payload: { items: items, renderModal: true },
          });

        } catch (err) {
          // alert(" ! ! ! ERROR IN CATCH BLOCK ! ! ! ")
        }
      }
    });  
  },

  doOpenModalLog: () => ({dispatch, store}) => {
    store.doModalOpen({
      children: [ ChangeLogModal ],
      options: {}, 
      openedBy: 'ChangeLogButton'
    });
     
      
    dispatch({
      type: "CHANGE_LOG_MODAL_WINDOW_OPENED",
      payload: { renderModal: false },
    });

  },

  selectChangeLogItems: (state) => {
    return state.changeLog.items;
  },

  reactRenderChangeLogModal: (state) => {
    if (state.changeLog.renderModal)
      return { actionCreator: "doOpenModalLog" };
  },

}

