package match

import (
	"fmt"
	"os"
	"path"
	"path/filepath"
	"testing"

	"github.com/antgroup/hugescm/modules/wildmatch"
)

var (
	matchPaths = []string{
		".aci.yml",
		".gitignore",
		".vscode/launch.json",
		"LEGAL.md",
		"Makefile",
		"OWNERS",
		"README.md",
		"VERSION",
		"bali.toml",
		"cmd/README.md",
		"cmd/zeta-kusion/balisrc.toml",
		"cmd/zeta-kusion/main.go",
		"cmd/zeta-kusion/new-tag.go",
		"cmd/zeta-kusion/res/versioninfo.json",
		"cmd/zeta-kusion/res/zeta-kusion.manifest",
		"cmd/zeta-kusion/show-changes.go",
		"cmd/zeta/balisrc.toml",
		"cmd/zeta/main.go",
		"cmd/zeta/res/versioninfo.json",
		"cmd/zeta/res/zeta.manifest",
		"docs/pack-format.md",
		"docs/zeta.toml",
		"go.mod",
		"go.sum",
		"modules/README.md",
		"modules/binary/read.go",
		"modules/binary/write.go",
		"modules/bitmap/LICENSE",
		"modules/bitmap/bitmap.go",
		"modules/bitmap/bitmap_test.go",
		"modules/crc/reader.go",
		"modules/diff/diff.go",
		"modules/env/broker.go",
		"modules/env/builder.go",
		"modules/env/constant.go",
		"modules/env/env.go",
		"modules/env/env_unix.go",
		"modules/env/env_windows.go",
		"modules/fnmatch/fnmatch.go",
		"modules/fnmatch/fnmatch_test.go",
		"modules/fnmatch/sparse.go",
		"modules/keyring/LICENSE",
		"modules/keyring/keyring.go",
		"modules/keyring/keyring_darwin.go",
		"modules/keyring/keyring_fallback.go",
		"modules/keyring/keyring_mock.go",
		"modules/keyring/keyring_mock_test.go",
		"modules/keyring/keyring_test.go",
		"modules/keyring/keyring_unix.go",
		"modules/keyring/keyring_windows.go",
		"modules/keyring/secret_service/secret_service.go",
		"modules/merkletrie/change.go",
		"modules/merkletrie/difftree.go",
		"modules/merkletrie/doc.go",
		"modules/merkletrie/doubleiter.go",
		"modules/merkletrie/filesystem/node.go",
		"modules/merkletrie/filesystem/node_test.go",
		"modules/merkletrie/index/node.go",
		"modules/merkletrie/internal/frame/frame.go",
		"modules/merkletrie/internal/fsnoder/dir.go",
		"modules/merkletrie/internal/fsnoder/doc.go",
		"modules/merkletrie/internal/fsnoder/file.go",
		"modules/merkletrie/internal/fsnoder/new.go",
		"modules/merkletrie/iter.go",
		"modules/merkletrie/noder/noder.go",
		"modules/merkletrie/noder/noder_test.go",
		"modules/merkletrie/noder/path.go",
		"modules/merkletrie/noder/path_test.go",
		"modules/merkletrie/noder/sparse.go",
		"modules/merkletrie/noder/sparse_test.go",
		"modules/netrc/LICENSE",
		"modules/netrc/netrc.go",
		"modules/plumbing/color/color.go",
		"modules/plumbing/error.go",
		"modules/plumbing/filemode/filemode.go",
		"modules/plumbing/filemode/filemode_test.go",
		"modules/plumbing/format/diff/colorconfig.go",
		"modules/plumbing/format/diff/patch.go",
		"modules/plumbing/format/diff/unified_encoder.go",
		"modules/plumbing/format/ignore/dir.go",
		"modules/plumbing/format/ignore/doc.go",
		"modules/plumbing/format/ignore/matcher.go",
		"modules/plumbing/format/ignore/pattern.go",
		"modules/plumbing/format/index/decoder.go",
		"modules/plumbing/format/index/decoder_test.go",
		"modules/plumbing/format/index/doc.go",
		"modules/plumbing/format/index/encoder.go",
		"modules/plumbing/format/index/encoder_test.go",
		"modules/plumbing/format/index/index.go",
		"modules/plumbing/format/index/match.go",
		"modules/plumbing/format/pktline/encoder.go",
		"modules/plumbing/format/pktline/encoder_test.go",
		"modules/plumbing/format/pktline/scanner.go",
		"modules/plumbing/format/pktline/scanner_test.go",
		"modules/plumbing/format/readme.md",
		"modules/plumbing/hash.go",
		"modules/plumbing/reference.go",
		"modules/plumbing/validate.go",
		"modules/progressbar/LICENSE",
		"modules/progressbar/colorstring/LICENSE",
		"modules/progressbar/colorstring/colorstring.go",
		"modules/progressbar/progressbar.go",
		"modules/progressbar/spinners.go",
		"modules/reftable/LICENSE",
		"modules/reftable/README.md",
		"modules/reftable/api.go",
		"modules/reftable/block.go",
		"modules/reftable/block_test.go",
		"modules/reftable/constants.go",
		"modules/reftable/iter.go",
		"modules/reftable/merged.go",
		"modules/reftable/merged_test.go",
		"modules/reftable/reader.go",
		"modules/reftable/record.go",
		"modules/reftable/record_test.go",
		"modules/reftable/refname.go",
		"modules/reftable/refname_test.go",
		"modules/reftable/reftable.go",
		"modules/reftable/reftable.md",
		"modules/reftable/reftable_test.go",
		"modules/reftable/stack.go",
		"modules/reftable/stack_test.go",
		"modules/reftable/types.go",
		"modules/reftable/writer.go",
		"modules/securejoin/LICENSE",
		"modules/securejoin/README.md",
		"modules/securejoin/join.go",
		"modules/securejoin/vfs.go",
		"modules/strengthen/du.go",
		"modules/strengthen/du_test.go",
		"modules/strengthen/du_windows.go",
		"modules/strengthen/duration.go",
		"modules/strengthen/humansize.go",
		"modules/strengthen/limitwriter.go",
		"modules/strengthen/path.go",
		"modules/strengthen/rename.go",
		"modules/strengthen/rename_test.go",
		"modules/strengthen/rid.go",
		"modules/strengthen/rid_test.go",
		"modules/strengthen/statfs.go",
		"modules/strengthen/statfs_linux.go",
		"modules/strengthen/statfs_openbsd.go",
		"modules/strengthen/statfs_test.go",
		"modules/strengthen/statfs_unix.go",
		"modules/strengthen/strings.go",
		"modules/mem/bytes.go",
		"modules/mem/sync.go",
		"modules/mem/zlib.go",
		"modules/mem/zstd.go",
		"modules/mem/zstd_test.go",
		"modules/trace/error.go",
		"modules/trace/trace.go",
		"modules/vfs/bound.go",
		"modules/vfs/bound_test.go",
		"modules/vfs/glob.go",
		"modules/vfs/vfs.go",
		"modules/zeta/backend/decode.go",
		"modules/zeta/backend/encode.go",
		"modules/zeta/backend/errors.go",
		"modules/zeta/backend/file_storer.go",
		"modules/zeta/backend/odb.go",
		"modules/zeta/backend/odb_test.go",
		"modules/zeta/backend/pack-objects.go",
		"modules/zeta/backend/pack-objects_test.go",
		"modules/zeta/backend/pack/bounds.go",
		"modules/zeta/backend/pack/encode.go",
		"modules/zeta/backend/pack/errors.go",
		"modules/zeta/backend/pack/index.go",
		"modules/zeta/backend/pack/index_version.go",
		"modules/zeta/backend/pack/pack_test.go",
		"modules/zeta/backend/pack/packfile.go",
		"modules/zeta/backend/pack/reader.go",
		"modules/zeta/backend/pack/set.go",
		"modules/zeta/backend/pack/storage.go",
		"modules/zeta/backend/storage/storage.go",
		"modules/zeta/backend/unpack.go",
		"modules/zeta/config/config.go",
		"modules/zeta/config/config_test.toml",
		"modules/zeta/config/decode.go",
		"modules/zeta/config/decode_test.go",
		"modules/zeta/config/encode.go",
		"modules/zeta/config/encode_test.go",
		"modules/zeta/config/type.go",
		"modules/zeta/object/blob.go",
		"modules/zeta/object/change.go",
		"modules/zeta/object/change_adaptor.go",
		"modules/zeta/object/commit.go",
		"modules/zeta/object/commit_test.go",
		"modules/zeta/object/difftree.go",
		"modules/zeta/object/file.go",
		"modules/zeta/object/fragments.go",
		"modules/zeta/object/object.go",
		"modules/zeta/object/patch.go",
		"modules/zeta/object/rename.go",
		"modules/zeta/object/storage.go",
		"modules/zeta/object/tree.go",
		"modules/zeta/object/tree_test.go",
		"modules/zeta/object/treenode.go",
		"modules/zeta/refs/backend.go",
		"modules/zeta/refs/error.go",
		"modules/zeta/refs/filesystem.go",
		"modules/zeta/refs/filesystem_test.go",
		"modules/zeta/refs/references.go",
		"modules/zeta/refs/rules.go",
		"modules/zeta/refs/rules_test.go",
		"pack-release",
		"pkg/command/README.md",
		"pkg/command/command.go",
		"pkg/command/command_add.go",
		"pkg/command/command_branch.go",
		"pkg/command/command_catfile.go",
		"pkg/command/command_checkout.go",
		"pkg/command/command_clean.go",
		"pkg/command/command_commit.go",
		"pkg/command/command_config.go",
		"pkg/command/command_diff.go",
		"pkg/command/command_gc.go",
		"pkg/command/command_log.go",
		"pkg/command/command_mv.go",
		"pkg/command/command_pull.go",
		"pkg/command/command_push.go",
		"pkg/command/command_reset.go",
		"pkg/command/command_restore.go",
		"pkg/command/command_rm.go",
		"pkg/command/command_status.go",
		"pkg/command/command_switch.go",
		"pkg/command/command_version.go",
		"pkg/command/msic.go",
		"pkg/kong/COPYING",
		"pkg/kong/README.md",
		"pkg/kong/build.go",
		"pkg/kong/callbacks.go",
		"pkg/kong/camelcase.go",
		"pkg/kong/context.go",
		"pkg/kong/defaults.go",
		"pkg/kong/doc.go",
		"pkg/kong/error.go",
		"pkg/kong/global.go",
		"pkg/kong/guesswidth.go",
		"pkg/kong/guesswidth_unix.go",
		"pkg/kong/help.go",
		"pkg/kong/hooks.go",
		"pkg/kong/interpolate.go",
		"pkg/kong/kong.go",
		"pkg/kong/levenshtein.go",
		"pkg/kong/mapper.go",
		"pkg/kong/model.go",
		"pkg/kong/options.go",
		"pkg/kong/resolver.go",
		"pkg/kong/scanner.go",
		"pkg/kong/tag.go",
		"pkg/kong/util.go",
		"pkg/kong/visit.go",
		"pkg/progress/indicators.go",
		"pkg/progress/progressbar.go",
		"pkg/tr/README.md",
		"pkg/tr/languages/zh-CN.toml",
		"pkg/tr/locale/LICENSE",
		"pkg/tr/locale/README.md",
		"pkg/tr/locale/error.go",
		"pkg/tr/locale/locale.go",
		"pkg/tr/locale/locale_darwin.go",
		"pkg/tr/locale/locale_js.go",
		"pkg/tr/locale/locale_posix.go",
		"pkg/tr/locale/locale_shared.go",
		"pkg/tr/locale/locale_windows.go",
		"pkg/tr/translate.go",
		"pkg/tr/translate_test.go",
		"pkg/transport/client/client.go",
		"pkg/transport/endpoint.go",
		"pkg/transport/http/auth.go",
		"pkg/transport/http/base.go",
		"pkg/transport/http/blob.go",
		"pkg/transport/http/branch.go",
		"pkg/transport/http/changes.go",
		"pkg/transport/http/commit.go",
		"pkg/transport/http/pull.go",
		"pkg/transport/http/push.go",
		"pkg/transport/http/tag.go",
		"pkg/transport/struct.go",
		"pkg/transport/transport.go",
		"pkg/version/verison.go",
		"pkg/zeta/branch.go",
		"pkg/zeta/cat-file.go",
		"pkg/zeta/config.go",
		"pkg/zeta/fetch.go",
		"pkg/zeta/gc.go",
		"pkg/zeta/log.go",
		"pkg/zeta/odb/commit.go",
		"pkg/zeta/odb/counting-objects.go",
		"pkg/zeta/odb/decode.go",
		"pkg/zeta/odb/encode.go",
		"pkg/zeta/odb/index.go",
		"pkg/zeta/odb/odb.go",
		"pkg/zeta/odb/pack.go",
		"pkg/zeta/odb/unpack.go",
		"pkg/zeta/odb/util.go",
		"pkg/zeta/options.go",
		"pkg/zeta/pull.go",
		"pkg/zeta/push.go",
		"pkg/zeta/repository.go",
		"pkg/zeta/revision.go",
		"pkg/zeta/status.go",
		"pkg/zeta/switch.go",
		"pkg/zeta/switch_test.go",
		"pkg/zeta/util.go",
		"pkg/zeta/util_test.go",
		"pkg/zeta/worktree.go",
		"pkg/zeta/worktree_bsd.go",
		"pkg/zeta/worktree_commit.go",
		"pkg/zeta/worktree_linux.go",
		"pkg/zeta/worktree_status.go",
		"pkg/zeta/worktree_test.go",
		"pkg/zeta/worktree_unix_other.go",
		"pkg/zeta/worktree_windows.go",
		"rpm/alipay-linkc-zeta.spec",
		"utils/areas/areas_test.go",
		"utils/auth/auth.go",
		"utils/netrc/netrc_test.go",
		"utils/show-ref/main.go",
		"utils/viewport/main.go",
	}
)

func TestFnMatch(t *testing.T) {
	patterns := []string{
		"pkg",
		"pk*",
		"*.md",
		"pkg/",
	}
	match := func(pattern string) {
		fmt.Fprintf(os.Stderr, "------------- check match: %s --------\n", pattern)
		w := wildmatch.NewWildmatch(pattern, wildmatch.SystemCase, wildmatch.Contents)
		for _, name := range matchPaths {
			fmt.Fprintf(os.Stderr, "%s | %s Wildmatch: %v\n", pattern, name, w.Match(name))
		}
	}
	for _, p := range patterns {
		match(p)
	}
	w2 := wildmatch.NewWildmatch("utils/viewport/main.go", wildmatch.SystemCase, wildmatch.Contents)
	fmt.Fprintf(os.Stderr, "%v\n", w2.Match("utils/viewport/main.go"))
}

func TestPathJoin(t *testing.T) {
	fmt.Fprintf(os.Stderr, "%s %s\n", path.Join("", "modules"), filepath.Join("", "modules")) // modules modules
}
