"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var styled_components_1 = __importStar(require("@xstyled/styled-components"));
var system_1 = require("@xstyled/system");
var Btn_1 = __importDefault(require("../shared-style/Btn"));
var Frame_1 = __importDefault(require("../Frame/Frame"));
var Range_1 = __importDefault(require("../Range"));
var Icon_1 = __importDefault(require("../Icon"));
var buttons_1 = require("./buttons");
var ListDivider_1 = __importDefault(require("../List/ListDivider"));
var VideoTag = styled_components_1.default.video(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n  width: 100%;\n  padding: 2;\n\n  display: ", ";\n"], ["\n  width: 100%;\n  padding: 2;\n\n  display: ", ";\n"])), function (_a) {
    var visible = _a.visible;
    return (visible ? 'block' : 'none');
});
var Source = function (_a) {
    var src = _a.src;
    return (React.createElement("source", { src: src, type: "video/" + src.substring(src.length - 3) }));
};
var ControlBtn = styled_components_1.default(Btn_1.default)(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n  display: inline-flex;\n  justify-content: center;\n  align-items: center;\n\n  &&,\n  &:active,\n  &:focus {\n    width: 20px;\n    height: 20px;\n    padding: 7;\n\n    ", "\n  }\n"], ["\n  display: inline-flex;\n  justify-content: center;\n  align-items: center;\n\n  &&,\n  &:active,\n  &:focus {\n    width: 20px;\n    height: 20px;\n    padding: 7;\n\n    ",
    "\n  }\n"])), function (_a) {
    var disabled = _a.disabled;
    return disabled && styled_components_1.css(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n        padding: 4;\n        svg {\n          fill: ", ";\n          border-bottom: 1px solid white;\n          border-right: 1px solid white;\n        }\n      "], ["\n        padding: 4;\n        svg {\n          fill: ", ";\n          border-bottom: 1px solid white;\n          border-right: 1px solid white;\n        }\n      "])), system_1.th('colors.grays.3'));
});
var TitleBar = styled_components_1.default.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n  height: 18px;\n  margin-bottom: 2;\n\n  color: ", ";\n  padding: 0 2;\n\n  display: flex;\n  align-items: center;\n  background-color: primary;\n"], ["\n  height: 18px;\n  margin-bottom: 2;\n\n  color: ", ";\n  padding: 0 2;\n\n  display: flex;\n  align-items: center;\n  background-color: primary;\n"])), system_1.th('colors.white'));
var Controls = styled_components_1.default.div(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n  display: flex;\n  align-items: center;\n  padding: 2 0;\n"], ["\n  display: flex;\n  align-items: center;\n  padding: 2 0;\n"])));
var CountDownContainer = styled_components_1.default(Frame_1.default)(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n  display: flex;\n  padding: 6;\n  margin-bottom: 4;\n\n  box-shadow: in;\n  background-color: ", ";\n  height: 50px;\n\n  color: white;\n"], ["\n  display: flex;\n  padding: 6;\n  margin-bottom: 4;\n\n  box-shadow: in;\n  background-color: ", ";\n  height: 50px;\n\n  color: white;\n"])), system_1.th('colors.black'));
var VideoFont = styled_components_1.default.span(templateObject_7 || (templateObject_7 = __makeTemplateObject(["\n  font-family: 'React95Video-Numbers';\n  text-transform: uppercase;\n"], ["\n  font-family: 'React95Video-Numbers';\n  text-transform: uppercase;\n"])));
var ResetFrame = styled_components_1.default(Frame_1.default)(templateObject_8 || (templateObject_8 = __makeTemplateObject(["\n  background-color: transparent;\n  box-shadow: none;\n"], ["\n  background-color: transparent;\n  box-shadow: none;\n"])));
var VideoRange = styled_components_1.default(Range_1.default)(templateObject_9 || (templateObject_9 = __makeTemplateObject(["\n  &::-webkit-slider-thumb {\n    height: 18px;\n    margin-top: -7px;\n    width: 10px;\n  }\n\n  &[value='0']::-webkit-slider-thumb {\n    margin-left: -2px;\n  }\n"], ["\n  &::-webkit-slider-thumb {\n    height: 18px;\n    margin-top: -7px;\n    width: 10px;\n  }\n\n  &[value='0']::-webkit-slider-thumb {\n    margin-left: -2px;\n  }\n"])));
var PlayOrPause = function (_a) {
    var playing = _a.playing;
    return playing ? React.createElement(buttons_1.Pause, null) : React.createElement(buttons_1.Play, null);
};
var arrayFy = function (str) { return [].concat(str); };
function updateProgressBar(player, updateProgress) {
    var percentage = Math.floor((100 / player.duration) * player.currentTime);
    updateProgress(percentage);
}
function parseCurrentTime(secs) {
    if (!secs) {
        return '00:00';
    }
    var sec = parseInt(secs.toString(), 10);
    var hours = Math.floor(sec / 3600);
    var minutes = Math.floor(sec / 60) % 60;
    var seconds = sec % 60;
    return [hours, minutes, seconds]
        .map(function (v) { return (v < 10 ? "0" + v : v); })
        .filter(function (v, i) { return v !== '00' || i > 0; })
        .join(':');
}
var Video = function (_a) {
    var name = _a.name, src = _a.src, videoProps = _a.videoProps, style = _a.style, props = __rest(_a, ["name", "src", "videoProps", "style"]);
    var _b = React.useState(false), playing = _b[0], setPlaying = _b[1];
    var _c = React.useState(false), loadeddata = _c[0], setLoadeddata = _c[1];
    var _d = React.useState(0), progress = _d[0], setProgress = _d[1];
    var player = React.useRef(null);
    var progressRef = React.useRef(null);
    var paths = arrayFy(src);
    var pathname = paths[0];
    React.useEffect(function () {
        var _a, _b, _c, _d;
        (_a = player.current) === null || _a === void 0 ? void 0 : _a.addEventListener('ended', function () {
            setPlaying(false);
            setProgress(0);
        }, false);
        (_b = player.current) === null || _b === void 0 ? void 0 : _b.addEventListener('timeupdate', function () {
            if (player.current) {
                updateProgressBar(player.current, setProgress);
            }
        }, false);
        (_c = player.current) === null || _c === void 0 ? void 0 : _c.addEventListener('loadeddata', function () {
            setLoadeddata(true);
        }, false);
        (_d = player.current) === null || _d === void 0 ? void 0 : _d.addEventListener('playing', function () {
            setPlaying(true);
        }, false);
    }, [player.current]);
    return (React.createElement(Frame_1.default, __assign({ p: 2 }, props, { style: __assign({ width: !loadeddata ? 260 : undefined }, style) }),
        React.createElement(TitleBar, null,
            React.createElement(Icon_1.default, { name: "mplayer_1_13", size: 16, style: { marginRight: 4 } }),
            name || pathname.replace(/^.*[\\/]/, ''),
            !loadeddata && ' (Openning)'),
        React.createElement(VideoTag, __assign({}, videoProps, { visible: loadeddata, ref: player }), paths.map(function (s) { return (React.createElement(Source, { key: s, src: s })); })),
        loadeddata && (React.createElement(ListDivider_1.default, { as: "span", style: { display: 'block', marginBottom: 2 } })),
        React.createElement(ResetFrame, { maxWidth: 250, mx: "auto", mb: 4 },
            React.createElement(CountDownContainer, null,
                React.createElement(ResetFrame, { display: "flex", flexDirection: "column", width: "40%" },
                    React.createElement(VideoFont, { style: {
                            marginTop: 'auto',
                        } }, player.current && parseCurrentTime(player.current.duration)),
                    React.createElement(VideoFont, { style: { height: 12 } }, !loadeddata && 'Openning')),
                React.createElement(ResetFrame, { display: "flex", flexDirection: "column", width: "40%" },
                    React.createElement(VideoFont, { style: {
                            marginTop: 'auto',
                            fontSize: 22,
                        } }, player.current && parseCurrentTime(player.current.currentTime)),
                    React.createElement(VideoFont, { style: { height: 12 } }, "time"))),
            React.createElement(Controls, null,
                React.createElement(ControlBtn, { disabled: !loadeddata, onClick: function () {
                        var _a, _b;
                        if (!playing) {
                            (_a = player.current) === null || _a === void 0 ? void 0 : _a.play();
                        }
                        else {
                            (_b = player.current) === null || _b === void 0 ? void 0 : _b.pause();
                        }
                        setPlaying(!playing);
                    } }, loadeddata ? (React.createElement(PlayOrPause, { playing: playing })) : (React.createElement(Icon_1.default, { name: "user_4", width: 16, height: 16, style: { marginTop: -2 } }))),
                React.createElement(ControlBtn, { disabled: !loadeddata, onClick: function () {
                        if (player.current) {
                            player.current.pause();
                            player.current.currentTime = 0;
                        }
                        setPlaying(false);
                    } },
                    React.createElement(buttons_1.Stop, null)),
                React.createElement(VideoRange, { ref: progressRef, min: "0", max: "100", step: "1", value: progress, style: {
                        width: '70%',
                        marginLeft: 20,
                    }, onClick: function (e) {
                        var el = progressRef.current;
                        var video = player.current;
                        if (video && el) {
                            var percent = e.nativeEvent.offsetX / el.offsetWidth;
                            video.currentTime = percent * video.duration;
                            setProgress(Math.floor(percent / 100));
                        }
                    } })))));
};
exports.default = Video;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9;
//# sourceMappingURL=Video.js.map