using BoredCareers.Entities;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;

namespace BoredCareers.Services.DatabaseService {
    public partial class DatabaseService {

        string GetString(DbDataReader reader, string name) {
            return reader.IsDBNull(reader.GetOrdinal(name)) ? "" : reader.GetString(name);
        }

        public async Task<Resume?> GetResumeHeader(int ResumeID) {
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                MySqlCommand cmd = new MySqlCommand("SELECT * FROM Resume WHERE ID = @ID;", connection);
                cmd.Parameters.AddWithValue("@ID", ResumeID);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _accountid = reader.GetInt32("AccountID");
                        string _title = GetString(reader, "Title");
                        string _name = GetString(reader, "Name");
                        string _field = GetString(reader, "Field");
                        string _email = GetString(reader, "Email");
                        string _phonenumber = GetString(reader, "PhoneNumber");
                        string _postalcode = GetString(reader, "PostalCode");
                        string _country = GetString(reader, "Country");
                        string _state = GetString(reader, "StateOrRegion");
                        string _city = GetString(reader, "City");
                        bool _isactive = reader.GetBoolean("IsActive");
                        return new Resume() {
                            ID = _id,
                            AccountID = _accountid,
                            Title = _title,
                            Name = _name,
                            Field = _field,
                            Email = _email,
                            PhoneNumber = _phonenumber,
                            PostalCode = _postalcode,
                            Country = _country,
                            StateOrRegion = _state,
                            City = _city,
                            IsActive = _isactive
                        };
                    }
                }
            }
            return null;
        }

        public async Task<ResumeExperience[]> GetResumeExperience(int resumeId) {
            List<ResumeExperience> experience = new List<ResumeExperience>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeExperience WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _jobtitle = GetString(reader, "JobTitle");
                        string _company = GetString(reader, "Company");
                        string _postalcode = GetString(reader, "PostalCode");
                        string _country = GetString(reader, "Country");
                        string _state = GetString(reader, "StateOrRegion");
                        string _city = GetString(reader, "City");
                        DateTime _datestarted = reader.GetDateTime("DateStarted");
                        bool _stillempoyed = reader.GetBoolean("StillEmployed");
                        DateTime _dateended = reader.GetDateTime("DateEnded");
                        experience.Add(new ResumeExperience() {
                            ID = _id,
                            ResumeID = _resumeid,
                            JobTitle = _jobtitle,
                            Company = _company,
                            PostalCode = _postalcode,
                            Country = _country,
                            StateOrRegion = _state,
                            City = _city,
                            DateStarted = _datestarted,
                            StillEmployed = _stillempoyed,
                            DateEnded = _dateended,
                        });
                    }
                }
            }
            return experience.ToArray();
        }

        public async Task<ResumeExperienceBullet[]> GetResumeExperienceBullets(int resumeId) {
            List<ResumeExperienceBullet> experienceBullets = new List<ResumeExperienceBullet>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeExperienceBullet WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        int _experienceid = reader.GetInt32("ResumeExperienceID");
                        string _jobfunction = GetString(reader, "JobFunction");
                        experienceBullets.Add(new ResumeExperienceBullet() {
                            ID = _id,
                            ResumeID = _resumeid,
                            ResumeExperienceID = _experienceid,
                            JobFunction = _jobfunction
                        });
                    }
                }
            }
            return experienceBullets.ToArray();
        }

        public async Task<ResumeMilitaryBullet[]> GetResumeMilitaryBullets(int resumeId) {
            List<ResumeMilitaryBullet> militaryBullets = new List<ResumeMilitaryBullet>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeMilitaryBullet WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        int _experienceid = reader.GetInt32("ResumeMilitaryID");
                        string _achievement = GetString(reader, "Achievement");
                        string _description = GetString(reader, "Description");
                        militaryBullets.Add(new ResumeMilitaryBullet() {
                            ID = _id,
                            ResumeID = _resumeid,
                            ResumeMilitaryID = _experienceid,
                            Achievement = _achievement,
                            Description = _description
                        });
                    }
                }
            }
            return militaryBullets.ToArray();
        }

        public async Task<ResumeMilitary?> GetResumeMilitary(int resumeId) {
            ResumeMilitary? military = null;
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeMilitary WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _country = GetString(reader, "Country");
                        string _rank = GetString(reader, "Rank");
                        DateTime _datestarted = reader.GetDateTime("DateStarted");
                        bool _stillServing = reader.GetBoolean("StillServing");
                        DateTime _dateended = reader.GetDateTime("DateEnded");
                        military = new ResumeMilitary() {
                            ID = _id,
                            ResumeID = _resumeid,
                            Country = _country,
                            Rank = _rank,
                            DateStarted = _datestarted,
                            StillServing = _stillServing,
                            DateEnded = _dateended,
                        };
                    }
                }
            }
            return military;
        }

        public async Task<ResumeEducation[]> GetResumeEducation(int resumeId) {
            List<ResumeEducation>? education = new List<ResumeEducation>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeEducation WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _degreetype = GetString(reader, "DegreeType");
                        string _degreefield = GetString(reader, "DegreeField");
                        string _school = GetString(reader, "School");
                        string _postalcode = GetString(reader, "PostalCode");
                        string _country = GetString(reader, "Country");
                        string _state = GetString(reader, "StateOrRegion");
                        string _city = GetString(reader, "City");
                        DateTime _datestarted = reader.GetDateTime("DateStarted");
                        bool _stillstudying = reader.GetBoolean("StillStudying");
                        DateTime _dateended = reader.GetDateTime("DateEnded");
                        education.Add(new ResumeEducation {
                            ID = _id,
                            ResumeID = _resumeid,
                            DegreeType = _degreetype,
                            DegreeField = _degreefield,
                            School = _school,
                            PostalCode = _postalcode,
                            Country = _country,
                            StateOrRegion = _state,
                            City = _city,
                            DateStarted = _datestarted,
                            StillStudying = _stillstudying,
                            DateEnded = _dateended
                        });
                    }
                }
            }
            return education.ToArray();
        }

        public async Task<ResumeSkill[]> GetResumeSkills(int resumeId) {
            List<ResumeSkill> skills = new List<ResumeSkill>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeSkill WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _name = GetString(reader, "Name");
                        string _description = GetString(reader, "Description");
                        skills.Add(new ResumeSkill {
                            ID = _id,
                            ResumeID = _resumeid,
                            Name = _name,
                            Description = _description
                        });
                    }
                }
            }
            return skills.ToArray();
        }

        public async Task<ResumeLanguage[]> GetResumeLanguages(int resumeId) {
            List<ResumeLanguage>? languages = new List<ResumeLanguage>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeLanguage WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _language = GetString(reader, "Language");
                        string _proficiency = GetString(reader, "Proficiency");
                        languages.Add(new ResumeLanguage {
                            ID = _id,
                            ResumeID = _resumeid,
                            Language = _language,
                            Proficiency = _proficiency
                        });
                    }
                }
            }
            return languages.ToArray();
        }

        public async Task<ResumeCertification[]> GetResumeCertification(int resumeId) {
            List<ResumeCertification> certs = new List<ResumeCertification>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeCertification WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _name = GetString(reader, "Name");
                        string _url = GetString(reader, "VerificationURL");
                        string _description = GetString(reader, "Description");
                        certs.Add(new ResumeCertification {
                            ID = _id,
                            ResumeID = _resumeid,
                            Name = _name,
                            VerificationURL = _url,
                            Description = _description
                        });
                    }
                }
            }
            return certs.ToArray();
        }

        public async Task<ResumeProject[]> GetResumeProjects(int resumeId) {
            List<ResumeProject>? projects = new List<ResumeProject>();
            using (var conn = GetConnection()) {
                await conn.OpenAsync();
                var cmd = new MySqlCommand("SELECT * FROM ResumeProject WHERE ResumeID = @ResumeID;", conn);
                cmd.Parameters.AddWithValue("@ResumeID", resumeId);
                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        string _name = GetString(reader, "Name");
                        string _url = GetString(reader, "URL");
                        string _description = GetString(reader, "Description");
                        projects.Add(new ResumeProject {
                            ID = _id,
                            ResumeID = _resumeid,
                            Name = _name,
                            URL = _url,
                            Description = _description
                        });
                    }
                }
            }
            return projects.ToArray();
        }
    }
}
