/* App bootstrap: Vue 2 root instance + lightweight hash router. */
(function () {
    toastr.options.timeOut = 2600;
    Vue.config.errorHandler = function (err, vm, info) { console.error('VueErr[' + info + ']:', (err && err.stack) || err); };

    var SECTIONS = window.SECTIONS;
    var byId = {};
    SECTIONS.forEach(function (s) { byId[s.id] = s; });

    function parseHash() {
        var h = (location.hash || '#/objects').replace(/^#\/?/, '');
        var parts = h.split('/');
        if (parts[0] === 'object' && parts[1]) return { name: 'object', params: { id: Number(parts[1]) } };
        if (parts[0] === 'new' && parts[1]) return { name: 'new', params: { kind: 'new', id: Number(parts[1]) } };
        var id = parts[0] || 'objects';
        if (!byId[id]) id = 'objects';
        return { name: id, params: { kind: byId[id].kind } };
    }

    var app = new Vue({
        el: '#app',
        data: {
            route: parseHash(),
            sections: SECTIONS,
            showMore: false,
            themePref: window.Theme.pref()
        },
        computed: {
            primaryTabs: function () {
                return window.PRIMARY_TABS.map(function (id) { return byId[id]; });
            },
            isDetail: function () { return this.route.name === 'object'; },
            showBack: function () { return this.route.name === 'object' || (this.route.name === 'new' && this.route.params.id); },
            activeSectionId: function () { return this.isDetail ? 'objects' : this.route.name; },
            currentSection: function () { return byId[this.route.name] || byId.objects; },
            currentComponent: function () {
                return this.isDetail ? 'object-detail' : this.currentSection.component;
            },
            currentTitle: function () {
                if (this.isDetail) return '№ ' + this.route.params.id;
                if (this.route.name === 'new' && this.route.params.id) return 'Redaktə obyekt';
                return this.currentSection.title;
            },
            currentSubtitle: function () {
                if (this.isDetail) return 'Obyekt kartı';
                return '';
            },
            themeIcon: function () {
                return { auto: 'fa-light fa-circle-half-stroke', light: 'fa-light fa-sun-bright', dark: 'fa-light fa-moon-stars' }[this.themePref];
            },
            themeLabel: function () {
                return { auto: 'Avtomatik', light: 'İşıqlı', dark: 'Qaranlıq' }[this.themePref];
            }
        },
        methods: {
            go: function (id) {
                this.showMore = false;
                if (('#/' + id) !== location.hash) location.hash = '/' + id;
                else this.route = parseHash();
                window.scrollTo(0, 0);
            },
            openObject: function (o) {
                location.hash = '/object/' + (o.Id || o);
                window.scrollTo(0, 0);
            },
            back: function () { history.length > 1 ? history.back() : (location.hash = '/objects'); },
            toggleTheme: function () {
                window.Theme.cycle();
                this.themePref = window.Theme.pref();
                toastr.info('Tema: ' + this.themeLabel);
            }
        },
        created: function () {
            var self = this;
            window.addEventListener('hashchange', function () { self.route = parseHash(); });
        }
    });

    /* Small dev helper to flip between mock and live backend from console. */
    window.App = {
        setLive: function (v) { window.APP_CONFIG.useMock = !v; toastr.info(v ? 'Canlı backend rejimi' : 'Mock rejim'); },
        instance: app
    };
})();
