<?php

/**
 * Implements hook_install() using the name of the country as the base of
 * the function name.
 */
function thailand_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' => 764,
      'country_name' => 'Thailand',
      'country_iso_code_2' => 'TH',
      'country_iso_code_3' => 'THA',
      'version' => 2,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(764, 'TH-10', 'Bangkok <Krung Thep Maha Nakhon>'),
    array(764, 'TH-S', 'Pattaya'),
    array(764, 'TH-37', 'Amnat Charoen'),
    array(764, 'TH-15', 'Ang Thong'),
    array(764, 'TH-31', 'Buri Ram'),
    array(764, 'TH-24', 'Chachoengsao'),
    array(764, 'TH-18', 'Chai Nat'),
    array(764, 'TH-36', 'Chaiyaphum'),
    array(764, 'TH-22', 'Chanthaburi'),
    array(764, 'TH-50', 'Chiang Mai'),
    array(764, 'TH-57', 'Chiang Rai'),
    array(764, 'TH-20', 'Chon Buri'),
    array(764, 'TH-86', 'Chumphon'),
    array(764, 'TH-46', 'Kalasin'),
    array(764, 'TH-62', 'Kamphaeng Phet'),
    array(764, 'TH-71', 'Kanchanaburi'),
    array(764, 'TH-40', 'Khon Kaen'),
    array(764, 'TH-81', 'Krabi'),
    array(764, 'TH-52', 'Lampang'),
    array(764, 'TH-51', 'Lamphun'),
    array(764, 'TH-42', 'Loei'),
    array(764, 'TH-16', 'Lop Buri'),
    array(764, 'TH-58', 'Mae Hong Son'),
    array(764, 'TH-44', 'Maha Sarakham'),
    array(764, 'TH-49', 'Mukdahan'),
    array(764, 'TH-26', 'Nakhon Nayok'),
    array(764, 'TH-73', 'Nakhon Pathom'),
    array(764, 'TH-48', 'Nakhon Phanom'),
    array(764, 'TH-30', 'Nakhon Ratchasima'),
    array(764, 'TH-60', 'Nakhon Sawan'),
    array(764, 'TH-80', 'Nakhon Si Thammarat'),
    array(764, 'TH-55', 'Nan'),
    array(764, 'TH-96', 'Narathiwat'),
    array(764, 'TH-39', 'Nong Bua Lam Phu'),
    array(764, 'TH-43', 'Nong Khai'),
    array(764, 'TH-12', 'Nonthaburi'),
    array(764, 'TH-13', 'Pathum Thani'),
    array(764, 'TH-94', 'Pattani'),
    array(764, 'TH-82', 'Phangnga'),
    array(764, 'TH-93', 'Phatthalung'),
    array(764, 'TH-56', 'Phayao'),
    array(764, 'TH-67', 'Phetchabun'),
    array(764, 'TH-76', 'Phetchaburi'),
    array(764, 'TH-66', 'Phichit'),
    array(764, 'TH-65', 'Phitsanulok'),
    array(764, 'TH-54', 'Phrae'),
    array(764, 'TH-14', 'Phra Nakhon Si Ayutthaya'),
    array(764, 'TH-83', 'Phuket'),
    array(764, 'TH-25', 'Prachin Buri'),
    array(764, 'TH-77', 'Prachuap Khiri Khan'),
    array(764, 'TH-85', 'Ranong'),
    array(764, 'TH-70', 'Ratchaburi'),
    array(764, 'TH-21', 'Rayong'),
    array(764, 'TH-45', 'Roi Et'),
    array(764, 'TH-27', 'Sa Kaeo'),
    array(764, 'TH-47', 'Sakon Nakhon'),
    array(764, 'TH-11', 'Samut Prakan'),
    array(764, 'TH-74', 'Samut Sakhon'),
    array(764, 'TH-75', 'Samut Songkhram'),
    array(764, 'TH-19', 'Saraburi'),
    array(764, 'TH-91', 'Satun'),
    array(764, 'TH-17', 'Sing Buri'),
    array(764, 'TH-33', 'Si Sa Ket'),
    array(764, 'TH-90', 'Songkhla'),
    array(764, 'TH-64', 'Sukhothai'),
    array(764, 'TH-72', 'Suphan Buri'),
    array(764, 'TH-84', 'Surat Thani'),
    array(764, 'TH-32', 'Surin'),
    array(764, 'TH-63', 'Tak'),
    array(764, 'TH-92', 'Trang'),
    array(764, 'TH-23', 'Trat'),
    array(764, 'TH-34', 'Ubon Ratchathani'),
    array(764, 'TH-41', 'Udon Thani'),
    array(764, 'TH-61', 'Uthai Thani'),
    array(764, 'TH-53', 'Uttaradit'),
    array(764, 'TH-95', 'Yala'),
    array(764, 'TH-35', 'Yasothon'),
    array(764, 'TH-38', 'Bueng Kan'),
  );

  $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(
    764,
    "!first_name !last_name\r\n".
    "!company\r\n".
    "!street1\r\n".
    "!street2\r\n".
    "!city, !zone_name !postal_code\r\n".
    "!country_name"
  );
}

function thailand_update($version) {
  switch ($version) {
    case 3:

      // Add some new zones
      $zones = array(
        array(764, 'TH-S', 'Pattaya'),
        array(764, 'TH-38', 'Bueng Kan'),
      );

      $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
      $zones = array(
        array(764, '1', 'TH-10', 'Bangkok <Krung Thep Maha Nakhon>'),
        array(764, '2', 'TH-12', 'Nonthaburi'),
        array(764, '3', 'TH-11', 'Samut Prakan'),
        array(764, '4', 'TH-13', 'Pathum Thani'),
        array(764, '5', 'TH-83', 'Phuket'),
        array(764, '6', 'TH-74', 'Samut Sakhon'),
        array(764, '7', 'TH-75', 'Samut Songkhram'),
        array(764, '8', 'TH-73', 'Nakhon Pathom'),
        array(764, '9', 'TH-94', 'Pattani'),
        array(764, '10', 'TH-14', 'Phra Nakhon Si Ayutthaya'),
        array(764, '11', 'TH-15', 'Ang Thong'),
        array(764, '12', 'TH-20', 'Chon Buri'),
        array(764, '13', 'TH-17', 'Sing Buri'),
        array(764, '14', 'TH-90', 'Songkhla'),
        array(764, '15', 'TH-44', 'Maha Sarakham'),
        array(764, '16', 'TH-19', 'Saraburi'),
        array(764, '17', 'TH-32', 'Surin'),
        array(764, '18', 'TH-21', 'Rayong'),
        array(764, '19', 'TH-33', 'Si Sa Ket'),
        array(764, '20', 'TH-40', 'Khon Kaen'),
        array(764, '21', 'TH-70', 'Ratchaburi'),
        array(764, '22', 'TH-96', 'Narathiwat'),
        array(764, '23', 'TH-45', 'Roi Et'),
        array(764, '24', 'TH-72', 'Suphan Buri'),
        array(764, '25', 'TH-80', 'Nakhon Si Thammarat'),
        array(764, '26', 'TH-31', 'Buri Ram'),
        array(764, '27', 'TH-93', 'Phatthalung'),
        array(764, '28', 'TH-46', 'Kalasin'),
        array(764, '29', 'TH-18', 'Chai Nat'),
        array(764, '30', 'TH-41', 'Udon Thani'),
        array(764, '31', 'TH-35', 'Yasothon'),
        array(764, '32', 'TH-39', 'Nong Bua Lam Phu'),
        array(764, '33', 'TH-48', 'Nakhon Phanom'),
        array(764, '34', 'TH-30', 'Nakhon Ratchasima'),
        array(764, '35', 'TH-92', 'Trang'),
        array(764, '36', 'TH-24', 'Chachoengsao'),
        array(764, '37', 'TH-43', 'Nong Khai'),
        array(764, '38', 'TH-66', 'Phichit'),
        array(764, '39', 'TH-16', 'Lop Buri'),
        array(764, '40', 'TH-26', 'Nakhon Nayok'),
        array(764, '41', 'TH-37', 'Amnat Charoen'),
        array(764, '42', 'TH-47', 'Sakon Nakhon'),
        array(764, '43', 'TH-91', 'Satun'),
        array(764, '44', 'TH-34', 'Ubon Ratchathani'),
        array(764, '45', 'TH-60', 'Nakhon Sawan'),
        array(764, '46', 'TH-57', 'Chiang Rai'),
        array(764, '47', 'TH-95', 'Yala'),
        array(764, '48', 'TH-25', 'Prachin Buri'),
        array(764, '49', 'TH-64', 'Sukhothai'),
        array(764, '50', 'TH-51', 'Lamphun'),
        array(764, '51', 'TH-36', 'Chaiyaphum'),
        array(764, '52', 'TH-81', 'Krabi'),
        array(764, '53', 'TH-62', 'Kamphaeng Phet'),
        array(764, '54', 'TH-50', 'Chiang Mai'),
        array(764, '55', 'TH-86', 'Chumphon'),
        array(764, '56', 'TH-22', 'Chanthaburi'),
        array(764, '57', 'TH-67', 'Phetchabun'),
        array(764, '58', 'TH-23', 'Trat'),
        array(764, '59', 'TH-65', 'Phitsanulok'),
        array(764, '60', 'TH-77', 'Prachuap Khiri Khan'),
        array(764, '61', 'TH-49', 'Mukdahan'),
        array(764, '62', 'TH-56', 'Phayao'),
        array(764, '63', 'TH-84', 'Surat Thani'),
        array(764, '64', 'TH-27', 'Sa Kaeo'),
        array(764, '65', 'TH-76', 'Phetchaburi'),
        array(764, '66', 'TH-54', 'Phrae'),
        array(764, '67', 'TH-52', 'Lampang'),
        array(764, '68', 'TH-53', 'Uttaradit'),
        array(764, '69', 'TH-82', 'Phangnga'),
        array(764, '70', 'TH-85', 'Ranong'),
        array(764, '71', 'TH-42', 'Loei'),
        array(764, '72', 'TH-61', 'Uthai Thani'),
        array(764, '73', 'TH-71', 'Kanchanaburi'),
        array(764, '74', 'TH-55', 'Nan'),
        array(764, '75', 'TH-63', 'Tak'),
        array(764, '76', 'TH-58', 'Mae Hong Son'),
      );

      foreach ($zones as $zone) {
        db_update('uc_zones')
          ->fields(array(
            'zone_name' => $zone[3],
            'zone_code' => $zone[2],
          ))
          ->condition('zone_country_id', $zone[0])
          ->condition('zone_code', $zone[1])
          ->execute();
      }

      break;
  }
}
