using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using AllContent_Client;

namespace AndroidContent.Views
{
    [Activity(Label = "AuthorizationActivity"/*, MainLauncher = true*/)]
    public class AuthorizationActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Authorizationlayout);
            var enter = FindViewById<Button>(Resource.Id.Enter);
            enter.Click += EnterClick;
            var reg = FindViewById<TextView>(Resource.Id.Registration);
            reg.Click += RegClick;
        }
        void RegClick(object sender, EventArgs e)
        {
            var reg = new Intent(this, typeof(RegistrationActivity));
            StartActivity(reg);
        }
        void EnterClick(object sender, EventArgs e)
        {
            string log = FindViewById<EditText>(Resource.Id.Login).Text;
            string passw = FindViewById<EditText>(Resource.Id.Password).Text;
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog error_dialog = builder.Create();
            try
            {
                if (User.MainUser.Authorization(log, passw))
                {
                    //MainView.user = this.user;
                    var main = new Intent(this, typeof(MainActivity));
                    StartActivity(main);
                }
                else
                {
                    error_dialog.SetTitle("Íå ïðàâèëüíûé ëîãèí èëè ïàðîëü");
                    error_dialog.SetMessage("Ïðîâåðüòå äàííûå è ïîâòîðèòå ïîïûòêó");
                    error_dialog.Show();
                }
            }
            catch (MySql.Data.MySqlClient.MySqlException exp)
            {
                if (exp.Number == 1024)
                {
                    error_dialog.SetTitle("Îøèáêà ïîäêëþ÷åíèÿ");
                    error_dialog.SetMessage("Ñåðâåð âðåìåííî íåäîñòóïåí");
                    error_dialog.Show();
                }                    
            }
        }
    }
}