/*
 * @require lottery.scss
 * */
define(["utils", "config", "media/mediaSet", "dialog", "media", "captcha", "./_lottery"], function (utils, config, mediaSet, dialog, login, captcha) {
    var apiUrl = config.apiUrl;
    var isLottering = false;
    var lottery = new SQ.Lottery({
        elem: ".lottery_container",     // 包裹元素，比如上面的 lottery
        item: ">li", // 奖品元素，被奖品元素包裹
        itemIgnore: "",  // 忽略的奖品元素， ">div" 是一个集合，可能包含其他东西（比如抽奖按钮之类的），这里可以排除
        highLight: "div.icon",  // 奖品元素中的高亮元素
        focus: "focus",    // 高亮的类名
        interval: 100,     // 切换奖品的间隔时间，微秒
        stopTimer: 4,     // 2s 后抽奖停止
        lowerTimer: 2.5, //降速时间
        lowerSpeed: 0.2 //降速最低幅度
    });

    $(document).on('click', '.info_fill,.info_fill_btn', function (e) {
        if (login.hasLogin()) {
            var info_fill_dialog = dialog({
                title: '信息填写',
                content: __inline('./info_fill.html'),
                buttons: {
                    '提交': function (text, label) {
                        utils._jsonP({
                            url: apiUrl.putUserInfo + "&" + $("#info_fill_form").serialize(),
                            data: {
                                category: mediaSet.category
                            }
                        }, function (res) {
                            if (+res.result === 1) {
                                SQ.alert("个人信息提交成功!");
                                info_fill_dialog.hide();
                            } else {
                                SQ.sAlert(res.msg);
                            }
                        })
                    }
                }
            })
        } else {
            login.dialog.show(1);
        }
    })
        .on('click', '.my_prize', function (e) {
            if (login.hasLogin()) {
                utils._jsonP({
                    url: apiUrl.my_rewards,
                    data: {
                        category: mediaSet.category
                    }
                }, function (res) {
                    if (res.list.rewards.length === 0) {
                        SQ.alert('您还没有中奖哦');
                        return;
                    }
                    var rewards = $.map(res.list.rewards, function (item, key) {
                        if (+item.TYPE === 3) {
                            return '<tr><td class="label">' + item.GIFT_NAME + '</td><td class="label">' + item.CARD_NUM + '</td><td class="time">' + item.TIME.substring(0, 10) + '</td></tr>';
                        } else if (+item.TYPE === 1) {
                            return '<tr><td class="label">' + item.GIFT_NAME + '</td><td class="label"><a class="info_fill_btn"></td> <td class="time">' + item.TIME.substring(0, 10) + '</td></tr>';
                        }
                    }).join('');

                    var content = SQ.T(__inline('./my_prize.html'), {content: rewards});

                    var my_prize_dialog = dialog({
                        title: "我的奖品",
                        content: content
                    });

                    my_prize_dialog.bind("show", function () {
                        $(".prize_copy_btn").each(function () {
                            SQ.clip(SQ.byId($(this).attr("id")));
                        });
                        $('.sq-dialog-client', my_prize_dialog.el).css({
                            padding: '7px 0'
                        });
                    });

                    my_prize_dialog.show();
                });
            } else {
                login.dialog.show(1);
            }
        })
        .on('click', '.lottery', function (e) {
            if (login.hasLogin()) {
                if (isLottering) {
                    return;
                }
                captcha.show(function (code, captcha_dialog) {
                    isLottering = true;
                    utils._jsonP({
                        url: apiUrl.lottery,
                        data: {
                            category: mediaSet.category,
                            captcha: code
                        }
                    }, function (res) {
                        if (+res.result === 1) {
                            captcha_dialog.hide();
                            lottery.start();
                            lottery.setStopItem(+res.list.result[0].GIFT_INDEX);
                            lottery.stopCallback = function () {
                                isLottering = false;
                                if (res.list.result.length === 1) {
                                    switch (res.list.result[0].CLASS_KEY) {
                                        case 'pillow':
                                        case 'battery':
                                        case 'jd_card50':
                                            var lottery_entity_dialog = dialog({
                                                title: '获奖信息',
                                                content: '恭喜您获得' + res.list.result[0].GIFT_NAME + '一份！<br/>请填写邮寄信息，奖品将在10个工作日内发放哦！',
                                                height: 100,
                                                buttons: {
                                                    '填写信息': function () {
                                                        lottery_entity_dialog.hide();
                                                        var info_fill_dialog = dialog({
                                                            title: "信息填写",
                                                            content: __inline('./info_fill.html'),
                                                            buttons: {
                                                                '提交': function (text, label) {
                                                                    utils._jsonP({
                                                                        url: apiUrl.putUserInfo + "&" + $("#info_fill_form").serialize() + "&id=" + res.list.result[0].ID,
                                                                        data: {
                                                                            category: mediaSet.category
                                                                        }
                                                                    }, function (res) {
                                                                        if (+res.result === 1) {
                                                                            SQ.alert("个人信息提交成功!");
                                                                            info_fill_dialog.hide();
                                                                        } else {
                                                                            SQ.sAlert(res.msg);
                                                                        }
                                                                    })
                                                                }
                                                            }
                                                        });
                                                    }
                                                }
                                            });
                                            break;
                                        case 'thanks':
                                            dialog({
                                                title: '温馨提示',
                                                content: '恭喜您成功避过所有奖品。抽到谢谢参与。别心塞，来一把游戏再说！',
                                                height: 100,
                                                buttons: {
                                                    '继续游戏': 'http://game.37.com/play.php?game_id=352'
                                                }
                                            });
                                            break;
                                        case 'game_gift':
                                            var lottery_game_gift_dialog = dialog({
                                                title: '温馨提示',
                                                content: SQ.T(__inline('./gift.html'), {
                                                    code: res.list.result[0].CARD_NUM,
                                                    prize: res.list.result[0].GIFT_NAME,
                                                    gift_content: config.gifts_content[res.list.result[0].CLASS_KEY]
                                                }),
                                                buttons: {
                                                    '点击激活': 'http://game.37.com/play.php?game_id=352'
                                                }
                                            }, function (el) {
                                                $(".copy-btn", el).removeAttr("disalbed");
                                                SQ.clip($(".copy-btn", el)[0]);
                                            });
                                            break;
                                        default:
                                            break;

                                    }
                                }
                            }
                        } else if (+res.result === -10000) {
                            SQ.alert("亲爱的玩家，您的抽奖次数不足。");
                            captcha_dialog.hide();
                            isLottering = false
                        } else if (+res.result === -40004) {
                            SQ.alert("亲爱的玩家，您的操作过于频繁，请稍后重试");
                            captcha_dialog.hide();
                            isLottering = false
                        } else if (+res.result === -90014) {
                            SQ.alert("亲爱的玩家，试玩时间未满，请在玩一会哦");
                            captcha_dialog.hide();
                            isLottering = false
                        } else if (+res.result === -40002) {
                            SQ.sAlert(res.msg);
                            isLottering = false
                        } else {
                            captcha_dialog.hide();
                            SQ.alert(res.msg);
                            isLottering = false
                        }
                    });
                });
            } else {
                login.dialog.show(1);
            }
        });
});
