﻿/* Copyright (C) 2011 MoSync AB

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/**
 * @file MoSyncCheckBox.cs
 * @author Rata Gabriela
 *
 * @brief This represents the CheckBox Widget implementation for the NativeUI
 *        component on Windows Phone 7, language C#
 *
 * @platform WP 7.1
 **/

using Microsoft.Phone.Controls;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Navigation;
using System;
using System.Text.RegularExpressions;
using System.Reflection;

namespace MoSync
{
	namespace NativeUI
	{
        /**
        * The CheckBox class defines the attributes and behavior of a check box widget
        */
		public class CheckBox : WidgetBaseWindowsPhone
		{
            /**
             * Native CheckBox object
             */
			protected System.Windows.Controls.CheckBox mCheckBox;

            /**
             * Constructor
             */
			public CheckBox()
			{
				mCheckBox = new System.Windows.Controls.CheckBox();
                View = mCheckBox;

				mCheckBox.Click += new RoutedEventHandler(
				delegate(Object from, RoutedEventArgs evt)
				{
						Memory eventData = new Memory(12);
						const int MAWidgetEventData_eventType = 0;
						const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_checked = 8;

						eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_CLICKED);
						eventData.WriteInt32(MAWidgetEventData_widgetHandle, mHandle);
                        eventData.WriteInt32(MAWidgetEventData_checked, mCheckBox.IsChecked.Value ? 1 : 0);

						mRuntime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
				});
			}

            /**
             * Property for setting and getting the state of the check box.
             * set: a String containing the values "true" or "false" must be provided.
             * get: returns the state of the checkbox as a String containg "true" or "false"
             */
			[MoSyncWidgetProperty(MoSync.Constants.MAW_CHECK_BOX_CHECKED)]
			public String Checked
			{
				get
				{
					return mCheckBox.IsChecked.ToString();
				}

				set
				{
					bool val;
					if(bool.TryParse(value, out val))
					{
						mCheckBox.IsChecked = val;
					}
                    else throw new InvalidPropertyValueException();
				}
			}

            #region Property validation methods

            /**
             * Validates a property based on the property name and property value.
             * @param propertyName The name of the property to be checked.
             * @param propertyValue The value of the property to be checked.
             * @returns true if the property is valid, false otherwise.
             */
            public new static bool ValidateProperty(string propertyName, string propertyValue)
            {
                bool isPropertyValid = WidgetBaseWindowsPhone.ValidateProperty(propertyName, propertyValue);

                if (propertyName.Equals("checked"))
                {
                    bool val;
                    if (!bool.TryParse(propertyValue, out val))
                    {
                        isPropertyValid = false;
                    }
                }

                return isPropertyValid;
            }

            #endregion
        } // end of CheckBox class
    } // end of NativeUI namespace
} // end of MoSync namespace
