using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SolarPower.Models;
using SolarPower.Models.SensorType;
using SolarPower.Repository.Interface;
using Dapper;
using SolarPower.Helper;
using System.Data;

namespace SolarPower.Repository.Implement
{
    public class SensorTypeRepository : RepositoryBase<SensorType>, ISensorTypeRepository
    {
        public SensorTypeRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
        {
            tableName = "sensor_type";
        }
        public async Task<List<SensorType>> GetAllSensorAsync()
        {
            List<SensorType> result;
            using (IDbConnection conn = this._databaseHelper.GetConnection())
            {
                try//tableName
                {
                    var sql = $"SELECT * FROM {tableName} WHERE deleted = 0";

                    result = (await conn.QueryAsync<SensorType>(sql)).ToList();
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                return result;
            }
        }

        /// <summary>
        /// 新增裝置資料
        /// </summary>
        /// <param name="SensorInfo"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public async Task AddSensor(SensorType sensorInfo, List<string> properties)
        {
            using (IDbConnection conn = _databaseHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var sql = GenerateInsertQueryWithCustomTable(properties, "sensor_type");
                        await conn.ExecuteAsync(sql, sensorInfo, trans);
                        trans.Commit();
                    }
                    catch (Exception exception)
                    {
                        trans.Rollback();
                        throw exception;
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }

        }

        /// <summary>
        /// 透過sensorEName，取得單一筆資料
        /// </summary>
        /// <param name="sensorEName"></param>
        /// <returns></returns>
        public async Task<SensorType> GetOneByEnglishNameAsync(string sensorEName)
        {
            SensorType result;
            using (IDbConnection conn = _databaseHelper.GetConnection())
            {
                try
                {
                    var sql = $"SELECT * FROM  {tableName}  WHERE deleted = 0 AND sensorEName = @SensorEName";

                    result = await conn.QueryFirstOrDefaultAsync<SensorType>(sql, new { SensorEName = sensorEName });
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                return result;
            }
        }

        /// <summary>
        /// 修改設備資料
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task UpdateSensorInfo(SensorType entity, List<string> properties)
        {
            GetProperties = typeof(SensorType).GetProperties();
            using (IDbConnection conn = _databaseHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var sql = GenerateUpdateQuery(properties);

                        await conn.ExecuteAsync(sql, entity, trans);

                        trans.Commit();
                    }
                    catch (Exception exception)
                    {
                        trans.Rollback();
                        throw exception;
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 取得設備類型 透過id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SensorType> GetOneSensorType(int id)
        {
            SensorType result;
            using (IDbConnection conn = this._databaseHelper.GetConnection())
            {
                conn.Open();
                try
                {
                    var sql = @$"SELECT * FROM  {tableName}  WHERE Deleted = 0 AND Id = @Id";

                    result = await conn.QueryFirstOrDefaultAsync<SensorType>(sql, new { Id = id });
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    conn.Close();
                }
                return result;
            }
        }

    }
}
