const preact = require('preact');
const jdenticon = require('jdenticon');

const { Provider } = require('preact-redux');
const { createStore, combineReducers } = require('redux');

const reducers = require('./reducers');
const actions = require('./actions');
// const setupKeys = require('./keyboard');
const createSocket = require('./socket');
const registerEvents = require('./events');

const Header = require('./components/header.component');
const Body = require('./components/body.component');

// Redux Store
const store = createStore(
    combineReducers({
        account: reducers.accountReducer,
        combiner: reducers.combinerReducer,
        game: reducers.gameReducer,
        cryps: reducers.crypsReducer,
        instances: reducers.instancesReducer,
        instance: reducers.instanceReducer,
        ws: reducers.wsReducer,
    })
);

document.fonts.load('10pt "Jura"').then(() => {
    const events = registerEvents(store);
    store.subscribe(() => console.log(store.getState()));
    // setupKeys(store);

    const ws = createSocket(events);
    store.dispatch(actions.setWs(ws));
    ws.connect();

    // tells jdenticon to look for new svgs and render them
    // so we don't have to setInnerHtml or manually call update
    jdenticon.config = {
        replaceMode: 'observe',
    };

    const Cryps = () => (
        <section>
            <Header />
            <Body />
        </section>
    );

    const Main = () => (
        <Provider store={store}>
            <Cryps />
        </Provider>
    );

    // eslint-disable-next-line
    preact.render(<Main />, document.body);
});
