import { useEffect, useReducer } from "react";
import "../config/globals";

const assets = {
  floodZones: require("../assets/pontos_alagamento_peq.png"),
  riverLevel: [
    require("../assets/rio_baixo.png"),
    require("../assets/rio_normal.png"),
    require("../assets/rio_alto.png"),
    require("../assets/rio_transbordando.png"),
  ],
  rainLevel: [
    require("../assets/sem_chuva.png"),
    require("../assets/chuva_peq.png"),
    require("../assets/chuva_peq.png"),
    require("../assets/chuva_forte.png"),
    require("../assets/chuva_muito_forte.png"),
    require("../assets/chuva_pancadas.png"),
  ],
  pluviometer: require("../assets/diario_pluviometrico.png"),
};

// NOTE: For debug pourposes
var offset = 0.001;
var displacement = 0;

var ID = 0;

function partsePluviometer(row) {
  displacement += offset;
  return {
    ID: ++ID,
    title: "Pluviometro",
    coordinate: {
      latitude: row["Latitude"] + displacement,
      longitude: row["Longitude"],
    },
    image: assets.pluviometer,
    description: row["Pluviometer"],
  };
}

function parseFloodZones(row) {
  displacement += offset;
  return {
    ID: ++ID,
    title: row["Passable"] == 0 ? "Transponível" : "Intransponível",
    coordinate: {
      latitude: row["Latitude"],
      longitude: row["Longitude"] + displacement,
    },
    image: assets.floodZones,
    description: row["Description"],
  };
}

function parseRiverLevel(row) {
  displacement += offset;
  const riverLevel = ["Baixo", "Rio normal", "Alto", "Transfordando"];
  const riverIdx = row["RiverIdx"];
  return {
    ID: ++ID,
    title: "Nível do rio",
    coordinate: {
      latitude: row["Latitude"],
      longitude: row["Longitude"] + displacement,
    },
    image: assets.riverLevel[riverIdx],
    description: riverLevel[riverIdx],
  };
}

function parseRainLevel(row) {
  displacement += offset;
  const rainLevel = [
    "Sem chuva",
    "Chuva fraca",
    "Chuva moderada",
    "Chuva forte",
    "Chuva muito forte",
    "Pancada de chuva",
  ];
  const rainIdx = row["RainIdx"];
  return {
    ID: ++ID,
    title: "Nível da chuva",
    coordinate: {
      latitude: row["Latitude"],
      longitude: row["Longitude"] + displacement,
    },
    image: assets.rainLevel[rainIdx],
    description: rainLevel[rainIdx],
  };
}

function parseResult(db_result, parseRow) {
  var warnings = [];

  for (let i = 0; i < db_result.rows.length; ++i) {
    var row = db_result.rows.item(i);
    warnings.push(parseRow(row));
  }

  return warnings;
}

function genericSelect(dispatch, query, parseFunction) {
  useEffect(() => {
    global.userDataBase.transaction((tx) => {
      tx.executeSql(query, [], (tx, results) => {
        dispatch({ increment: parseResult(results, parseFunction) });
      });
    });
  }, []);
}

const initialState = { markers: [] };

function reducer(state = initialState, action) {
  return {
    markers: [...state.markers, ...action.increment],
  };
}

function useMarkers() {
  const [state, dispatch] = useReducer(reducer, initialState);

  const queriesToParsersMapper = [
    ["SELECT * FROM FloodZones;", parseFloodZones],
    ["SELECT * FROM Pluviometer;", partsePluviometer],
    ["SELECT * FROM RiverLevel;", parseRiverLevel],
    ["SELECT * FROM RainLevel;", parseRainLevel],
  ];

  queriesToParsersMapper.forEach(([query, parser]) => {
    console.log(query);
    genericSelect(dispatch, query, parser);
  });

  return state;
}

export default useMarkers;