using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OnlineAssessment.Models;
using OnlineAssessment.Services;
using System.Threading.Tasks;
using OnlineAssessment.Common;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Cors;

namespace OnlineAssessment.Controllers
{
    [Authorize]
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("OdiwarePolicy")]
    public class AWSS3FileController : ControllerBase
    {
        private readonly IAWSS3FileService _AWSS3FileService;
        public AWSS3FileController(IAWSS3FileService AWSS3FileService)
        {
            this._AWSS3FileService = AWSS3FileService;
        }
        [Route("uploadFile")]
        [HttpPost]
        public async Task<IActionResult> UploadFileAsync(string uploadFileName)
        {
            var result = await _AWSS3FileService.UploadFile(uploadFileName);
            return Ok(new { isSucess = result });
        }

        /*
        [Route("uploadMyPic")]
        [HttpPost]
        [Authorize(Roles = "Admin,Teacher,Student")]
        public async Task<IActionResult> UploadProfileImageAsync([FromBody] UploadFilePath uploadFilePath)
        {
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            var result = await _AWSS3FileService.UploadProfileImage(institute_id, user_id, uploadFilePath.file_path);
            return Ok(new { isSucess = result });
        }
        */

        [Route("uploadMyPic")]
        [HttpPost]
        [Authorize(Roles = "Admin,Teacher,Student")]
        public async Task<IActionResult> UploadProfileImageAsync(IFormFile file)
        {
            IActionResult returnResponse = null;

            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            dynamic result = await _AWSS3FileService.UploadProfileImage(institute_id, user_id, file);

            if (result is bool && result == true)
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(result));
            } 
            
            else if (result is bool && result == false)
            {
                returnResponse = Ok(ReturnResponse.GetFailureStatus(result));
            }
            else if (result is string)
            {
                returnResponse = Ok(ReturnResponse.GetFailureStatus(result));
            } 

            else
            {
                returnResponse = Ok(ReturnResponse.GetFailureStatus(result));
            }

            return returnResponse;
        }

        [Route("{exam_id}/uploadExamImage")]
        [HttpPost]
        [Authorize(Roles = "Admin,Teacher")]
        public async Task<IActionResult> UploadExamImageAsync(int exam_id, IFormFile file)
        {
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            var result = await _AWSS3FileService.UploadExamImage(institute_id, user_id, exam_id, file);
            return Ok(new { isSucess = result });
        }

        [Route("{practice_id}/uploadPracticeImage")]
        [HttpPost]
        [Authorize(Roles = "Admin,Teacher")]
        public async Task<IActionResult> UploadPracticeImageAsync(int practice_id, IFormFile file)
        {
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            var result = await _AWSS3FileService.UploadPracticeImage(institute_id, user_id, practice_id, file);
            return Ok(new { isSucess = result });
        }

        [Route("uploadQuestionImage")]
        [HttpPost]
        [Authorize(Roles = "Admin,Teacher")]
        public async Task<IActionResult> UploadQuestionImageAsync(int practice_id, IFormFile file)
        {
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            var result = await _AWSS3FileService.UploadQuestionImage(institute_id, file);
            return Ok(new { isSucess = result });
        }

        [Route("filesList")]
        [HttpGet]
        [Authorize(Roles = "Admin,Teacher")]
        public async Task<IActionResult> FilesListAsync()
        {
            var result = await _AWSS3FileService.FilesList();
            return Ok(result);
        }

        [Route("getFile/{fileName}")]
        [HttpGet]
        [Authorize]
        public async Task<IActionResult> GetFile(string fileName)
        {
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            string folder = "/exams/";

            try
            {
                var result = await _AWSS3FileService.GetFile(institute_id, user_id, folder, fileName);
                return File(result, "image/png");
            }
            catch
            {
                return Ok("NoFile");
            }

        }


        [Route("getMyPic")]
        [HttpGet]
        [Authorize(Roles = "Admin,Teacher,Student")]
        public async Task<IActionResult> GetFile()
        {
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
            string folder = "/user-photo/";
            string fileName = $"{user_id.ToString()}";
            try
            {
                var result = await _AWSS3FileService.GetFile(institute_id, user_id, folder, fileName);
                return File(result, "image/png");
            }
            catch
            {
                return Ok("NoFile");
            }

        }

        [Route("updateFile")]
        [HttpPost]
        public async Task<IActionResult> UpdateFile(UploadFileName uploadFileName, string fileName)
        {
            var result = await _AWSS3FileService.UpdateFile(uploadFileName, fileName);
            return Ok(new { isSucess = result });
        }

        [Route("deleteMyPic")]
        [HttpPost]
        [Authorize(Roles = "Admin,Teacher,Student")]
        public async Task<IActionResult> DeleteMyPic()
        {
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
            int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));

            try
            {
                var result = await _AWSS3FileService.DeleteProfileImage(institute_id, user_id);
                return Ok(new { isSucess = result });
            }
            catch
            {
                return Ok("NoFile");
            }
        }

        [Route("deleteFile/{fileName}")]
        [HttpPost]
        public async Task<IActionResult> DeleteFile(string fileName)
        {
            var result = await _AWSS3FileService.DeleteFile(fileName);
            return Ok(new { isSucess = result });
        }
    }
}