using MaterialSkin;
using MaterialSkin.Controls;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.Common.Services;
using VisionBuilder.UI.Common.ViewModel;

namespace VisionBuilder.UI.Windows.Duo
{
    public partial class Form1 : MaterialForm
    {
        private readonly MainWindowVM _mainWindowVm;
        private readonly IPasswordInputService _passwordInputService;
        private readonly UIConfiguration _uiConfiguration;
        private readonly WindowsUISettings _windowsUiSettings;

        public Form1(MainWindowVM mainWindowVm, IPasswordInputService passwordInputService, UIConfiguration uiConfiguration, WindowsUISettings windowsUiSettings)
        {
            
            _mainWindowVm = mainWindowVm;
            _passwordInputService = passwordInputService;
            _uiConfiguration = uiConfiguration;
            _windowsUiSettings = windowsUiSettings;
            MaterialSkin.MaterialSkinManager.ConfigureForInspectron();
            InitializeComponent();
            singleCameraControl1.SetViewModel(mainWindowVm.SingleCameraVms[0]);
            singleCameraControl2.SetViewModel(mainWindowVm.SingleCameraVms[1]);
            Load += Form1_Load;
            _mainWindowVm.PropertyChanged += _mainWindowVm_PropertyChanged;
            lblVersion.Text = $"v{Application.ProductVersion.Split('+')[0]}";
            if(!_uiConfiguration.ShowTestModeButton)
                flowLayoutPanel1.Controls.Remove(btnTestMode);

        }

        private void _mainWindowVm_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(_mainWindowVm.TestMode))
            {
                if (_mainWindowVm.TestMode)
                {
                    MaterialSkinManager.Instance.ColorScheme = new ColorScheme(Primary.Green800, Primary.Green800,
                        Primary.Green400, Accent.Green200, TextShade.WHITE);
                }
                else
                {
                    MaterialSkinManager.ConfigureForInspectron();
                }
                Refresh();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _mainWindowVm.OnProgramStarted();
            BringToFront();
            if (_windowsUiSettings.FullScreen)
            {
                WindowState = FormWindowState.Maximized;
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            flowLayoutPanel1.Left = (ClientSize.Width - flowLayoutPanel1.Width) / 2;


        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            _mainWindowVm.OnProgramClosed();
            Environment.Exit(0);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            _mainWindowVm.OnProgramClosed();
            Environment.Exit(0);
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private async void btnSettings_Click(object sender, EventArgs e)
        {
            if (_uiConfiguration.ProtectSettingsWithPassword)
            {
                var passwordOk = await _passwordInputService.GetPasswordAsync() == _uiConfiguration.AdminPassword;
                if (!passwordOk)
                    return;
            }
            _mainWindowVm.SettingsCommand.Execute(null);
        }

        private void btnTestMode_Click(object sender, EventArgs e)
        {
            _mainWindowVm.ToggleTestModeCommand.Execute(null);
        }

        public object GetHandle()
        {
            return this;
        }
    }
}
