/*
 Copyright 2017 Michael Hughes

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import { boundCheckAndAdd, boundCheckAndRetrieve } from './Common';

/**
 * Implements functions for converting to/from a Maidenhead Locator using WGS84 latitude and longitude
 * coordinates.
 *
 * Original description of system by @see {@link http://www.arrl.org/grid-squares|ARRL}
 * @module
 */

const firstLevelDivisions = 18;
const secondLevelDivisions = 10;
const thirdLevelDivisions =  24;
const startingLongitude = -180;
const startingLatitude = -90;

const alphabet = [
  'a',
  'b',
  'c',
  'd',
  'e',
  'f',
  'g',
  'h',
  'i',
  'j',
  'k',
  'l',
  'm',
  'n',
  'o',
  'p',
  'q',
  'r',
  's',
  't',
  'u',
  'v',
  'w',
  'x'
];

const charMap = alphabet.reduce((accumulator, val, idx) => {
  /* eslint no-param-reassign: 0 */
  accumulator[idx] = val;
  return accumulator;
},{});


/**
 * Converts to a Maidenhead Locator Square from a WGS84 decimal latitude, longitude pair
 * @param {number} latitude A WGS84 decimal latitude
 * @param {number} longitude A WGS84 decimal longitude
 * @return {string} A Maidenhead Locator Square
 */
export function mhLocatorFromLatLng(latitude, longitude) {
  const characters = [];

  // First level calculations
  boundCheckAndAdd(Math.floor(
    (longitude - startingLongitude) / 20), characters, alphabet);
  boundCheckAndAdd(Math.floor(
    (latitude - startingLatitude) / 10), characters, alphabet);
  // Only the top level square characters are upper case
  characters[0] = characters[0].toUpperCase();
  characters[1] = characters[1].toUpperCase();
  // End first level calculations


  return characters.join('');
}

/**
 * Converts to a decimal WGS84 latitude, longitude pair from a Maidenhead Locator Square
 * @param {string} mhLocator A Maiden Locator Square
 * @return {object} A point object with latitude and longitude
 */
export function latLngFromMhLocator(mhLocator) {

}

@@ -903,33 +903,166 @@
 nst 
-secondLevelDivisions = 10
+firstLevelLongitudeDivision = 360 / firstLevelDivisions;%0Aconst firstLevelLatitudeDivision = 180 / firstLevelDivisions;%0Aconst secondLevelLongitudeDivisions = 2
 ;%0Aco
@@ -1869,33 +1869,47 @@
 tions%0A  
-boundCheckAndAdd(
+let westLongitudeBoxBoundary = 
 Math.flo
@@ -1954,315 +1954,1149 @@
 ) / 
-20), characters, alphabet);%0A  boundCheckAndAdd(Math.floor(%0A    (latitude - startingLatitude) / 10), characters, alphabet);%0A  // Only the top level square characters are upper case%0A  characters%5B0%5D = characters%5B0%5D.toUpperCase();%0A  characters%5B1%5D = characters%5B1%5D.toUpperCase();%0A  // End first level calculations
+firstLevelLongitudeDivision);%0A  boundCheckAndAdd(westLongitudeBoxBoundary, characters, alphabet);%0A  let southLatitudeBoxBoundary = Math.floor(%0A    (latitude - startingLatitude) / firstLevelLatitudeDivision);%0A  boundCheckAndAdd(southLatitudeBoxBoundary, characters, alphabet);%0A  // Only the top level square characters are upper case%0A  characters%5B0%5D = characters%5B0%5D.toUpperCase();%0A  characters%5B1%5D = characters%5B1%5D.toUpperCase();%0A  // End first level calculations%0A%0A  // Second level, 'square', calculations%0A  westLongitudeBoxBoundary = Math.floor((longitude %25 firstLevelLongitudeDivision) / 2);%0A  southLatitudeBoxBoundary = Math.floor(latitude %25 firstLevelLatitudeDivision);%0A  characters.push(westLongitudeBoxBoundary.toString());%0A  characters.push(southLatitudeBoxBoundary.toString());%0A  // End second level calculations%0A%0A  // Third level, 'subsquares', calculations%0A  westLongitudeBoxBoundary = Math.floor((longitude %25 2) * (120 / 24));%0A  southLatitudeBoxBoundary = Math.floor((latitude %25 1) * (60 / 24));%0A  boundCheckAndAdd(westLongitudeBoxBoundary, characters, alphabet);%0A  boundCheckAndAdd(southLatitudeBoxBoundary, characters, alphabet);%0A
 %0A%0A%0A 

Fix a few things, break a few things