using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Media;

using System.Windows.Forms;

namespace AVS
{
    public partial class CartonForm : Form
    {
        public SoundPlayer soundPlayer = new SoundPlayer();
        public int ID;

        public CartonForm(string modelNO, int id)
        {
            InitializeComponent();
            this.ControlBox = false;
            txt_model_no.Text = modelNO;
            ID = id;
        }

        private void txt_barcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (txt_barcode.Text.Length > 0)
                {
                    txt_result.Text = txt_barcode.Text;


                    //Remove unique date from shipping mark
                    if (txt_barcode.Text.Contains(";"))
                    {
                        txt_barcode.Text = txt_barcode.Text.Split(';')[0].Trim();

                    }

                    if (txt_model_no.Text.ToUpper().TrimEnd() == txt_barcode.Text.ToUpper().TrimEnd())
                    {
                        lbl_status.ForeColor = Color.Green;
                        lbl_status.Text = "CARTON MATCHED";
                        panel1.Visible = false;
                        // Optionally hide the form without closing
                        HideFormOrReset();
                        // Set up a timer to close the form after 5 seconds
                        ////Timer timer = new Timer();
                        ////timer.Interval = 1000; // 5 seconds
                        ////timer.Tick += (s, args) =>
                        ////{
                        ////    timer.Stop(); // Stop the timer
                        ////    this.Close(); // Close the form
                        ////};
                        ////timer.Start();
                    }
                    else
                    {
                        string FinalValue = txt_result.Text.Trim().ToString();
                        LiteDbClass liteDbClass = new LiteDbClass(null);
                        liteDbClass.UpdateCartonValue(FinalValue, ID);


                        lbl_status.ForeColor = Color.Red;
                        lbl_status.Text = "WRONG CARTON";
                        showControls();
                       //PlayAlertSound();


                        this.Hide(); // Hide the MainForm
                        LoginForm loginForm = new LoginForm(this, false,null);
                        loginForm.Show();

                    }

                    txt_barcode.Text = "";
                }
            }
        }
        private void HideFormOrReset()
        {
            // Option 1: Hide the form if it’s a secondary form
            if (this != Application.OpenForms[0])
            {
                this.Hide();
            }
            else
            {
                // Option 2: Reset form fields without closing
                txt_barcode.Clear();
                txt_result.Clear();
                lbl_status.Text = "Ready for next scan";
                panel1.Visible = true;
            }
        }



        public void showControls()
        {
            panel1.Visible = true;
            
        }
      

        void PlayAlertSound()
        {
            string soundFilePath = @"C:\Users\Public\Documents\AVS\alert-alaram.wav";
            soundPlayer.SoundLocation = soundFilePath;
            soundPlayer.PlayLooping();
        }

        private void CartonForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (soundPlayer != null)
            {
                soundPlayer.Stop();
            }
        }
    }
}
