using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CowainHmi
{
    public partial class DlgPosEdit : Form
    {
        private string posAddr;
        private string posName;
        private int id;
        private int posId;
        private int unit;
        public DlgPosEdit(string posName, string posAddr, int id, int posId, int unit)
        {
            InitializeComponent();
            this.Text = posName;
            this.posAddr = posAddr;
            this.posName = posName;
            this.id = id;
            this.posId = posId;
            this.unit = unit;
        }

        private async void btnOk_Click(object sender, EventArgs e)
        {
            PCHMI.RT.SEND_INT32(0, $"{posAddr}.{id * 384 + posId * 12}", (int)this.txtTargetPos.Value);
            PCHMI.RT.SEND_INT32(0, $"{posAddr}.{id * 384 + posId * 12 + 8}", (int)this.txtTargetVel.Value);
            await Task.Delay(300);
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private async void DlgPosEdit_Load(object sender, EventArgs e)
        {
            this.lblUnit.Value = (ulong)unit;
            this.lblUnit2.Value = (ulong)unit;
            PCHMI.VL.GET_INT32(0, $"{posAddr}.{id * 384 + posId * 12}");
            PCHMI.VL.GET_INT32(0, $"{posAddr}.{id * 384 + posId * 12 + 8}");
            await Task.Delay(300);
            var pos = PCHMI.VL.GET_INT32(0, $"{posAddr}.{id * 384 + posId * 12}");
            var vel = PCHMI.VL.GET_INT32(0, $"{posAddr}.{id * 384 + posId * 12 + 8}");
            this.txtTargetPos.Value = (ulong)pos;
            this.txtTargetVel.Value = (ulong)vel;
        }
    }
}
