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

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

        public async Task SetResume(MySqlConnection connection, Resume resume) {
            string command = @"
            INSERT INTO Resume
            (ID,AccountID,Name,Field,Email,PhoneNumber,PostalCode,Country,StateOrRegion,City,IsActive)
            VALUES
            (@ID,@AccountID,@Name,@Field,@Email,@PhoneNumber,@PostalCode,@Country,@StateOrRegion,@City,@IsActive)
            ON DUPLICATE KEY UPDATE
            AccountID = @AccountID,
            Name = @Name,
            Field = @Field,
            Email = @Email,
            PhoneNumber = @PhoneNumber,
            PostalCode = @PostalCode,
            Country = @Country,
            StateOrRegion = @StateOrRegion,
            City = @City,
            IsActive = @IsActive;
            ";

            MySqlCommand cmd = new MySqlCommand(command, connection);
            cmd.Parameters.AddWithValue("@ID", resume.ID);
            cmd.Parameters.AddWithValue("@AccountID", resume.AccountID);
            cmd.Parameters.AddWithValue("@Name", resume.Name);
            cmd.Parameters.AddWithValue("@Field", resume.Field);
            cmd.Parameters.AddWithValue("@Email", resume.Email);
            cmd.Parameters.AddWithValue("@PhoneNumber", resume.PhoneNumber);
            cmd.Parameters.AddWithValue("@PostalCode", resume.PostalCode);
            cmd.Parameters.AddWithValue("@Country", resume.Country);
            cmd.Parameters.AddWithValue("@StateOrRegion", resume.StateOrRegion);
            cmd.Parameters.AddWithValue("@City", resume.City);
            cmd.Parameters.AddWithValue("@IsActive", resume.IsActive);

            await cmd.ExecuteNonQueryAsync();
        }

        public async Task SetResumeExperienceBullets(MySqlConnection connection, ResumeExperienceBullet[] bullets) {
            foreach (ResumeExperienceBullet cur in bullets) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,ResumeExperienceID,JobFunction)
                VALUES
                (@ID,@ResumeID,@ResumeExperienceID,@JobFunction)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                ResumeExperienceID = @ResumeExperienceID,
                JobFunction = @JobFunction;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@ResumeExperienceID", cur.ResumeExperienceID);
                cmd.Parameters.AddWithValue("@JobFunction", cur.JobFunction);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeExperience(MySqlConnection connection, ResumeExperience[] experiences) {
            foreach (ResumeExperience cur in experiences) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,JobTitle,Company,PostalCode,Country,StateOrRegion,City,DateStarted,StillEmployed,DateEnded)
                VALUES
                (@ID,@ResumeID,@JobTitle,@Company,@PostalCode,@Country,@StateOrRegion,@City,@DateStarted,@StillEmployed,@DateEnded)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                JobTitle = @JobTitle,
                Company = @Company,
                PostalCode = @PostalCode,
                Country = @Country,
                StateOrRegion = @StateOrRegion,
                City = @City,
                DateStarted = @DateStarted,
                StillEmployed = @StillEmployed,
                DateEnded = @DateEnded;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@JobTitle", cur.JobTitle);
                cmd.Parameters.AddWithValue("@Company", cur.Company);
                cmd.Parameters.AddWithValue("@PostalCode", cur.PostalCode);
                cmd.Parameters.AddWithValue("@DateStarted", cur.DateStarted.ToUniversalTime());
                cmd.Parameters.AddWithValue("@StillEmployed", cur.StillEmployed);
                cmd.Parameters.AddWithValue("@DateEnded", cur.DateEnded.ToUniversalTime());

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeMilitaryBullets(MySqlConnection connection, ResumeMilitaryBullet[] bullets) {
            foreach (ResumeMilitaryBullet cur in bullets) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,ResumeMilitaryID,Achievement)
                VALUES
                (@ID,@ResumeID,@ResumeMilitaryID,@Achievement)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                ResumeMilitaryID = @ResumeMilitaryID,
                Achievement = @Achievement,
                Description = @Description;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@ResumeMilitaryID", cur.ResumeMilitaryID);
                cmd.Parameters.AddWithValue("@Achievement", cur.Achievement);
                cmd.Parameters.AddWithValue("@Description", cur.Description);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeMilitary(MySqlConnection connection, ResumeMilitary military) {
            string command = @"
            INSERT INTO Resume
            (ID,ResumeID,Country,Rank,DateStarted,StillServing,DateEnded)
            VALUES
            (@ID,@ResumeID,@Country,@Rank,@DateStarted,@StillServing,@DateEnded)
            ON DUPLICATE KEY UPDATE
            ResumeID = @ResumeID,
            Country = @Country,
            Rank = @Rank,
            DateStarted = @DateStarted,
            StillServing = @StillServing,
            DateEnded = @DateEnded;
            ";

            MySqlCommand cmd = new MySqlCommand(command, connection);
            cmd.Parameters.AddWithValue("@ID", military.ID);
            cmd.Parameters.AddWithValue("@ResumeID", military.ResumeID);
            cmd.Parameters.AddWithValue("@Country", military.Country);
            cmd.Parameters.AddWithValue("@Rank", military.Rank);
            cmd.Parameters.AddWithValue("@DateStarted", military.DateStarted.ToUniversalTime());
            cmd.Parameters.AddWithValue("@StillServing", military.StillServing);
            cmd.Parameters.AddWithValue("@DateEnded", military.DateEnded.ToUniversalTime());

            await cmd.ExecuteNonQueryAsync();
        }

        public async Task SetResumeEducation(MySqlConnection connection, ResumeEducation[] educations) {
            foreach (ResumeEducation cur in educations) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,DegreeType,DegreeField,School,PostalCode,Country,StateOrRegion,City,DateStarted,StillStudying,DateEnded)
                VALUES
                (@ID,@ResumeID,@DegreeType,@DegreeField,@School,@PostalCode,@Country,@StateOrRegion,@City,@DateStarted,@StillStudying,@DateEnded)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                DegreeType = @DegreeType,
                DegreeField = @DegreeField,
                School = @School,
                PostalCode = @PostalCode,
                Country = @Country,
                StateOrRegion = @StateOrRegion,
                City = @City,
                DateStarted = @DateStarted,
                StillStudying = @StillStudying,
                DateEnded = @DateEnded;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@DegreeType", cur.DegreeType);
                cmd.Parameters.AddWithValue("@DegreeField", cur.DegreeField);
                cmd.Parameters.AddWithValue("@School", cur.School);
                cmd.Parameters.AddWithValue("@PostalCode", cur.PostalCode);
                cmd.Parameters.AddWithValue("@Country", cur.Country);
                cmd.Parameters.AddWithValue("@StateOrRegion", cur.StateOrRegion);
                cmd.Parameters.AddWithValue("@City", cur.City);
                cmd.Parameters.AddWithValue("@DateStarted", cur.DateStarted.ToUniversalTime());
                cmd.Parameters.AddWithValue("@StillStudying", cur.StillStudying);
                cmd.Parameters.AddWithValue("@DateEnded", cur.DateEnded.ToUniversalTime());

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeSkills(MySqlConnection connection, ResumeSkill[] skills) {
            foreach (ResumeSkill cur in skills) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,Name,Description)
                VALUES
                (@ID,@ResumeID,@Name,@Description)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                Name = @Name,
                Description = @Description;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@Name", cur.Name);
                cmd.Parameters.AddWithValue("@Description", cur.Description);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeLanguages(MySqlConnection connection, ResumeLanguage[] languages) {
            foreach (ResumeLanguage cur in languages) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,Language,Proficiency)
                VALUES
                (@ID,@ResumeID,@Language,@Proficiency)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                Language = @Language,
                Proficiency = @Proficiency;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@Language", cur.Language);
                cmd.Parameters.AddWithValue("@Proficiency", cur.Proficiency);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeCertification(MySqlConnection connection, ResumeCertification[] certifications) {
            foreach (ResumeCertification cur in certifications) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,Name,VerificationURL,Description)
                VALUES
                (@ID,@ResumeID,@Name,@VerificationURL,@Description)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                Name = @DegreeNameType,
                VerificationURL = @VerificationURL,
                Description = @Description;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@Name", cur.Name);
                cmd.Parameters.AddWithValue("@VerificationURL", cur.VerificationURL);
                cmd.Parameters.AddWithValue("@Description", cur.Description);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task SetResumeProjects(MySqlConnection connection, ResumeProject[] projects) {
            foreach (ResumeProject cur in projects) {
                string command = @"
                INSERT INTO Resume
                (ID,ResumeID,Name,URL,Description)
                VALUES
                (@ID,@ResumeID,@Name,@URL,@Description)
                ON DUPLICATE KEY UPDATE
                ResumeID = @ResumeID,
                Name = @Name,
                URL = @URL,
                Description = @Description;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", cur.ID);
                cmd.Parameters.AddWithValue("@ResumeID", cur.ResumeID);
                cmd.Parameters.AddWithValue("@Name", cur.Name);
                cmd.Parameters.AddWithValue("@URL", cur.URL);
                cmd.Parameters.AddWithValue("@Description", cur.Description);

                await cmd.ExecuteNonQueryAsync();
            }
        }

    }
}
