<?php

namespace App\Models;

use App;
use App\Facade\RedisConnect;
use App\Facade\TableName;
use App\Game\GlobalUserInfo;
use App\Game\Services\OuroGameService;
use App\Http\helper\NumConfig;
use App\Util;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PrivateMail extends Model
{
    const TABLE = 'QPAccountsDB.dbo.PrivateMail';
    protected $table = self::TABLE;
    public $timestamps = false;

    public static function sendMail($MailType, $UserID, $TitleString, $TextString, $BonusString, $order_sn, $amount = 0, $type = 1, $flag = false)
    {

        $params = compact('MailType', 'UserID', 'TitleString', 'TextString', 'BonusString', 'order_sn',
            'amount', 'type', 'flag');
        if (!empty($order_sn) && $type == 2) {
            $redis = new RedisConnect();
            if ($redis->redis()->exists($order_sn)) {
                Log::info("private mail order_sn already exists", $params);
                return true;
            } else {
                Log::info("private mail order_sn set", $params);
                $redis->redis()->setnx($order_sn, 1);
            }
        }

        if($order_sn){
            //免费币不需要邮件领取
            $OrderWithDraw = DB::table(TableName::QPAccountsDB() . 'OrderWithDraw')
                ->where('OrderID', $order_sn)
                ->first();
            if ($OrderWithDraw && $OrderWithDraw->score_type == 1) {
                $amount = 0;
                $BonusString = '';
                $params['amount'] = 0;
                $params['bonus'] = '';
                Log::info("private mail order_sn amount", $params);
                //DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('InsureScore',$amount);
            }
        }

        $mailData = [
            'MailType' => $MailType,
            'MailStatus' => 1,
            'UserID' => $UserID,
            'CreateTime' => date('Y-m-d H:i:s', time()),
            'TitleString' => $TitleString,
            'TextString' => mb_strlen($TextString) > 500 ? mb_substr($TextString, 0, 500) : $TextString,
            'BonusString' => $BonusString,
            'type' => $type,
            'order_sn' => $order_sn,
            'amount' => $amount,
            'DrawBase' => $flag ? $amount : 0
        ];
        $res = DB::connection('write')->table('QPAccountsDB.dbo.PrivateMail')->insertGetId($mailData);
        $params['res'] = $res;
        Log::info("private mail order_sn insert", $params);
        OuroGameService::notifyMail($UserID);
    }

    public static function praiseSendMail($UserID)
    {

        return;
       $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');

        // Set the application locale based on user's language
        App::setLocale($language);
        $package = DB::connection('write')->table('QPPlatformDB.dbo.ChannelPackageName')
            ->where('Channel', AccountsInfo::first($UserID)->Channel)->value('PackageName');

        if (isset($package) && !empty($package)) {
            $data = [
                'MailType' => 2,
                'MailStatus' => 1,
                'UserID' => $UserID,
                'CreateTime' => date('Y-m-d H:i:s'),
                'TitleString' => trans("messages.mail_type.praise"),
                'TextString' => trans("messages.mail_content.praise_text", ['package' => $package]),
                'BonusString' => '',
                'FromAgentID' => 1,
                'amount' => 0,
                'type' => 3,
            ];
            DB::table('QPAccountsDB.dbo.PrivateMail')->insert($data);
        }
    }

    // Recharge success email
    public static function paySendMail($UserID, $order_sn, $favorable_price, $amount)
    {
        $amount = $amount * NumConfig::NUM_VALUE;

        $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');

        App::setLocale($language);

        return true;

        $TitleString = trans("messages.mail_type.pay_success");
        $TextString = trans("messages.mail_content.pay_success_text", [
            'favorable_price' => $favorable_price,
            'order_sn' => $order_sn
        ]);
        self::sendMail(2, $UserID,$TitleString,$TextString,'', $order_sn, $amount, 1);
    }

    // Withdrawal success email
    public static function successMail($UserID, $order_sn, $amount)
    {
        $amount = round($amount / 100, 2);
        $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');
        App::setLocale($language);

        $TitleString = trans("messages.mail_type.withdraw_success");
        $TextString = trans("messages.mail_content.withdraw_success_text", [
            'order_sn' => $order_sn,
            'amount' => $amount
        ]);
        self::sendMail(2, $UserID, $TitleString, $TextString, '', $order_sn, $amount, 2);
    }

    // Withdrawal failure email
    public static function failMail($UserID, $order_sn, $amount, $msg, $bonus)
    {
        $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');
        App::setLocale($language);

        $TitleString = trans("messages.mail_type.withdraw_fail");
        $TextString = trans("messages.mail_content.withdrawal_failure_text", [
            'order_sn' => $order_sn,
            'msg' => $msg
        ]);
        self::sendMail(2, $UserID, $TitleString, $TextString, $bonus, $order_sn, $amount, 2, true);
    }

    // Withdrawal refusal email
    public static function refuseMail($UserID, $order_sn, $amount, $flag = false)
    {
        $bonus = '30000,' . $amount;
        $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');
        App::setLocale($language);

        $TitleString = trans("messages.mail_type.withdraw_refuse");
        $TextString = trans("messages.mail_content.withdraw_refuse_text", [
            'order_sn' => $order_sn
        ]);
        self::sendMail(2, $UserID, $TitleString, $TextString, $bonus, $order_sn, $amount, 2, $flag);
    }

    // Withdrawal clearance email
    public static function ClearDrawMail($UserID, $order_sn, $amount, $flag = false)
    {
        $bonus = '30000,' . $amount;
        $language = GlobalUserInfo::getLocaleByUserID( $UserID,'en');
        App::setLocale($language);

        $TitleString = trans("messages.mail_type.withdraw_clear");
        $TextString = trans("messages.mail_content.withdraw_clear_text", [
            'order_sn' => $order_sn
        ]);
        self::sendMail(2, $UserID, $TitleString, $TextString, $bonus, $order_sn, $amount, 2, $flag);
    }
    public static function RecoveryMail($UserId, $order_sn)
    {
        // 获取用户语言
        App::setLocale(GlobalUserInfo::getLocaleByUserID( $UserId,'en'));

        // 使用语言包获取标题和文本内容
        $TitleString = trans('messages.mail_content.withdrawal_failure_text');
        $TextString = trans('messages.mail_content.recovery_text', ['order_sn' => $order_sn]);

        self::sendMail(2, $UserId, $TitleString, $TextString, '', $order_sn, 0, 2);
    }

    public static function ReplyMessage($userId, $message)
    {
        // 获取用户语言

        App::setLocale(GlobalUserInfo::getLocaleByUserID( $userId,'en'));

        // 使用语言包获取标题
        $TitleString = trans('messages.mail_content.customer_service_reply');

        self::sendMail(8, $userId, $TitleString, $message, '', 'ser' . time() . rand(100, 999), 0, 9);
    }

    public static function PaySuccess($user_id, $payAmt, $favorable_price, $order_sn)
    {
        // 获取用户语言
        App::setLocale(GlobalUserInfo::getLocaleByUserID( $user_id,'en'));

        // 使用语言包获取标题和文本内容
        $TitleString = trans('messages.mail_content.transaction_completed');
        $TextString = trans('messages.mail_content.purchase_text', [
            'favorable_price' => $favorable_price,
            'order_sn' => $order_sn,
        ]);

        self::sendMail(2, $user_id, $TitleString, $TextString, '', $order_sn, $payAmt);
    }

}
