using Domain;
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Appication

{
    public class EsbShipService : IShipService
    {
        private SimpleContext context ;
        
        public EsbShipService(SimpleContext context)
        {
            this.context = context;
        }

        public void DeleteShip(int id)
        {
            MyShip emp = context.MyShips.Find(id);

            context.MyShips.Remove(emp);
            context.SaveChanges();
        }

        public List<ShipResponModel> GetAllShip()
        {
            var emp = context.MyShips.Take(20).ToList();

            List<ShipResponModel> results = new List<ShipResponModel>();

            

            foreach (var e in emp)
            {
                results.Add(new ShipResponModel()
                {

                    ShipRegisterNoKey = e.ShipRegisterNoKey,
                    Tha_Name = e.Tha_Name,
                    Eng_Name = e.Eng_Name,
                    TonGross = e.TonGross,
                    ShipRegisterNo = e.ShipRegisterNo,
                    Use_type_key = e.Use_type_key,
                    Use_type_Name = e.Use_type_Name,
                    Use_bound_Name = e.Use_bound_Name,
                    Registerdate = e.Registerdate
                    
                    

                });
            }

            return results; 
        }


        public ShipResponModel GetShipByShipCode(string ShipRegisterNo)
        {
            var emp = context.MyShips.Where(e => e.Tha_Name == ShipRegisterNo).Single();


            return new ShipResponModel()
            {

                ShipRegisterNoKey = emp.ShipRegisterNoKey,
                Tha_Name = emp.Tha_Name,
                Eng_Name = emp.Eng_Name,
                TonGross = emp.TonGross,
                ShipRegisterNo = emp.ShipRegisterNo,
                Use_type_key = emp.Use_type_key,
                Use_type_Name = emp.Use_type_Name,
                Use_bound_Name = emp.Use_bound_Name,
                Registerdate = emp.Registerdate

            };
        }


        public List<ShipResponModel> GetShowDoc()
        {
            List<MyShip> emps = context.MyShips
                .Include(x => x.ShipCertificate)
                    .ThenInclude(x => x.Document)
                .Include(x => x.ShipFishVisits)
                .Take(20)
                .ToList();


            List<ShipResponModel> result = new List<ShipResponModel>();

            foreach (var ship in emps)
            {
                var e = new ShipResponModel()
                {
                    ShipRegisterNoKey = ship.ShipRegisterNoKey,
                    Tha_Name = ship.Tha_Name,
                    Eng_Name = ship.Eng_Name,
                    TonGross = ship.TonGross,
                    ShipRegisterNo = ship.ShipRegisterNo,
                    Use_type_key = ship.Use_type_key,
                    Use_type_Name = ship.Use_type_Name,
                    Use_bound_Name = ship.Use_bound_Name,
                    Registerdate = ship.Registerdate,
                    Document = ship.ShipCertificate.Select(x => new ShipDocument() { documentname = x.Document.DocumentName, documentnumber = x.certificateno }).Take(3).ToList(),
                    totalvisit = ship.ShipFishVisits.Count()



                };

                result.Add(e);
            }

            return result;
        }


        public void InsertShip(CreateShip x)
        {

            MyShip emp = new MyShip();

            
            emp.ShipRegisterNo = x.ShipRegisterNo;
            emp.Tha_Name = x.Tha_Name;
            emp.Eng_Name = x.Eng_Name;
            emp.Use_type_key = x.Use_type_key;
            emp.Use_type_Name = x.Use_type_Name;
            emp.Use_bound_Name = x.Use_bound_Name;
            emp.TonGross = x.TonGross;
            emp.TonNet = x.TonNet;
            emp.Loa = x.Loa;
            emp.Beam = x.Beam;
            emp.Depth = x.Depth;
            emp.Registerdate = x.Registerdate;
            emp.RegisterProviceCode = x.RegisterProviceCode;
            emp.Permit_ValidDate = x.Permit_ValidDate;
            emp.Permit_ExpireDate = x.Permit_ExpireDate;
            emp.PicShip1 = x.PicShip1;
            emp.PicShip2 = x.PicShip2;
            emp.PicShip3 = x.PicShip3;
            emp.visittype = x.visittype;
            emp.shipstatus = x.shipstatus;
            emp.totalseaman = x.totalseaman;
            emp.totallabour = x.totallabour;
            emp.totalcrewonboard = x.totalcrewonboard;
            emp.procurementtype = x.procurementtype;
            emp.approvestatus = x.approvestatus;
            emp.CreateDateTime = x.CreateDateTime;
            emp.CreateBy = x.CreateBy;
            emp.ModifyDateTime = x.ModifyDateTime;
            emp.ModifyBy = x.ModifyBy;
            emp.CallSign = x.CallSign;

            context.MyShips.Add(emp);
            context.SaveChanges();

            
        }

        public void UpShip(UpdateShipModel x)
        {

            MyShip emp = context.MyShips.Find(x.ShipRegisterNoKey);

            emp.ShipRegisterNo = x.ShipRegisterNo;
            emp.Tha_Name = x.Tha_Name;
            emp.Eng_Name = x.Eng_Name;
            emp.Use_type_key = x.Use_type_key;
            emp.Use_type_Name = x.Use_type_Name;
            emp.Use_bound_Name = x.Use_bound_Name;
            emp.TonGross = x.TonGross;
            emp.TonNet = x.TonNet;
            emp.Loa = x.Loa;
            emp.Beam = x.Beam;
            emp.Depth = x.Depth;
            emp.Registerdate = x.Registerdate;
            emp.RegisterProviceCode = x.RegisterProviceCode;
            emp.Permit_ValidDate = x.Permit_ValidDate;
            emp.Permit_ExpireDate = x.Permit_ExpireDate;
            emp.PicShip1 = x.PicShip1;
            emp.PicShip2 = x.PicShip2;
            emp.PicShip3 = x.PicShip3;
            emp.visittype = x.visittype;
            emp.shipstatus = x.shipstatus;
            emp.totalseaman = x.totalseaman;
            emp.totallabour = x.totallabour;
            emp.totalcrewonboard = x.totalcrewonboard;
            emp.procurementtype = x.procurementtype;
            emp.approvestatus = x.approvestatus;
            emp.CreateDateTime = x.CreateDateTime;
            emp.CreateBy = x.CreateBy;
            emp.ModifyDateTime = x.ModifyDateTime;
            emp.ModifyBy = x.ModifyBy;

            
            context.SaveChanges();
        }
    }
}
