using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml;
using JXCMS.CMS.Movie.Entity;
using SufeiUtil;

namespace JXCMS.CMS.Movie.Spider
{
    public class ClassifySpider
    {
        public static void GetAllClassifyFromUrl(int websiteId, string url, List<WebSiteClassifyEntity> webSiteClassifyEntities)
        {
            var helper = new HttpHelper();
            var item = new HttpItem();
            item.URL = url;
            var result = helper.GetHtml(item);
            if (result.StatusCode == HttpStatusCode.OK)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(result.Html);
                var classNode = doc.SelectSingleNode("/rss/class");
                
                foreach (XmlNode childNode in classNode.ChildNodes)
                {
                    if (webSiteClassifyEntities.All(x => x.TypeId.ToString() != childNode.Attributes["id"].Value))
                    {
                        WebSiteClassifyEntity webSiteClassifyEntity = new WebSiteClassifyEntity();
                        webSiteClassifyEntity.TypeId = int.Parse(childNode.Attributes["id"].Value);
                        webSiteClassifyEntity.TypeName = childNode.InnerText;
                        webSiteClassifyEntity.WebSiteId = websiteId;
                        webSiteClassifyEntities.Add(webSiteClassifyEntity);
                    }
                }
            }
        }
    }
}