"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const react_1 = __importStar(require("react"));
const styled_components_1 = require("styled-components");
const components_1 = require("../../../themes/components");
const MediaRoomService_1 = require("../../../services/MediaRoomService");
const borderRadius_1 = __importDefault(require("../../../themes/foundations/borderRadius"));
const ConfigProvider_1 = require("../../../utils/ConfigProvider");
const OrientationContext_1 = require("../../../services/OrientationContext");
const styles_1 = __importDefault(require("./styles"));
function CreateNewPoll(props) {
    const { toggle } = props;
    const { mediaRoom } = react_1.useContext(MediaRoomService_1.MediaRoomContext);
    const [pollData, setPollData] = react_1.default.useState({
        question: '',
        options: Array(2).fill(''),
        anonymous: false,
        hideVotes: true,
    });
    const { colors } = react_1.useContext(styled_components_1.ThemeContext);
    const createPoll = () => {
        const { question, options } = pollData;
        if (!question) {
            react_native_1.Alert.alert('Error:', "Question Can't be empty", [{ text: 'OK' }]);
        }
        else if (options[0] === '' || options[1] === '') {
            react_native_1.Alert.alert('Error:', 'Please add minimum two options', [
                { text: 'OK' },
            ]);
        }
        else {
            const data = Object.assign({}, pollData);
            data.options = options.filter((el) => el !== '');
            mediaRoom.newPoll(data);
            toggle();
        }
    };
    const onQuestionTextChange = (question) => {
        setPollData(Object.assign(Object.assign({}, pollData), { question }));
    };
    const onChangeOptionsText = (text, index) => {
        const { options } = pollData;
        options[index] = text;
        setPollData(Object.assign(Object.assign({}, pollData), { options }));
    };
    const addOption = () => {
        const { options } = pollData;
        options.push('');
        setPollData(Object.assign(Object.assign({}, pollData), { options }));
    };
    const onChangeAnonymous = () => {
        const anonymous = !pollData.anonymous;
        setPollData(Object.assign(Object.assign({}, pollData), { anonymous }));
    };
    const hideResultsBeforeVoting = () => {
        const hideVotes = !pollData.hideVotes;
        setPollData(Object.assign(Object.assign({}, pollData), { hideVotes }));
    };
    const { orientation } = react_1.default.useContext(OrientationContext_1.OrientationContext);
    const isLandscape = orientation === 'LANDSCAPE';
    const { sessionConfig } = react_1.useContext(ConfigProvider_1.ConfigServiceContext);
    const windowDimensions = react_native_1.useWindowDimensions();
    const { height } = sessionConfig.theme.dimensions || windowDimensions;
    return (react_1.default.createElement(react_native_1.ScrollView, { style: {
            height: height - 200,
        } },
        react_1.default.createElement(components_1.Box, { py: 20, px: 10 },
            react_1.default.createElement(components_1.VStack, { spacing: 4 },
                react_1.default.createElement(components_1.Text, { fontSize: "xl", textAlign: "center" }, "Poll Question"),
                react_1.default.createElement(react_native_1.TextInput, { multiline: true, autoFocus: false, placeholder: "What is your poll for?", numberOfLines: 4, value: pollData.question, placeholderTextColor: colors.textPrimary['400'], onChangeText: onQuestionTextChange, style: [
                        {
                            backgroundColor: colors.secondary['800'],
                            height: 75,
                            color: colors.textPrimary[300],
                        },
                        styles_1.default.createPollInputText,
                    ] }),
                pollData.options.map((poll, index) => (react_1.default.createElement(react_native_1.TextInput, { autoFocus: false, placeholder: "Enter an option", value: pollData.options[index], placeholderTextColor: colors.textPrimary['400'], onChangeText: (text) => onChangeOptionsText(text, index), style: [
                        {
                            backgroundColor: colors.secondary['800'],
                            color: colors.textPrimary[300],
                        },
                        styles_1.default.createPollInputText,
                    ] }))),
                react_1.default.createElement(components_1.Box, { bg: "secondary.700", mt: 20, width: 125, height: 30, alignItems: "center", justifyContent: "center" },
                    react_1.default.createElement(components_1.Button, { onClick: () => addOption() },
                        react_1.default.createElement(components_1.Text, null, "+ Add an Option"))),
                react_1.default.createElement(components_1.CheckBox, { rightText: "Anonymous", style: {
                        marginTop: 15,
                        paddingRight: 10,
                        height: 20,
                        justifyContent: 'center',
                        borderRadius: borderRadius_1.default.small,
                    }, rightTextStyle: { color: 'white' }, checkedCheckBoxColor: "white", uncheckedCheckBoxColor: "white", isChecked: pollData.anonymous, onClick: onChangeAnonymous }),
                react_1.default.createElement(components_1.CheckBox, { rightText: "Hide Results before Voting", style: {
                        marginTop: 15,
                        paddingRight: 10,
                        height: 20,
                        justifyContent: 'center',
                        borderRadius: borderRadius_1.default.small,
                    }, rightTextStyle: { color: 'white' }, checkedCheckBoxColor: "white", uncheckedCheckBoxColor: "white", isChecked: pollData.hideVotes, onClick: hideResultsBeforeVoting }),
                react_1.default.createElement(components_1.Box, { mt: 10, bg: "primary.500", p: 8, mb: 5, justify: "center", borderRadius: borderRadius_1.default.small },
                    react_1.default.createElement(components_1.Button, { fontSize: "16", onClick: createPoll },
                        react_1.default.createElement(components_1.Text, { textAlign: "center" }, "Create Poll")))))));
}
exports.default = CreateNewPoll;
//# sourceMappingURL=CreateNewPoll.js.map