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

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using DAL;
using SQLite;
using System.IO;

namespace DistroList
{
    [Activity(Label = "AddDistroActivity", LaunchMode = Android.Content.PM.LaunchMode.SingleTask, ParentActivity = typeof(MainActivity))]
    public class AddDistroActivity : Activity
    {
        private string dbPath;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.AddDistro);

            dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "distros.db");

            var statusLbl = FindViewById<TextView>(Resource.Id.statusLbl);
            var addBtn = FindViewById<Button>(Resource.Id.addDistroBtn);

            if (savedInstanceState == null)
                statusLbl.Text = "";
            else
                statusLbl.Text = savedInstanceState.GetString("status");

            addBtn.Click += AddDistroButtonClick;
        }

        private void AddDistroButtonClick(object sender, EventArgs e)
        {
            var statusLbl = FindViewById<TextView>(Resource.Id.statusLbl);
            var nameEditText = FindViewById<EditText>(Resource.Id.distroNameTxtBox);
            var websiteEditText = FindViewById<EditText>(Resource.Id.distroWebsiteTxtBox);
            var fixedBox = FindViewById<CheckBox>(Resource.Id.fixedCheckBox);
            var rollingBox = FindViewById<CheckBox>(Resource.Id.rollingCheckBox);
            byte releaseModel = 0;

            if (string.IsNullOrWhiteSpace(nameEditText.Text))
            {
                statusLbl.Text = "You must enter a name for the distribution";
            }
            else if (string.IsNullOrWhiteSpace(websiteEditText.Text))
            {
                statusLbl.Text = "You must enter a website for the distribution";
            }
            else if(!Regex.IsMatch(websiteEditText.Text.Trim(), @"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,7})([\/\w \.-]*)*\/?[^\.]$", RegexOptions.IgnoreCase))
            {
                statusLbl.Text = "The website appears to be in an incorrect format";
            }
            else if (!fixedBox.Checked && !rollingBox.Checked)
            {
                statusLbl.Text = "You must select at least one release model";
            }
            else
            {
                if (fixedBox.Checked)
                    releaseModel += (byte)Distribution.ReleaseModels.Fixed;

                if (rollingBox.Checked)
                    releaseModel += (byte)Distribution.ReleaseModels.Rolling;

                var dist = new Distribution
                {
                    Name = nameEditText.Text.Trim(),
                    Website = websiteEditText.Text.Trim(),
                    ReleaseModel = releaseModel
                };

                using(var db = new SQLiteConnection(dbPath))
                {
                    var distCheck = db.Table<Distribution>().ToList().Where(d => Regex.IsMatch(d.Name, dist.Name, RegexOptions.IgnoreCase)).FirstOrDefault();

                    if(distCheck == null)
                    {
                        db.Insert(dist);
                        statusLbl.Text = "Successfully Added New Distribution";
                    }
                    else
                    {
                        distCheck.ReleaseModel = dist.ReleaseModel;
                        distCheck.Website = dist.Website;
                        db.Update(distCheck);
                        statusLbl.Text = "Successfully Updated Distribution";
                    }
                }
            }
        }

        protected override void OnSaveInstanceState(Bundle outState)
        {
            var statusLbl = FindViewById<TextView>(Resource.Id.statusLbl);
            outState.PutString("status", statusLbl.Text);
            base.OnSaveInstanceState(outState);            
        }
    }
}