package main

import (
	"encoding/json"
	"io/ioutil"
	"os"

	"github.com/multiformats/go-multihash"

	"github.com/filecoin-project/go-state-types/abi"

	"github.com/filecoin-project/lotus/chain/consensus/filcns"
)

func main() {
	if _, err := os.Stat("code.json"); err != nil {
		panic(err) // note: must run in lotuspond/front/src/chain
	}

	// TODO: ActorUpgrade: this is going to be a problem.
	names := map[string]string{
		"system":   "fil/1/system",
		"init":     "fil/1/init",
		"cron":     "fil/1/cron",
		"account":  "fil/1/account",
		"power":    "fil/1/storagepower",
		"miner":    "fil/1/storageminer",
		"market":   "fil/1/storagemarket",
		"paych":    "fil/1/paymentchannel",
		"multisig": "fil/1/multisig",
		"reward":   "fil/1/reward",
		"verifreg": "fil/1/verifiedregistry",
	}

	{
		b, err := json.MarshalIndent(names, "", "  ")
		if err != nil {
			panic(err)
		}

		if err := ioutil.WriteFile("code.json", b, 0664); err != nil {
			panic(err)
		}
	}

	out := map[string][]string{}

	for c, methods := range filcns.NewActorRegistry().Methods {
		cmh, err := multihash.Decode(c.Hash())
		if err != nil {
			panic(err)
		}

		name := string(cmh.Digest)
		remaining := len(methods)

		// iterate over actor methods in order.
		for i := abi.MethodNum(0); remaining > 0; i++ {
			m, ok := methods[i]
			if !ok {
				continue
			}
			out[name] = append(out[name], m.Name)
			remaining--
		}
	}

	{
		b, err := json.MarshalIndent(out, "", "  ")
		if err != nil {
			panic(err)
		}

		if err := ioutil.WriteFile("methods.json", b, 0664); err != nil {
			panic(err)
		}
	}
}
