package itemprops

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/rs/zerolog"
)

type Material struct {
    Id                string                       `json:"id"`
    Name              string                       `json:"name"`
    Flags             MaterialFlags                `json:"material_flags"`
    Density           DimensionItemDensity         `json:"density"`
    FractureToughness DimensionFractureToughness   `json:"fracture_toughness"`
    MeltingPoint      DimensionItemNullTemperature `json:"melting_point,omitempty"`
    BoilingPoint      DimensionItemNullTemperature `json:"boiling_point,omitempty"`
}

func (m Material) Unmarshal() {}

type MaterialFlags struct {
    ConductsElictricity bool `json:"conducts_elictricity"`
    BlocksLiquid        bool `json:"blocks_liquid"`
    AcidResistant       bool `json:"acid_resistant"`
    BlocksGas           bool `json:"blocks_gas"`
    Flammable           bool `json:"flammable"`
    ConductsHeat        bool `json:"conducts_heat"`
    Radiates            bool `json:"radiates"`
}

type MaterialMap map[string]*Material

type tt map[string]MaterialMap

func NewMaterialMap(path string, logger zerolog.Logger) (MaterialMap, error) {

    mm := make(MaterialMap)
    tmp := make(map[string][]interface{})
    flags := make(map[string]*MaterialFlags)
    err := filepath.Walk(path+"/assets/materials",
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }

            if strings.HasSuffix(path, ".json") {
                splt := strings.Split(path, "/")
                logger.Info().Msgf("loading %s %d", splt[len(splt)-1], info.Size())
                bytes, err := os.ReadFile(path)
                if err != nil {
                    return err
                }
                ttmp := make(map[string][]interface{})
                err = json.Unmarshal(bytes, &ttmp)

                if err != nil {
                    return err
                }
                for idx := range ttmp {
                    tmp[idx] = append(tmp[idx], ttmp[idx]...)
                }
            }
            return nil
        })

    _ = flags
    if lst, ok := tmp["material_flags"]; ok {
        for _, item := range lst {
            ttt := item.(map[string]interface{})
            _ = ttt
            for clause, item2 := range ttt {
                bts, err := json.Marshal(item2)
                if err != nil {
                    return nil, fmt.Errorf("Could not marshal back:%w", err)
                }
                flags[clause] = &MaterialFlags{}
                err = json.Unmarshal(bts, flags[clause])
                if err != nil {
                    return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
                }
            }
        }
    }
    logger.Info().Msgf("loaded %d material flag sets", len(flags))

    if lst, ok := tmp["materials"]; ok {
        for _, item := range lst {
            ttt := item.(map[string]interface{})
            _ = ttt
            for clause, item2 := range ttt {

                toReplace := item2.(map[string]interface{})["material_flags"]

                //todo generalize
                if ref, ok := toReplace.(map[string]interface{})["$ref"]; ok {
                    rfs := strings.Split(ref.(string), "/")
                    referredFlag := rfs[len(rfs)-1]
                    item2.(map[string]interface{})["material_flags"] = flags[referredFlag]
                }
                bts, err := json.Marshal(item2)
                if err != nil {
                    return nil, fmt.Errorf("Could not marshal back:%w", err)
                }
                mm[clause] = &Material{Id: clause}
                err = json.Unmarshal(bts, mm[clause])
                if err != nil {
                    return nil, fmt.Errorf("Could not unmarshal to material_flags:%w", err)
                }
            }
        }
    }
    return mm, err
}
