using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
namespace Project
{
    public class ResourceController : BaseController
    {
        [HttpGet]
        public HttpResponseMessage file()
        {
            var config = RequestContext.Configuration;
            var routeData = config.Routes.GetRouteData(Request).Values.ToList();

            var p_resource = routeData.Where(t => t.Key == "resource").FirstOrDefault();
            var p_path = routeData.Where(t => t.Key == "path").FirstOrDefault();
            var p_ext = routeData.Where(t => t.Key == "ext").FirstOrDefault();
            var p_subdir = routeData.Where(t => t.Key == "subdir").FirstOrDefault();

            var v_resource = string.Empty;
            var v_path = string.Empty;
            var v_ext = string.Empty;
            var v_subdir = string.Empty;

            if (p_resource.Key == "resource") v_resource = p_resource.Value.ToString();
            if (p_path.Key == "path") v_path = p_path.Value.ToString();
            if (p_ext.Key == "ext") v_ext = p_ext.Value.ToString();
            if (p_subdir.Key == "subdir") v_subdir = p_subdir.Value.ToString();

            //var file_ext = routeData[0].Value.ToString();
            //var name_resource = routeData[1].Value.ToString() + "." + file_ext;
            //var name_action = routeData[3].Value.ToString();

            Boolean isBinary = true;


            string content_type = "text/plain";

            if (v_ext == "json")
            {
                isBinary = false;
                content_type = "application/json";
            }
            else if (v_ext == "js")
            {
                isBinary = false;
                content_type = "application/js";
            }
            else if (v_ext == "css")
            {
                isBinary = false;
                content_type = "text/css";
            }
            else if (v_ext == "csv")
            {
                isBinary = false;
                content_type = "text/csv";
            }
            else if (v_ext == "ico")
            {
                isBinary = true;
                content_type = "image/x-icon";
            }
            else if(v_ext == "ttf" || v_ext == "otf")
            {
                isBinary = true;
                content_type = "application/octet-stream";
            }

            HttpContent resultContent = null;
            var file = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "View", v_path, v_subdir, v_resource + "." + v_ext);

            if (isBinary)
            {

                if (System.IO.File.Exists(file))
                {
                    var buffer = System.IO.File.ReadAllBytes(file);
                    resultContent = new ByteArrayContent(buffer);
                    Console.WriteLine(">>File(B) : " + file);
                }
                else Console.WriteLine("no resouoir file  " + file);

            }
            else
            {
                if (System.IO.File.Exists(file))
                {

                    var buffer = System.IO.File.ReadAllText(file, System.Text.Encoding.UTF8);
                    resultContent = new StringContent(buffer, System.Text.Encoding.UTF8, content_type);
                    Console.WriteLine(">>File(S) : " + file);
                }
                else Console.WriteLine("no resouoir file  " + file);
            }


            return new HttpResponseMessage()
            {
                Content = resultContent
            };

        }
    }
}
