'use strict';
const _ = require('lodash');

const headerModel = require('../../../doraemon/models/header');
const aboutModel = require('../models/about');

let responseData = {
    module: 'about',
    page: 'about'
};

/**
 * 关于有货
 */
const yohobuy = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    req.ctx(headerModel).requestHeaderData(channel).then(result => {
        res.render('yohobuy', Object.assign({}, responseData, result));
    }).catch(next);
};

/**
 * 新力传媒
 */
const newpower = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    req.ctx(headerModel).requestHeaderData(channel).then(result => {
        res.render('newpower', Object.assign({}, responseData, result));
    }).catch(next);
};

/**
 * 联系我们
 */
const contact = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    req.ctx(headerModel).requestHeaderData(channel).then(result => {
        res.render('contact', Object.assign({}, responseData, result));
    }).catch(next);
};

/**
 * 隐私条款
 */
const privacy = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    req.ctx(headerModel).requestHeaderData(channel).then(result => {
        res.render('privacy', Object.assign({}, responseData, result));
    }).catch(next);
};

/**
 * 友情链接
 */
const link = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    Promise.all([
        req.ctx(headerModel).requestHeaderData(channel),
        req.ctx(aboutModel).link(),
    ]).then(result => {
        res.render('link', Object.assign(_.get(result, '[1]', {}), responseData, result[0]));
    }).catch(next);
};

/**
 * 品类大全
 */
const category = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    return Promise.all([
        req.ctx(headerModel).requestHeaderData(channel, true),
        req.ctx(aboutModel).getCategoryDataWithCache(channel)
    ]).then(result => {
        res.render('category', Object.assign(result[1], responseData, result[0]));
    }).catch(next);
};

/**
 * 产品大全
 */
const chanpin = (req, res, next) => {
    let channel = req.yoho.channel || 'boys';

    return Promise.all([
        req.ctx(headerModel).requestHeaderData(channel, true),
        req.ctx(aboutModel).getChanpinData(channel)
    ]).then(result => {
        res.render('chanpin', Object.assign(result[1], responseData, result[0]));
    }).catch(next);
};

module.exports = {
    yohobuy,
    newpower,
    contact,
    privacy,
    link,
    category,
    chanpin
};