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;
        MODELLog log = new MODELLog();
        SKULog skulog;
        DailyLog dailylog;
        LiteDbClass liteDbClass = new LiteDbClass(null);
        public CartonForm(string modelNO, int id, DailyLog dailylog, SKULog skulog)
        {
            InitializeComponent();
            this.ControlBox = false;
            txt_model_no.Text = modelNO;
            ID = id;
            this.skulog = skulog;
            this.dailylog = dailylog;
        }

        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(";"))
                    {
                        var parts = txt_barcode.Text.Split(';');
                        txt_barcode.Text = parts[0].Trim();
                        log.QR = parts.Length > 1 ? parts[1].Trim() : string.Empty;
                    }
                    else
                    {
                        log.QR = txt_barcode.Text.ToUpper().TrimEnd();
                    }

                    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();


                        // Create carton entry
                        string formattedDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                       
                        log.MODEL_NO = txt_model_no.Text.ToUpper().TrimEnd();
                        log.SYS_IP = dailylog.SYS_IP;
                        log.RECORD_DATE = DateTime.Parse(formattedDate);
                        log.USER_ID = skulog.USER_ID;
                        log.LIVE_WEIGHT =  Convert.ToDecimal(skulog.ItemsPerBox) * Convert.ToDecimal(skulog.NetWeightKg);
                        log.PC_NAME = dailylog.PC_NAME;
                        log.MARKET_PLACE = skulog.MARKET_PLACE;
                        log.USER_ID = dailylog.USER_ID;

                        liteDbClass.InsertRecord("MODELLogs", log, "CONTAINER_NO", "MODEL_NO");
                    }
                    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, "SHIPPING");
                        loginForm.StartPosition = FormStartPosition.CenterParent; // optional: center over parent
                        loginForm.ShowDialog(this); // ✅ Blocks current form and stays on top

                    }

                    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();
            }
        }
    }
}
