using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CafeTerrace.Models;
using CafeTerrace.Models.DataBase;
using CafeTerrace.Services;
using CafeTerrace.Utilities;
using RestSharp;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace CafeTerrace.Views.DialogBox
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class JobDialogBox : ContentView
    {
        public event EventHandler JobSelected;
        private Frame selectedFrame = null;
        List<Job> innerJobsList=new List<Job>
        {
            new Job
            {
                Name = "مهندسی"
            },
            new Job
            {
                Name = "پزشکی"
            },
            new Job
            {
                Name = "فرهنگی"
            },
            new Job
            {
                Name = "حقوقی"
            },
            new Job
            {
                Name = "نظامی"
            },
            new Job
            {
                Name = "کارمند"
            },
            new Job
            {
                Name = "دیگر"
            },
        };
        public JobDialogBox()
        {
            InitializeComponent();
        }
        public JobDialogBox(List<Job> jobs)
        {
            InitializeComponent();
            foreach (var job in jobs)
            {
                if(job.Name.Contains("پزشک") || job.Name.Contains("پرستار") || job.Name.Contains("داروساز")|| job.Name.Contains("ماما"))
                    innerJobsList[1].ChildJobs.Add(job);
                else if(job.Name.Contains("مکانیک")|| job.Name.Contains("برق") || job.Name.Contains("معماری") || job.Name.Contains("عمران") || job.Name.Contains("پتروشیمی"))
                    innerJobsList[0].ChildJobs.Add(job);
                else if (job.Name.Contains("وکیل") || job.Name.Contains("قاضی"))
                    innerJobsList[3].ChildJobs.Add(job);
                else if (job.Name.Contains("استاد") || job.Name.Contains("معلم") || job.Name.Contains("دانشجو") || job.Name.Contains("دانش"))
                    innerJobsList[0].ChildJobs.Add(job);
                else if (job.Name.Contains("سپاهی") || job.Name.Contains("ارتشی") || job.Name.Contains("نیرو"))
                    innerJobsList[4].ChildJobs.Add(job);
                else if (job.Name.Contains("کارمند"))
                    innerJobsList[5].ChildJobs.Add(job);
                else
                    innerJobsList[6].ChildJobs.Add(job);
            }

            BindingContext = innerJobsList;
        }

        private void JobItem_OnTapped(object sender, EventArgs e)
        {
            Frame selected = sender as Frame;
            Label selectedLabel = selected.Content as Label;
            selected.BackgroundColor = (Color) Application.Current.Resources["PrimaryColor"];
            selectedLabel.TextColor = Color.Accent;
            selectedLabel.FontSize = 16;
            if (selectedFrame != null)
            {
                Label pastLabel = selectedFrame.Content as Label;
                selectedFrame.BackgroundColor = Color.White;
                pastLabel.TextColor = Color.Accent;
                pastLabel.FontSize = 12;
            }
            selectedFrame = selected;
        }

        private async void SubmitButton_OnClicked(object sender, EventArgs e)
        {
            JobSelected?.Invoke((selectedFrame.BindingContext as Job),e);
        }
    }
}