using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace IPRS.xiaoshi.sz.com
{
    public class Parser
    {
        public Patent ParserFulltext(string fulltextXml)
        {
            Patent ret = new Patent();
            XDocument doc = XDocument.Parse(fulltextXml);

            XElement BiblioElement = doc.Root.Element("{http://www.sipo.gov.cn/XMLSchema/business}BibliographicData");
            XElement pubElement = BiblioElement.Elements("{http://www.sipo.gov.cn/XMLSchema/business}PublicationReference").FirstOrDefault(e=>e.Attribute("dataFormat")?.Value  == "original");
            XElement DocIdElement = pubElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}DocumentID");
            var WIPOST3Code = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}WIPOST3Code").Value;
            var DocNumber = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}DocNumber").Value;
            var Kind = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}Kind").Value;
            var Date = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}Date").Value;
            ret.PubNo = $"{WIPOST3Code}{DocNumber}{Kind}";
            ret.PubDate = DateTime.ParseExact(Date.ToString(), "yyyyMMdd", null);

            XElement appElement = BiblioElement.Elements("{http://www.sipo.gov.cn/XMLSchema/business}ApplicationReference").FirstOrDefault(e => e.Attribute("dataFormat")?.Value == "original");
            DocIdElement = pubElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}DocumentID");
            WIPOST3Code = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}WIPOST3Code").Value;
            DocNumber = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}DocNumber").Value;
            Kind = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}Kind").Value;
            Date = DocIdElement.Element("{http://www.sipo.gov.cn/XMLSchema/base}Date").Value;
            ret.AppNo = $"{WIPOST3Code}{DocNumber}{Kind}";
            ret.AppDate  = DateTime.ParseExact(Date.ToString(), "yyyyMMdd", null);


            return ret;
        }
    }
}
