 import TSMessages
 import MessageUI
 
+enum Sections: Int {
+	case sortingOptions = 0
+	case displayOptions
+	case otherOptions
+}
+
 class SettingsViewController: UITableViewController, UITableViewDelegate, MFMailComposeViewControllerDelegate {
 
     override func viewDidLoad() {

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		if section == 0 {
			return 4
		} else if section == 1 {
			return 2
		} else {
			return 6
		}
    }

	override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
		if section == 0 {
			return NSLocalizedString("SORTING_OPTIONS", comment: "Sort by")
		} else if section == 1 {
			return NSLocalizedString("DISPLAY_OPTIONS", comment: "Display")
		} else {
			return NSLocalizedString("OTHER_OPTIONS", comment: "Other")
		}
	}

	override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
		let cell: UITableViewCell = UITableViewCell()

		// /////////////////////////////////
		// Sorting Options
		// /////////////////////////////////
		let sortingType = NSUserDefaults.standardUserDefaults().stringForKey("SortingType")
		if indexPath.section == 0 {
			switch indexPath.row {
			case 0:
				cell.textLabel?.text = NSLocalizedString("SORTINGTYPE_DEFAULT", comment: "Default")
				if sortingType == "default" {
					cell.accessoryType = UITableViewCellAccessoryType.Checkmark
				}
			case 1:
				cell.textLabel?.text = NSLocalizedString("SORTINGTYPE_LOCATION", comment: "Distance")
				if sortingType == "distance" {
					cell.accessoryType = UITableViewCellAccessoryType.Checkmark
				}
			case 2:
				cell.textLabel?.text = NSLocalizedString("SORTINGTYPE_ALPHABETICAL", comment: "Alphabetical")
				if sortingType == "alphabetical" {
					cell.accessoryType = UITableViewCellAccessoryType.Checkmark
				}
			case 3:
				cell.textLabel?.text = NSLocalizedString("SORTINGTYPE_FREESPOTS", comment: "Free spots")
				if sortingType == "free" {
					cell.accessoryType = UITableViewCellAccessoryType.Checkmark
				}
			default:
				break
			}
		}

		// /////////////////////////////////
		// Display Options
		// /////////////////////////////////
		else if indexPath.section == 1 {
			switch indexPath.row {
			case 0:
				cell.textLabel?.text = NSLocalizedString("HIDE_NODATA_LOTS", comment: "Hide lots without data")
				let doHideLots = NSUserDefaults.standardUserDefaults().boolForKey("SkipNodataLots")
				if doHideLots {
					cell.accessoryType = UITableViewCellAccessoryType.Checkmark
				}
			case 1:
				cell.textLabel?.text = NSLocalizedString("USE_GRAYSCALE_COLORS", comment: "Use grayscale colors")
				let useGrayscale = NSUserDefaults.standardUserDefaults().boolForKey("grayscaleColors")
				if useGrayscale {
					cell.accessoryType = UITableViewCellAccessoryType.Checkmark
				}
			default:
				break
			}
		}

		// /////////////////////////////////
		// Other Options
		// /////////////////////////////////
		else if indexPath.section == 2 {
			switch indexPath.row {
			case 0:
				cell.textLabel?.text = NSLocalizedString("EXPERIMENTAL_PROGNOSIS", comment: "Experimental: Prognosis") 
				cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
			case 1:
				cell.textLabel?.text = NSLocalizedString("ABOUT_BUTTON", comment: "About")
				cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
			case 2:
				cell.textLabel?.text = NSLocalizedString("RESET_NOTIFICATIONS", comment: "Reset Notifications")
			case 3:
				cell.textLabel?.text = NSLocalizedString("SHARE_ON_TWITTER", comment: "Share on Twitter")
				cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
			case 4:
				cell.textLabel?.text = NSLocalizedString("SHARE_ON_FACEBOOK", comment: "Share on Facebook")
				cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
			case 5:
				cell.textLabel?.text = NSLocalizedString("SEND_FEEDBACK", comment: "Got Feedback?")
				cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
			default:
				break
			}
		}

		cell.textLabel?.font = UIFont(name: "AvenirNext-Regular", size: 16.0)

				if MFMailComposeViewController.canSendMail() {
					let mail = MFMailComposeViewController()
					mail.mailComposeDelegate = self

					let versionNumber: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
					mail.setSubject("[ParkenDD v\(versionNumber)] Feedback")
					mail.setToRecipients(["parkendd@kilian.io"])

					self.presentViewController(mail, animated: true, completion: nil)
				}
