'use strict';
import {
    Controller
} from 'js/yoho-mvc';

import {
    NavView,
    ContentView
} from './view';

import {
    globalSearch as search
} from '../models';

let filter = require('js/plugin/filter');
let noResultHbs = require('hbs/product/search/no-result-new.hbs');

class ListController extends Controller {
    constructor() {
        super();
        this.navView = new NavView();
        this.content = new ContentView();
        this.page = 1;
        this.searching = false;
        this.nav = this.navView.getDefaultNav();
        this.nav.reload = false;
        this.query = $.extend(this.queryString(), {
            type: 'new',
            order: this.nav.order
        });

        this.navView.on('nav-change', this.doNavChange.bind(this));
        this.content.on('search', this.doSearch.bind(this));

        this.created();
    }

    queryString() {
        let vars = {},
            hash,
            i;
        let hashes = window.location.search.slice(1).split('&');

        for (i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

    doNavChange(e, nav, navType) {
        this.content.doContentChange(e, navType);
        this.nav = nav;

        if (nav.reload) {
            this.nav.page = 0;
        } else if (nav.end) {
            return;
        }
        this.query = $.extend(this.query, {
            type: navType,
            order: nav.order
        });
        if (this.nav.page === 0) {
            this.search();
        }
    }

    doSearch() {
        if (!this.nav.end) {
            this.search();
        }
    }

    search(opt) {
        if (this.searching) {
            return;
        }

        let ext = {};

        if (opt) {
            switch (opt.type) {
                case 'ageLevel':
                    ext = {
                        age_level: opt.id
                    };
                    break;
                default:
                    ext[opt.type] = opt.id;
                    break;
            }
            this.query = $.extend({}, this.query, ext);
            this.nav.reload = true;
            this.nav.page = 0;

            ['sort', 'color', 'price'].forEach((k) => {
                if (this.query[k] === 0) {
                    delete this.query[k];
                }
            });
        }

        let page = this.nav.page + 1;
        let params = $.extend({}, this.query, {
            page: page,
            currentUrl: location.pathname
        });

        this.searching = true;
        search('//m.yohobuy.com/product/global/search', params).then(data => {
            this.nav.page = page;

            if (!data) {
                this.nav.end = true;
                if (this.nav.reload) {
                    this.content.setList(noResultHbs(), {
                        reload: true
                    });
                }
            } else {
                if (this.nav.reload) {
                    this.content.setList(data, {
                        reload: true
                    });
                } else {
                    this.content.setList(data, {});
                }
            }
        }).catch(() => {}).finally(() => {
            this.nav.reload = false;
            this.searching = false;
        });
    }

    created() {
        this.navView.setFilter(filter);
        filter.initFilter({
            fCbFn: this.search.bind(this),
            hCbFn: () => {
                this.navView.preActive();
            }
        });
    }
}

module.exports = ListController;