package com.yutou.bilibili.Tools;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.bean.live.LiveRoomConfig;
import com.yutou.biliapi.bean.live.LiveRoomInfo;
import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveDatabase;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class LiveInfoNfoTools {
    public static void saveLiveInfoNfo(LiveRoomInfo info, String path,String xmlName) {
        try {
            LiveRoomConfig config = LiveRoomConfig.buildConfig(info.getRoomId().toString());
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = null;
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.newDocument();
            Element movie = doc.createElement("movie");
            Element plot = doc.createElement("plot");
            CDATASection section = doc.createCDATASection(info.getDescription());
            plot.appendChild(section);
            movie.appendChild(plot);

            Element title = doc.createElement("title");
            title.setTextContent(info.getTitle());
            movie.appendChild(title);

            Element actor = doc.createElement("actor");
            Element name = doc.createElement("name");
            Element role = doc.createElement("role");
            name.setTextContent("UID:" + info.getUid().toString());
            role.setTextContent(config.getAnchorName());
            actor.appendChild(name);
            actor.appendChild(role);
            movie.appendChild(actor);

            Element year = doc.createElement("year");
            year.setTextContent(info.getLiveTime().split("-")[0]);
            movie.appendChild(year);
            Element aired = doc.createElement("aired");
            aired.setTextContent(info.getLiveTime().split(" ")[0]);
            movie.appendChild(aired);

            movie.appendChild(createTag(doc,info.getParentAreaName()));
            movie.appendChild(createTag(doc,info.getAreaName()));



            doc.appendChild(movie);


            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(path, xmlName));
            transformer.transform(source, result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static Element createTag(Document doc,String tag){
        Element genre = doc.createElement("genre");
        genre.setTextContent(tag);
        return genre;
    }

    public static void main(String[] args) {
    }
}
