using Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Models;

namespace TEAMModelOS.SDK
{
    public class TmdUserService
    {
        public static async Task<TmdUser> JoinSchool(CosmosClient client, string tmdid,string picture,string name , string schoolId, string schoolName) {
            TmdUser tmdUser = null;
            try {
                tmdUser= await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<TmdUser>(tmdid, partitionKey: new PartitionKey("Base"));
                tmdUser.picture = string.IsNullOrEmpty(picture) ? picture:tmdUser.picture;
                tmdUser.name = string.IsNullOrEmpty(name) ? name : tmdUser.name;
                var school= tmdUser.schools.Find(x => x.schoolId.Equals(schoolId));
                if (school == null)
                {

                    tmdUser.schools.Add(new TmdUser.School { schoolId = schoolId, name = schoolName, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() });
                    await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
                }
                else {
                    school.name = schoolName;
                    await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
                }
            } catch (CosmosException ex) {
                if (ex.Status == 404) {
                    tmdUser = new TmdUser()
                    {
                        id = tmdid,
                        code = "Base",
                        schools = new List<TmdUser.School>() { 
                            new TmdUser.School { 
                                schoolId = schoolId, status = "join", name = schoolName, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                            } 
                        },
                        defaultSchool=schoolId,
                        picture=picture,
                        name=name,
                        ttl=-1
                    };
                    await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(tmdUser, partitionKey: new PartitionKey(tmdUser.code));
                }
            }
            return tmdUser;
        }
        public static async Task<TmdUser> LeaveSchool(CosmosClient client, string tmdid, string schoolId)
        {
            TmdUser tmdUser = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<TmdUser>(tmdid, partitionKey: new PartitionKey("Base"));
            var school = tmdUser.schools.Find(x => x.schoolId.Equals(schoolId));
            if (school != null)
            {
                tmdUser.schools.Remove(school);
                await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<TmdUser>(tmdUser, tmdid, partitionKey: new PartitionKey("Base"));
            }
            return tmdUser;
        }
    }
}
