package producer

import (
	"encoding/xml"
	"os"
)

type Params struct {
	PanRawFile           string
	MssRawFile           string
	AuxRawFile           string
	SaveRegisteredMssRaw bool
	DoPansharpen         bool
	OutputDir            string
	PanTiffFile          string
	MssTiffFile          string
	FusTIffFile          string
	SubScenes            bool
	ReportFile           string
	DataId               string
	Targets              ROITargets // 分景的时候优先按目标分景输出（_ROI_100_2000_）
}

type ROITargets struct {
	XMLName xml.Name `xml:"targets"`
	Targets []struct {
		StartLine int `xml:"startLine"`
		EndLine   int `xml:"endLine"`
	} `xml:"target"`
}

type XMLImageTask struct {
	XMLName       xml.Name         `xml:"task"`
	Name          string           `xml:"name,attr"`
	ID            string           `xml:"id,attr"`
	Script        string           `xml:"script"`
	InputFileList XMLInputFileList `xml:"inputfilelist"`
	Params        XMLParams        `xml:"params"`
}

type XMLInputFileList struct {
	PanData string `xml:"panData"`
	MssData string `xml:"mssData"`
	AuxData string `xml:"auxData"`
}

type XMLParams struct {
	Satellite      string     `xml:"satellite"`
	Sensor         string     `xml:"sensor"`
	ProductLevel   string     `xml:"productLevel"`
	ProductID      string     `xml:"productId"`
	TempPath       string     `xml:"tempPath"`
	OutputPath     string     `xml:"outputPath"`
	DeleteTempFlag int        `xml:"deleteTempFlag"`
	ReportFile     string     `xml:"reportFile"`
	DataID         string     `xml:"dataId"`
	DoPansharpen   bool       `xml:"doPansharpen"`
	Targets        ROITargets `xml:"targets"`
}

func ParseXMLImageTask(xmlFile string) (*XMLImageTask, error) {
	var task XMLImageTask
	data, err := os.ReadFile(xmlFile)
	if err != nil {
		return nil, err
	}
	err = xml.Unmarshal(data, &task)
	if err != nil {
		return nil, err
	}
	return &task, nil
}

type XMLFUSTask struct {
	XMLName       xml.Name            `xml:"task"`
	Name          string              `xml:"name,attr"`
	ID            string              `xml:"id,attr"`
	Script        string              `xml:"script"`
	InputFileList XMLFUSInputFileList `xml:"inputfilelist"`
	Params        XMLFUSParams        `xml:"params"`
}

type XMLFUSInputFileList struct {
	PanTiff string `xml:"panTiff"`
	MssTiff string `xml:"mssTiff"`
}

type XMLFUSParams struct {
	Satellite      string `xml:"satellite"`
	Sensor         string `xml:"sensor"`
	ProductLevel   string `xml:"productLevel"`
	ProductID      string `xml:"productId"`
	TempPath       string `xml:"tempPath"`
	OutputPath     string `xml:"outputPath"`
	DeleteTempFlag int    `xml:"deleteTempFlag"`
	ReportFile     string `xml:"reportFile"`
	DataID         string `xml:"dataId"`
}

func ParseXMLFUSTask(xmlFile string) (*XMLFUSTask, error) {
	var task XMLFUSTask
	data, err := os.ReadFile(xmlFile)
	if err != nil {
		return nil, err
	}
	err = xml.Unmarshal(data, &task)
	if err != nil {
		return nil, err
	}
	return &task, nil
}
