<?php

function vietnam_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' => 704,
      'country_name' => 'Viet Nam',
      'country_iso_code_2' => 'VN',
      'country_iso_code_3' => 'VNM',
      'version' => 2,
    ))
    ->execute();

  $zones = array(
    array(704, 'AG', 'An Giang'),
    array(704, 'BV', 'Ba Ria-Vung Tau'),
    array(704, 'BK', 'Bac Can'),
    array(704, 'BG', 'Bac Giang'),
    array(704, 'BL', 'Bac Lieu'),
    array(704, 'BN', 'Bac Ninh'),
    array(704, 'BR', 'Ben Tre'),
    array(704, 'BD', 'Binh Dinh'),
    array(704, 'BI', 'Binh Duong'),
    array(704, 'BP', 'Binh Phuoc'),
    array(704, 'BU', 'Binh Thuan'),
    array(704, 'CM', 'Ca Mau'),
    array(704, 'CN', 'Can Tho'),
    array(704, 'CB', 'Cao Bang'),
    array(704, 'DA', 'Da Nang'),
    array(704, 'DC', 'Dac Lac'),
    array(704, 'DO', 'Dak Nong'),
    array(704, 'DB', 'Dien Bien'),
    array(704, 'DN', 'Dong Nai'),
    array(704, 'DT', 'Dong Thap'),
    array(704, 'GL', 'Gia Lai'),
    array(704, 'HG', 'Ha Giang'),
    array(704, 'HM', 'Ha Nam'),
    array(704, 'HA', 'Ha Tay'),
    array(704, 'HT', 'Ha Tinh'),
    array(704, 'HD', 'Hai Duong'),
    array(704, 'HP', 'Haiphong'),
    array(704, 'HN', 'Ha Noi'),
    array(704, 'HU', 'Hau Giang'),
    array(704, 'HC', 'Ho Chi Minh'),
    array(704, 'HO', 'Hoa Binh'),
    array(704, 'HY', 'Hung Yen'),
    array(704, 'KH', 'Khanh Hoa'),
    array(704, 'KG', 'Kien Giang'),
    array(704, 'KT', 'Kon Tum'),
    array(704, 'LI', 'Lai Chau'),
    array(704, 'LD', 'Lam Dong'),
    array(704, 'LS', 'Lang Son'),
    array(704, 'LO', 'Lao Cai'),
    array(704, 'LA', 'Long An'),
    array(704, 'ND', 'Nam Dinh'),
    array(704, 'NA', 'Nghe An'),
    array(704, 'NB', 'Ninh Binh'),
    array(704, 'NT', 'Ninh Thuan'),
    array(704, 'PT', 'Phu Tho'),
    array(704, 'PY', 'Phu Yen'),
    array(704, 'QB', 'Quang Binh'),
    array(704, 'QM', 'Quang Nam'),
    array(704, 'QG', 'Quang Ngai'),
    array(704, 'QN', 'Quang Ninh'),
    array(704, 'QT', 'Quang Tri'),
    array(704, 'ST', 'Soc Trang'),
    array(704, 'SL', 'Son La'),
    array(704, 'TN', 'Tay Ninh'),
    array(704, 'TB', 'Thai Binh'),
    array(704, 'TY', 'Thai Nguyen'),
    array(704, 'TH', 'Thanh Hoa'),
    array(704, 'TT', 'Thua Thien-Hue'),
    array(704, 'TG', 'Tien Giang'),
    array(704, 'TV', 'Tra Vinh'),
    array(704, 'TQ', 'Tuyen Quang'),
    array(704, 'VL', 'Vinh Long'),
    array(704, 'VC', 'Vinh Phuc'),
    array(704, 'YB', 'Yen Bai'),
  );

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

function vietnam_update($version) {
  switch ($version) {
    case 2:
      // Correct ISO-3166-1 country name
      db_update('uc_countries')
        ->fields(array('country_name' => 'Viet Nam'))
        ->condition('country_id', 704)
        ->execute();

      db_update('uc_zones')
        ->fields(array('zone_name' => 'Dak Nong'))
        ->condition('zone_country_id', 704)
        ->condition('zone_name', 'Dac Nong')
        ->execute();

      db_update('uc_zones')
        ->fields(array('zone_name' => 'Ha Noi'))
        ->condition('zone_country_id', 704)
        ->condition('zone_name', 'Hanoi')
        ->execute();

      break;
  }
}
