import { ConnectButton } from '@rainbow-me/rainbowkit';
import { AwesomeButton } from 'react-awesome-button';


export function Wallet() {  
    return (
        <ConnectButton.Custom>
            {({
                account,
                chain,
                openAccountModal,
                openChainModal,
                openConnectModal,
                mounted,
            }) => {
                const ready = mounted;
                const connected =
                ready &&
                account &&
                chain;

                return (
                <div 
                    className='buttons'
                    {...(!ready && {
                    'aria-hidden': true,
                    'style': {
                        opacity: 0,
                        pointerEvents: 'none',
                        userSelect: 'none',
                    },
                    })}
                >
                    {(() => {
                    if (!connected) {
                        return (
                            <AwesomeButton
                                type="secondary"
                                ripple={true}
                                loadingLabel=""
                                resultLabel=""
                                releaseDelay={1500}
                                onReleased={(event, release) => {
                                    openConnectModal();
                                    release();
                                }}>
                                Connect Wallet
                            </AwesomeButton>
                        );
                    }

                    if (chain.unsupported) {
                        return (
                        <AwesomeButton
                            type="secondary"
                            ripple={true}
                            loadingLabel=""
                            resultLabel=""
                            releaseDelay={1500}
                            onReleased={(event, release) => {
                                openChainModal();
                                release();
                            }}>
                            Wrong network
                        </AwesomeButton>
                        );
                    }

                    return (
                        <div style={{ display: 'flex', gap: 12 }}>
                        <AwesomeButton
                            type="secondary"
                            style={{ display: 'flex', alignItems: 'center' }}
                            ripple={true}
                            loadingLabel=""
                            resultLabel=""
                            releaseDelay={1500}
                            onReleased={(event, release) => {
                                openChainModal();
                                release();
                            }}>
                            {chain.hasIcon && (
                            <div
                                style={{
                                background: chain.iconBackground,
                                width: 24,
                                height: 24,
                                borderRadius: 999,
                                overflow: 'hidden',
                                marginRight: 4,
                                }}
                            >
                                {chain.iconUrl && (
                                <img
                                    alt={chain.name ?? 'Chain icon'}
                                    src={chain.iconUrl}
                                    style={{ width: 12, height: 12 }}
                                />
                                )}
                            </div>
                            )}
                            {chain.name}
                        </AwesomeButton>

                        <AwesomeButton
                            type="primary"
                            style={{ display: 'flex', alignItems: 'center' }}
                            ripple={true}
                            loadingLabel=""
                            resultLabel=""
                            releaseDelay={1500}
                            onReleased={(event, release) => {
                                openAccountModal();
                                release();
                            }}>
                            {account.displayName}
                            {account.displayBalance
                            ? ` (${account.displayBalance})`
                            : ''}
                        </AwesomeButton>
                        </div>
                    );
                    })()}
                </div>
                );
            }}
        </ConnectButton.Custom>
    );
};
