<?php

/**
 * Implements hook_install() using the name of the country as the base of
 * the function name.
 */
function bahrain_install() {
  // Make the entry in the country table.
  // VALUES = Country ID, Country Name, 2-digit Code, 3-digit Code, File Version
  db_insert('uc_countries')
    ->fields(array(
      'country_id' => 48,
      'country_name' => 'Bahrain',
      'country_iso_code_2' => 'BH',
      'country_iso_code_3' => 'BHR',
      'version' => 3,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(48, 'BH-13', 'Al Manamah'),
    array(48, 'BH-14', 'Al Janubiyah'),
    array(48, 'BH-15', 'Al Muharraq'),
    array(48, 'BH-16', 'Al Wusta'),
    array(48, 'BH-17', 'Ash Shamaliyah'),
  );

  $query = db_insert('uc_zones')->fields(array('zone_country_id', 'zone_code', 'zone_name'));
  foreach ($zones as $zone) {
    $query->values($zone);
  }
  $query->execute();

  // Set address format
  uc_set_address_format(
    48,
    "!company\r\n".
    "!first_name !last_name\r\n".
    "!street1 !street2\r\n".
    "!zone_name !postal_code\r\n".
    "!country_name_if"
  );
}

function bahrain_update($version) {
  switch ($version) {
    case 3:
      // Correct ISO-3166-2 and ISO-3166-3 codes
      db_update('uc_countries')
        ->fields(array('country_iso_code_2' => 'BH', 'country_iso_code_3' => 'BHR'))
        ->condition('country_id', 48)
        ->execute();

      // Remove old obsolete zones
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'AHD'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'AMG'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'JHA'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'STH'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'MAT'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'ARR'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'AMJ'))
        ->execute();
      db_delete('uc_zones')
        ->condition(db_and()
          ->condition('zone_country_id', 48)
          ->condition('zone_code', 'MAH'))
        ->execute();

      // Add some new zones
      $zones = array(
        array(48, 'BH-14', 'Al Janubiyah'),
      );

      $query = db_insert('uc_zones')->fields(array('zone_country_id', 'zone_code', 'zone_name'));
      foreach ($zones as $zone) {
        $query->values($zone);
      }
      $query->execute();

      // Rename remaining zones
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Al Manamah', 'zone_code' => 'BH-13'))
        ->condition('zone_country_id', 48)
        ->condition('zone_code', 'AMH')
        ->execute();
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Al Muharraq', 'zone_code' => 'BH-15'))
        ->condition('zone_country_id', 48)
        ->condition('zone_code', 'AMQ')
        ->execute();
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Al Wusta', 'zone_code' => 'BH-16'))
        ->condition('zone_country_id', 48)
        ->condition('zone_code', 'AMW')
        ->execute();
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Ash Shamaliyah', 'zone_code' => 'BH-17'))
        ->condition('zone_country_id', 48)
        ->condition('zone_code', 'AMS')
        ->execute();

      break;
  }
}
