using AR;
using Microsoft.SqlServer.Server;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Project.Controller
{
    public class ModelController : ApiController
    {
        //private static List<Item> _values = new List<Item> {
        //    new Item { Id = 1,  Name = "USB2.0 Extension Cable" },
        //    new Item { Id = 2, Name = "USB3.0 Extension Cable" },
        //       new Item { Id =3, Name = "USB3.1 Extension Cable" }
        //};

        // GET api/values
        public IHttpActionResult Get()
        {
            HttpResponseMessage responsem = new HttpResponseMessage(HttpStatusCode.OK);
            var data = new
            {
                model = PUB.Result.vModel.Title,
            };
            return Ok(data);
        }

        // GET api/values/5
        public IHttpActionResult Get(int id)
        {
            //var value = DataManager.Items?.Find(v => v.Id == id) ?? null;
            //if (value == null)
            //{
            //    return NotFound();
            //}
            return Ok();
        }

        // POST api/values
        [HttpGet]
        public IHttpActionResult Set(string id)
        {
            //Change to specified model.
            var msg = "";

            if (id.isEmpty()) msg = "NO MODELNAME";
            if (msg.isEmpty())
            {
                if (PUB.sm.Step == eSMStep.IDLE || PUB.sm.Step == eSMStep.FINISH)
                {
                    var buf = id.Split(new char[] { '|' }, System.StringSplitOptions.RemoveEmptyEntries);
                    var modelname = buf[0];
                    var conv = true;
                    if (buf.Length == 2) conv = buf[1] == "1";

                    var modelVision = PUB.mdm.GetDataV(modelname);
                    if (modelVision == null)
                    {
                        msg = "NO MODELDATA";
                    }
                    else
                    {
                        VAR.BOOL[eVarBool.Use_Conveyor] = conv;
                        var rlt = PUB.SelectModelV(modelname, false);
                        if (rlt == false)
                        {
                            msg = "[OP]LOAD FAIL";
                        }
                        else
                        {
                            var motion = conv ? "Conveyor" : "Default";
                            rlt = PUB.SelectModelM("Conveyor");
                            if(rlt ==false)
                            {
                                msg = "[MT]LOAD FAIL";
                            }
                        }
                        
                        
                    }
                }
                else
                {
                    msg = "NOT IDLE";
                }
            }

            var data = new
            {
                Message = msg,
                Result = (msg.isEmpty() ? true : false),
            };
            return Ok(data);
        }

        // PUT api/values/5
        public IHttpActionResult Put(int id, [FromBody] JObject value)
        {
            return Ok();
        }

        // DELETE api/values/5
        public IHttpActionResult Delete(int id)
        {
            return Ok();
        }
    }
}
