package celoutils

import (
	"reflect"
	"testing"

	"github.com/grassrootseconomics/w3-celo"
)

func TestEncodeConstructorArgsFromContractABIReader(t *testing.T) {
	type args struct {
		contractBin     string
		contractABI     string
		constructorArgs []any
	}
	tests := []struct {
		name    string
		args    args
		want    []byte
		wantErr bool
	}{
		{
			// contract TopKek {
			// 	constructor(uint256 a, uint256 b) {
			//
			// 	}
			// }
			// 6080604052348015600e575f80fd5b5060405160e438038060e48339818101604052810190602c91906064565b5050609b565b5f80fd5b5f819050919050565b6046816036565b8114604f575f80fd5b50565b5f81519050605e81603f565b92915050565b5f806040838503121560775760766032565b5b5f6082858286016052565b92505060206091858286016052565b9150509250929050565b603e8060a65f395ff3fe60806040525f80fdfea26469706673582212208c499f41d67b9b50acccca0643d94970b6431cced131bd92a09352eb11230e7f64736f6c63430008180033
			name: "Test empty contract named TopKek with constructor(uint256 a, uint256 b) public{}",
			args: args{
				contractBin: "6080604052348015600e575f80fd5b5060405160e438038060e48339818101604052810190602c91906064565b5050609b565b5f80fd5b5f819050919050565b6046816036565b8114604f575f80fd5b50565b5f81519050605e81603f565b92915050565b5f806040838503121560775760766032565b5b5f6082858286016052565b92505060206091858286016052565b9150509250929050565b603e8060a65f395ff3fe60806040525f80fdfea26469706673582212208c499f41d67b9b50acccca0643d94970b6431cced131bd92a09352eb11230e7f64736f6c63430008180033",
				contractABI: `[{"inputs": [{"internalType": "uint256","name": "a","type": "uint256"	},{	"internalType": "uint256","name": "b","type": "uint256"}],"stateMutability": "nonpayable","type": "constructor"}]`,
				constructorArgs: []any{
					w3.Big1,
					w3.Big2,
				},
			},
			want:    w3.B("0x6080604052348015600e575f80fd5b5060405160e438038060e48339818101604052810190602c91906064565b5050609b565b5f80fd5b5f819050919050565b6046816036565b8114604f575f80fd5b50565b5f81519050605e81603f565b92915050565b5f806040838503121560775760766032565b5b5f6082858286016052565b92505060206091858286016052565b9150509250929050565b603e8060a65f395ff3fe60806040525f80fdfea26469706673582212208c499f41d67b9b50acccca0643d94970b6431cced131bd92a09352eb11230e7f64736f6c6343000818003300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
			wantErr: false,
		},
		{
			// 6080604052348015600e575f80fd5b506040516100fa3803806100fa8339818101604052810190602e9190608a565b5060b0565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f605e826037565b9050919050565b606c816056565b81146075575f80fd5b50565b5f815190506084816065565b92915050565b5f60208284031215609c57609b6033565b5b5f60a7848285016078565b91505092915050565b603e806100bc5f395ff3fe60806040525f80fdfea264697066735822122002d6422ed3c6a721f4b2191d7a01c7ccf95b845a29dd6e70541a8bdb2d3ea3cf64736f6c63430008180033
			name: "Test empty contract named TopKek with constructor(address a) public{}",
			args: args{
				contractBin: "6080604052348015600e575f80fd5b506040516100fa3803806100fa8339818101604052810190602e9190608a565b5060b0565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f605e826037565b9050919050565b606c816056565b81146075575f80fd5b50565b5f815190506084816065565b92915050565b5f60208284031215609c57609b6033565b5b5f60a7848285016078565b91505092915050565b603e806100bc5f395ff3fe60806040525f80fdfea264697066735822122002d6422ed3c6a721f4b2191d7a01c7ccf95b845a29dd6e70541a8bdb2d3ea3cf64736f6c63430008180033",
				contractABI: `[{"inputs": [{"internalType": "address","name": "a","type": "address"}],"stateMutability": "nonpayable","type": "constructor" }]`,
				constructorArgs: []any{
					HexToAddress(CUSDContractMainnet),
				},
			},
			want:    w3.B("0x6080604052348015600e575f80fd5b506040516100fa3803806100fa8339818101604052810190602e9190608a565b5060b0565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f605e826037565b9050919050565b606c816056565b81146075575f80fd5b50565b5f815190506084816065565b92915050565b5f60208284031215609c57609b6033565b5b5f60a7848285016078565b91505092915050565b603e806100bc5f395ff3fe60806040525f80fdfea264697066735822122002d6422ed3c6a721f4b2191d7a01c7ccf95b845a29dd6e70541a8bdb2d3ea3cf64736f6c63430008180033000000000000000000000000765de816845861e75a25fca122bb6898b8b1282a"),
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := PrepareContractBytecodeData(tt.args.contractBin, tt.args.contractABI, tt.args.constructorArgs)
			if (err != nil) != tt.wantErr {
				t.Errorf("EncodeConstructorArgsFromContractABIReader() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("EncodeConstructorArgsFromContractABIReader() = %v, want %v", got, tt.want)
			}
		})
	}
}
