package com.example.demo.expand.location;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.InputSource;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

public class LocationParser {
    List<Element> countryRegionList;

    public LocationParser(String xmlFilePath) {
        try {
            FileInputStream fileInputStream = new FileInputStream(xmlFilePath);
            InputSource source = new InputSource(fileInputStream);
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(source);
            Element root = document.getRootElement();
            countryRegionList = root.getChildren();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String parseNameByCode(String countryCode, String stateCode, String cityCode) {
        String countryName = "";
        String stateName = "";
        String cityName = "";
        List<Element> stateNodeList = null;
        List<Element> cityNodeList = null;
        for (Element countryNode : countryRegionList) {
            if (countryNode.getAttribute("Code").getValue().equals(countryCode)) {
                countryName = countryNode.getAttribute("Name").getValue();
                stateNodeList = countryNode.getChildren("State");
                break;
            }
        }
        if (stateNodeList != null) {
            for (Element stateNode : stateNodeList) {
                Attribute CodeAtb = stateNode.getAttribute("Code");
                if (CodeAtb != null) {
                    if (stateCode.equals(CodeAtb.getValue())) {
                        Attribute NameAtb = stateNode.getAttribute("Name");
                        if (NameAtb != null) {
                            stateName = NameAtb.getValue();
                            cityNodeList = stateNode.getChildren("City");
                            break;
                        }
                    }
                } else if (CodeAtb == null && isNullOrEmpty(stateCode)) {
                    cityNodeList = stateNode.getChildren("City");
                    break;
                }
            }
        }
        if (cityNodeList != null) {
            for (Element cityNode : cityNodeList) {
                Attribute CodeAtb = cityNode.getAttribute("Code");
                if (CodeAtb != null) {
                    if (cityCode.equals(CodeAtb.getValue())) {
                        Attribute NameAtb = cityNode.getAttribute("Name");
                        if (NameAtb != null) {
                            cityName = NameAtb.getValue();
                            break;
                        }
                    }
                }
            }
        }
        return countryName + " " + stateName + " " + cityName;
    }

    private boolean isNullOrEmpty(String str) {
        if (str == null) return true;
        if (str.isEmpty()) return true;
        return false;
    }

    public static void main(String[] args) throws Exception {
        String xmlFilePath = new File("").getCanonicalPath()
                + File.separator + "res"
                + File.separator + "location"
                + File.separator + "LocList.xml";
        LocationParser locationParser = new LocationParser(xmlFilePath);
        String res = locationParser.parseNameByCode("1", "11", "1");
        System.out.println(res);
    }
}
