/**
 * 商品详情models
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/5/6
 */

'use strict';
const utils = '../../../utils';
const _ = require('lodash');
const comment = require('./consult-comment');
const bundle = require('./bundle');
const api = global.yoho.API;
const singleAPI = global.yoho.SingleAPI;
const helpers = global.yoho.helpers;
const productProcess = require(`${utils}/product-process`);

const SINGLE_TICKETS_SKN = 51335912; // 展览票

/**
 * 获取用户数据信息
 * @param  {[string]} uid
 * @return {[array]}
 */
const _getUserProfile = (uid) => {
    if (!uid) {
        return Promise.resolve({
            code: 200,
            data: {}
        });
    }
    return api.get('', {
        method: 'app.passport.profile',
        uid: uid
    }, {
        cache: true
    });
};

/**
 * 处理品牌关联店铺信息
 * @param  {array}
 * @return {array}
 */
const _processShopsInfo = (data) => {
    let enterStore = [];

    _.forEach(data, function(value) {
        let shopInfo = {
            img: value.brand_ico,
            storeName: value.brand_name
        };

        if (value.shop_id) {
            shopInfo.url = helpers.urlFormat('/product/index/brand', {
                shop_id: value.shop_id
            });
        } else {
            shopInfo.url = helpers.urlFormat('', null, value.brand_domain);
        }

        enterStore.push(shopInfo);
    });
    return enterStore;
};

/**
 * 处理限购商品的有关按钮状态(或取现购买以及底部商品购买按钮)
 * @param  {array} 需要处理的数据
 * @param  {int} 限购商品的关联状态
 * @param  {Boolean} 限购商品是否已开售
 * @return {array}
 */
const _procShowStatus = (data, showStatus, isBeginSale) => {
    switch (showStatus) {
        case 1: // 开售前/后,立即分享获得限购码(用户未领取限购码)
            // 显示获取限购码按钮
            data.canGetLimitCode = true;

            if (isBeginSale) { // 开售后
                data.cartInfo.noLimitCode = true;
            } else {
                data.cartInfo.limitNotForSale = true;
            }
            break;
        case 2: // 开售后,限购码已抢光(用户未领取限购码)
            data.codeEmpty = true;
            data.cartInfo.noLimitCode = true;
            break;
        case 3: // 开售后,商品已经售罄
            data.noLimitGoodsBtn = true;
            data.cartInfo.soldOut = true;
            break;
        case 4: // 开售后,立即购买(用户已领取限购码)
            data.gotCode = true;
            data.cartInfo.canNotBuy = true;
            break;
        case 5: // 开售前,限购码已被抢光(用户未领取限购码)
            data.codeEmpty = true;
            data.cartInfo.limitNotForSale = true;
            break;
        case 6: // 开售前,即将开售(用户已领取限购码)
            data.gotCode = true;
            data.cartInfo.limitNotForSale = true;
            break;
        case 7: // 开售后,用户已经用获得的限购码购买过商品
            data.gotCode = true;
            data.cartInfo.noLimitCode = true;
            break;
        default:
            break;
    }

    return data;
};

/**
 * 根据设备类型获得限购商品跳转app的url
 * @param  {string} productCode 限购商品码
 * @param  {string} skn 限购商品skn
 * @return {string} 限购商品跳转url
 */
const _getLimitCodeUrl = (productCode, skn, ua) => {
    let url = 'yohoapp://yoho.app/openwith?limit_product_code=' + productCode +
        '&product_skn=' + skn;

    let isIphone = String(ua).indexOf('iPhone') >= 0;
    let isIpad = ua.indexOf('iPad') >= 0;

    if (isIphone || isIpad) {
        let params = {};

        params.lp = productCode;
        url = 'yohobuy://' + helpers.urlFormat('/limitpurchase', params);
    }
    return url;
};

let _getShopsInfo = (brandId) => {
    if (!brandId) {
        return Promise.resolve([]);
    }
    return api.get('', {
        method: 'app.shop.queryShopsByBrandId',
        brand_id: _.toString(brandId)
    }, {
        cache: true
    }).then(shops => {
        if (shops && shops.code === 200) {
            return _processShopsInfo(shops.data);
        }

        return [];
    });
};

let _getPromotionInfo = (skn) => {
    return api.get('', {
        method: 'app.product.promotion',
        product_skn: _.toString(skn)
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return result.data;
        }

        return {};
    });
};

/**
 * [单独获取用户收藏商品状态]
 */
let _getFavorite = (productId, uid) => {
    if (!uid) {
        return Promise.resolve(false);
    }
    return singleAPI.get('favorite', {
        method: 'app.favorite.isFavoriteNew',
        uid: uid,
        id: productId
    }).then((result) => {
        if (result && result.code === 200) {
            return result.data;
        }

        return false;
    });
};

/**
 * [商品获取数据] pagecache重构
 */
let getProductData = (data) => {
    let finalResult;
    let params = {
        method: 'app.product.data'
    };

    if (data.id) { // 通过 productId 获取商品详情
        Object.assign(params, {
            product_id: _.toString(data.id)
        });
    } else if (data.productSkn) { // 通过 productSkn 获取商品详情
        Object.assign(params, {
            product_skn: _.toString(data.productSkn)
        });
    }

    return api.get('', params, {
        code: 200,
        cache: true
    }).then(result => {
        if (!result || result.code === 500 || !result.data) {
            return {};
        }
        result = result.data;
        result.goods_id = data.goodsId;
        return Promise.all([
            _getShopsInfo(_.get(result, 'brand_info.brand_id', 0)),
            comment.getCommentInfo({
                productId: result.product_id
            }),
            _getCommonConsult(),
            comment.getConsults(result.product_id, 1, 2),
            bundle.getBundleBySkn(result.product_skn)
        ]).then((info) => {
            finalResult = _detailDataPkg(result, data.ua);
            finalResult.enterStore = info[0];
            finalResult.isStudent = data.isStudent;

            let bundleData = _.get(info[4], 'data', null);

            /* 套装 */
            if (bundleData && _.get(bundleData, 'bundleInfo.discountType', null) === 1) {
                finalResult.bundleData = {
                    title: '优惠套装',
                    href: helpers.urlFormat('/product/bundle/detail', {skn: result.product_skn, id: result.product_id}),
                    description: '立省¥' + (parseInt(_.get(bundleData, 'bundleInfo.salesPrice', 0), 10) -
                        parseInt(_.get(bundleData, 'bundleInfo.discountPrice', 0), 10)) + '元',
                    productList: productProcess.processProductList(bundleData && bundleData.productList)
                };
            }

            /* 量贩 */
            if (bundleData && _.get(bundleData, 'bundleInfo.discountType', null) === 2) {
                finalResult.discountBuy = {
                    num: _.get(bundleData, 'bundleInfo.bundleCount', 1),
                    promotionPhrase: _.get(bundleData, 'bundleInfo.promotionPhrase', '')
                };
            }

            Object.assign(finalResult.feedbacks, info[1]);

            /* 如果有咨询,显示咨询,否则显示常见问题 */
            if (info[3].total) {
                finalResult.feedbacks.consultsNum = parseInt(info[3].total, 10);

                Object.assign(finalResult.feedbacks, {
                    commonConsults: false,
                    consultsNum: parseInt(info[3].total, 10),
                    consults: _.take(info[3].list, 2)
                });
            } else if (!_.isEmpty(info[2]) && !_.get(info[1], 'feedbacks.consultsNum', 0)) {
                Object.assign(finalResult.feedbacks, {
                    commonConsults: true,
                    consultsNum: true,
                    consults: _.take(info[2], 2)
                });
            }

            /* tar add 161129 SEO 优化使用 */
            finalResult.cononical = result.product_url;

            return finalResult;
        });

    });
};

/**
 * [商品数据格式化] pagecache重构
 */
// const _detailDataPkg = (origin, uid, vipLevel, ua) => {
const _detailDataPkg = (origin, ua) => {
    let dest = {}, // 结果输出
        thumbImageList = [],
        colorGroup = {},
        sizeGroup = [],
        totalStorageNum = 0;

    // 商品名称
    if (!origin.product_name) {
        dest.feedbacks = {};
        return dest;
    }
    dest.goodsName = origin.product_name;

    // 是否是虚拟商品
    dest.virtualGoods = (origin.attribute * 1 === 3);

    // 活动促销短语
    origin.market_phrase && (dest.marketPhrase = origin.market_phrase);

    // 商品促销短语
    origin.sales_phrase && (dest.goodsSubtitle = origin.sales_phrase);

    // 商品标签
    if (origin.tags) {
        let productTags = {};

        _.forEach(origin.tags, function(value) {
            productTags[value] = true;
        });

        dest.tags = productTags;
    }

    // 上市期
    origin.expect_arrival_time && (dest.periodOfMarket = origin.expect_arrival_time);

    // 商品咨询
    dest.feedbacks = {
        consults: [],
        consultsNum: 0
    };

    let consultParams = {
        product_id: origin.product_id
    };

    if (_.has(dest, 'feedbacks.consultsNum')) {
        consultParams.total = dest.feedbacks.consultsNum;
        dest.feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consults', consultParams);
    } else {
        dest.feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consultform', consultParams);
    }

    // 商品评价
    dest.feedbacks.commentsUrl = helpers.urlFormat('/product/detail/comments', {
        product_id: origin.product_id
    });

    // 品牌信息
    if (origin.brand_info) {
        let extra = `?productSkn=${origin.product_skn}&brandId=${origin.brand_info.brand_id}`;

        dest.preferenceUrl = `/product/detail/preference${extra}`;
    }
    dest.brandId = origin.brand_id || 0;

    dest.productSkn = origin.product_skn;
    dest.isLimitBuy = Number(origin.isLimitBuy); // 1 限购商品, 0 非限购商品

    // 商品信息
    if (origin.goods_list.length) {
        let goodsGroup = [];

        // pagecache重构
        _.forEach(origin.goods_list, function(value) {

            // 商品分组
            if (value.images_list) {
                _.forEach(value.images_list, function(good) {
                    goodsGroup.push({
                        goodsId: value.goods_id,
                        img: good.image_url
                    });
                });
            }
        });


        // 商品图:多个
        if (goodsGroup.length > 1) {
            let bannerList = [];

            _.forEach(goodsGroup, function(value) {
                bannerList.push({
                    img: value.img
                });
            });

            dest.bannerTop = {
                list: bannerList
            };
        } else if (goodsGroup[0]) {
            dest.bannerTop = {
                img: goodsGroup[0].img
            };
        }
    }

    // 悬浮的购物车信息
    dest.cartInfo = {
        cartUrl: helpers.urlFormat('/cart/index/index'),
        numInCart: 0,
        goodsInstore: origin.storage_sum
    };

    // 底部简介URL链接
    dest.introUrl = '/product/detail/intro/' + origin.product_skn;
    dest.id = origin.product_id;
    dest.goodsId = origin.goods_id;
    dest.isDepositAdvance = origin.is_deposit_advance === 'Y';  // 是否定金预售
    dest.isSeckill = origin.is_secKill === 'Y';   // 是否秒杀
    dest.isLimitBuy = origin.isLimitBuy;  // 是否 限购
    dest.isPresale = Boolean(origin.expect_arrival_time); // 是否普通预售

    // 自定义 属性
    dest.showCoupon = !(
        dest.isDepositAdvance || dest.isSeckill || dest.isLimitBuy || dest.isPresale
    );  // 商品有限购、秒杀、定金预售、普通预售 不显示领券

    // 20170113 要求关闭商品详情页面领券功能
    // dest.showCoupon = false;

    return dest;
};

const _cartCount = (uid, shoppingKey) => {
    return api.get('', {
        method: 'app.Shopping.count',
        uid: uid,
        shopping_key: shoppingKey
    }).then((result) => {
        if (result && result.code === 200) {
            var count = result.data.cart_goods_count || 0;

            if (count > 99) {
                count = '99+';
            }
            return count;
        }
        return 0;
    });
};

/**
 * [商品信息的异步数据获取] (优化)
 */
let getNewProductAsyncData = (data) => {
    let finalResult;
    let params = {
        method: 'app.product.data'
    };

    if (data.id) { // 通过 productId 获取商品详情
        Object.assign(params, {
            product_id: _.toString(data.id)
        });
    } else if (data.productSkn) { // 通过 productSkn 获取商品详情
        Object.assign(params, {
            product_skn: _.toString(data.productSkn)
        });
    }
    if (data.uid) {
        params.uid = data.uid;
    }

    params.is_student = data.isStudent ? 1 : 0;

    params.current_vip_level = data.vipLevel;

    // if (!params.is_student) {
    //     params.current_vip_level = data.vipLevel;
    // }

    return api.get('', params, {
        code: 200,
        cache: true
    }).then(result => {
        if (!result || result.code === 500 || !result.data) {
            return {};
        }
        result = result.data;
        result.goods_id = data.goodsId;

        let apiArray = [
            _cartCount(data.uid, data.shoppingKey),
            _detailDataPkgAsync(result, data.uid, data.vipLevel, data.ua),
            _getFavorite(result.product_id, data.uid)
        ];

        if (data.bundleType) {
            apiArray.push(bundle.getBundleBySkn(data.productSkn));
        }

        return Promise.all(apiArray).then((res) => {
            var cartCount = res[0];
            var finalResult = res[1];
            var isFavorite = res[2];
            let bundleData = _.get(res[3], 'data', {});

            if (cartCount) {
                finalResult.cartCount = cartCount;
            }

            finalResult.isCollect = isFavorite;
            if (finalResult.cartInfo) {
                finalResult.cartInfo.isCollect = isFavorite;
            }
            finalResult.isStudent = data.isStudent;

            /* 量贩 */
            if (bundleData && _.get(bundleData, 'bundleInfo.discountType', null) === 2) {
                let minBuyNum = _.get(bundleData, 'bundleInfo.bundleCount', 1);
                let isNotEnough = true;

                // 是否满足量贩购买要求:剩余商品数量大于量贩最小购买量
                _.forEach(result.goods_list, goods => {
                    _.forEach(goods.size_list, sizes => {
                        if (sizes.storage_number >= minBuyNum) {
                            isNotEnough = false;
                        }
                    });
                });

                if (finalResult.cartInfo && isNotEnough) {
                    finalResult.cartInfo.soldOut = isNotEnough;
                    finalResult.cartInfo.addToCartUrl = false;
                }

                finalResult.discountBuy = {
                    num: minBuyNum,
                    promotionPhrase: _.get(bundleData, 'bundleInfo.promotionPhrase', ''),
                    discount: _.get(bundleData, 'bundleInfo.discount', '')
                };
            }

            return finalResult;
        });
    });
};

/**
 * [商品信息的异步数据获取]
 */
let getProductAsyncData = (data) => {
    let finalResult;
    let params = {
        method: 'app.product.data'
    };

    if (data.id) { // 通过 productId 获取商品详情
        Object.assign(params, {
            product_id: _.toString(data.id)
        });
    } else if (data.productSkn) { // 通过 productSkn 获取商品详情
        Object.assign(params, {
            product_skn: _.toString(data.productSkn)
        });
    }
    if (data.uid) {
        params.uid = data.uid;
    }

    params.is_student = data.isStudent ? 1 : 0;
    params.current_vip_level = data.vipLevel;

    return api.get('', params, {
        code: 200,
        cache: true
    }).then(result => {
        if (!result || result.code === 500 || !result.data) {
            return {};
        }

        result = result.data;
        result.goods_id = data.goodsId;
        return Promise.all([_getPromotionInfo(result.product_skn), _getFavorite(result.product_id, data.uid)]).then((res) => {
            result.promotionBoList = res[0];
            var isFavorite = res[1];

            return _detailDataPkgAsync(result, data.uid, data.vipLevel, data.ua).then(pkg => {
                finalResult = pkg;
                finalResult.isCollect = isFavorite;
                if (finalResult.cartInfo) {
                    finalResult.cartInfo.isCollect = isFavorite;
                }
                finalResult.isStudent = data.isStudent;
                return finalResult;
            });

        });

    });
};

/**
 * [商品信息格式化异步接口]
 */
let _detailDataPkgAsync = (origin, uid, vipLevel, ua) => {
    let dest = {}; // 结果输出

    // 用户未登录时
    if (!uid) {
        dest.loginUrl = helpers.urlFormat('/signin.html', {
            refer: helpers.urlFormat('/product/show_' + origin.product_skn + '.html')
        });
    }

    // 商品名称
    if (!origin.product_name) {
        dest.feedbacks = {};
        return dest;
    }
    dest.goodsName = origin.product_name;

    // 活动促销短语
    origin.market_phrase && (dest.marketPhrase = origin.market_phrase);

    // 商品促销短语
    origin.sales_phrase && (dest.goodsSubtitle = origin.sales_phrase);

    // 促销信息
    if (origin.promotionBoList) {
        let discountList = [];

        _.forEach(origin.promotionBoList, function(value) {
            discountList.push({
                text: `【${value.promotionType}】${value.promotionTitle}`
            });
        });

        discountList.length && (dest.goodsDiscount = {
            list: discountList
        });
    }

    // 商品价格
    let goodsPrice = {
        currentPrice: origin.format_sales_price === '0' ? origin.format_market_price : origin.format_sales_price
    };

    if (origin.format_sales_price !== '0' && origin.format_market_price !== origin.format_sales_price) {
        goodsPrice.previousPrice = origin.format_market_price;
    }

    if (origin.student_price) {
        goodsPrice.studentPrice = origin.student_price;
    }

    dest.goodsPrice = goodsPrice;

    if (origin.is_secKill) {
        dest.isSecKill = origin.is_secKill; // 判断秒杀字段
    }

    if (origin.is_secKill) {
        // dest.isDepositAdvance = origin.isDeposit_advance;// A定金预售字段 = origin.is_deposit_advance === 'Y';  // 是否定金预售

        dest.isPresale = Boolean(origin.expect_arrival_time);
        dest.isDepositAdvance = origin.is_deposit_advance; // 判断定金预售字段
    }

    // 商品返回 YOHO 币
    origin.yohoCoinNum && (dest.commodityReturn = origin.yohoCoinNum);

    // VIP 商品价格
    if (origin.vip) {
        let vipList = [];
        let levelList = {
            银卡: 1,
            金卡: 2,
            白金: 3
        };

        _.forEach(origin.vip, function(value) {
            vipList.push({
                level: levelList[value.caption],
                text: value.price,
                currentLevel: (levelList[value.caption] === parseInt(vipLevel, 10))
            });
        });

        vipList.length && (dest.vipLevel = {
            list: vipList
        });

    }

    // 商品信息
    let cartInfo = productProcess.processSizeInfo(origin);
    let soldOut = (origin.storage_sum === 0) || (cartInfo.totalStorageNum === 0); // status
    let notForSale = origin.attribute === 2;
    let preSale = (origin.status === 0 && origin.advance_shelve_time > 0);

    return new Promise((resolve, reject) => {
        // 悬浮的购物车信息
        dest.cartInfo = {
            cartUrl: helpers.urlFormat('/cart/index/index'),
            numInCart: 0,
            goodsInstore: origin.storage_sum
        };

        // 显示加入购物车链接
        if (!soldOut && !notForSale && !preSale || origin.isLimitBuy) {
            Object.assign(dest.cartInfo, cartInfo, {
                price: dest.goodsPrice.previousPrice ? dest.goodsPrice.previousPrice : '',
                salePrice: dest.goodsPrice.currentPrice ? dest.goodsPrice.currentPrice : '',
            });

            // 限购商品
            if (origin.isLimitBuy) {
                return api.get('', {
                    method: 'app.limitProduct.productStatus',
                    limitProductCode: origin.limitProductCode,
                    uid: uid,
                    product_skn: origin.product_skn
                }, {
                    code: 200,
                    cache: true
                }).then((result) => {
                    if (result.data) {
                        if (!result.data.isLimitBuy) {
                            dest.cartInfo.soldOut = true;
                            return resolve(dest);
                        }

                        // 是否开售
                        let isBeginSale = (result.data.saleStatus === 1);

                        // 限购商品有关的展示状态
                        let showStatus = 1;

                        origin.showStatus && (showStatus = parseInt(result.data.showStatus, 10));

                        // 处理限购商品有关的按钮状态
                        dest = _procShowStatus(dest, showStatus, isBeginSale);

                        dest.cartInfo.limitProductCode = origin.limitProductCode;
                        dest.cartInfo.limitCodeUrl = _getLimitCodeUrl(origin.limitProductCode, origin.product_skn, ua);
                        dest.cartInfo.limitProductPay = helpers.urlFormat('/cart/index/orderEnsure');
                        return resolve(dest);
                    } else {
                        dest.cartInfo.soldOut = true;
                        return resolve(dest);
                    }

                });
            } else {
                dest.cartInfo.addToCartUrl = helpers.urlFormat('/product/buy_' + origin.product_id + '_' +
                    origin.goods_id + '.html');
            }
        } else if (notForSale && !preSale) {
            dest.cartInfo.notForSale = true;
        } else if (soldOut && !preSale) {
            dest.cartInfo.soldOut = true;
        } else if (preSale) {
            dest.cartInfo.preSale = true;
        }
        return resolve(dest);
    }).then(result => {
        // 虚拟商品(门票)
        if (origin.attribute * 1 === 3) {
            result.tickets = true;
            result.ticketsConfirm = helpers.urlFormat('/cart/index/ticketsConfirm');

            // 展览票
            if (origin.product_skn * 1 === SINGLE_TICKETS_SKN) {
                result.single = true;
            } else {
                // 套票
                result.package = true;
            }

            // 购票限制
            result.cartInfo.limit = 4;

            // 清空活动
            result.goodsDiscount = [];
        }
        result.id = origin.product_id;
        result.goodsId = origin.goods_id;
        if (result.isSecKill === 'Y' && result.cartInfo.totalNum > 0) {
            result.totalNum = 1;
        }

        return result;
    });
};


/**
 * 获取默认咨询列表
 */
const _getCommonConsult = () => {
    let params = {
        method: 'app.consult.common'
    };

    return api.get('', params, {
        code: 200
    }).then(result => {
        let data = {};

        if (result.data) {
            data = result.data;
        }

        return data;
    });
};

/**
 * [根据商品SKN获取商品的简要信息]
 * @param  {[array]} skns [skns]
 * @return {[type]}
 */
let _productInfoBySkns = (skns) => {
    // 调用搜索接口
    let param = {
        method: 'app.search.li',
        query: skns.join(' '),
        limit: skns.length,
        order: 's_t_desc'
    };

    return api.get('', param, {
        cache: true
    });
};

module.exports = {
    getProductData,
    getProductAsyncData,
    getNewProductAsyncData,
    getUserProfile: _getUserProfile,
    productInfoBySkns: _productInfoBySkns,
    detailDataPkgAsync: _detailDataPkgAsync
};