<?php

namespace App\Lib\Settlement\Adapter;


trait RulePlus
{

    public $return_he = ['result' => '2', 'matchResult' => '无规则和'];
    public $return_fail = ['result' => '-1', 'matchResult' => ''];
    public $return_error = ['result' => '100', 'matchResult' => '无胜负判断规则'];


    //足球让球   $scoreDiff比分差   $condition条件   $matchResultWord匹配文字(原样输出)
    //$condition 可能  1  +1  -1  +1.5/2  -1.5/2 这样
    public function zq_letball($scoreDiff, $condition, $matchResultWord)
    {
        $scoreDiff = intval($scoreDiff);
        $returnDatas['matchResult'] = $matchResultWord;

        $hasFH = in_array(substr($condition, 0, 1), ['+', '-']);
        if ($hasFH) {
            $condArr = explode("/", substr($condition, 1));
            $FH = substr($condition, 0, 1);
        } else {
            $condArr = explode("/", $condition);
            $FH = '+';
        }

        if (count($condArr) == 1) {
            $smin = $scoreDiff + ($FH == '+' ? 1 : -1) * $condArr['0'];
            if ($smin > 0) {
                $returnDatas['result'] = 1;
            } elseif ($smin == 0) {
                $returnDatas['result'] = 2;
            } else {
                $returnDatas['result'] = -1;
            }
            return $returnDatas;
        }

        $smin = $scoreDiff + ($FH == "+" ? 1 : -1) * $condArr[0];
        $smax = $scoreDiff + ($FH == "+" ? 1 : -1) * $condArr[1];
        if ($smin > $smax) {
            $this->doexchage($smin, $smax);
        }

        if ($smin < 0 && $smax < 0) {
            $returnDatas['result'] = -1;
            return $returnDatas;
        }

        if ($smin > 0 && $smax > 0) {
            $returnDatas['result'] = 1;
            return $returnDatas;
        }

        if ($smin + $smax > 0) {
            $returnDatas['result'] = 3;
            return $returnDatas;
        }

        $returnDatas['result'] = 4;
        return $returnDatas;

    }

    //足球进球大小 $score进球数  type:2大 1小    $matchResultWord匹配文字原样输出
    //$condition 0.5/1  2/2.5   2   这样的

    /**
     * 条件  大/小 进球数                   输赢
     * 1/1.5 大    0  $min=-1.5,$max=-1     -1
     * 1/1.5 大    1  $min=-0.5,$max=0       4
     * 1/1.5 大    2  $min=0.5,$max=1        1
     * 1/1.5 大    3  $min=1.5,$max=2        1
     *
     * 1.5/2 大    0  $min=-2,$max=-1.5     -1
     * 1.5/2 大    1  $min=-1,$max=-0.5     -1
     * 1.5/2 大    2  $min=0,$max=0.5      3
     * 1.5/2 大    3  $min=1,$max=1.5      1
     * --------------------------------------------------
     * 1/1.5 小    0  $min=-1.5,$max=-1      1
     * 1/1.5 小    1  $min=-0.5,$max=0      3
     * 1/1.5 小    2  $min=0.5,$max=1     -1
     * 1/1.5 小    3  $min=1.5,$max=2       -1
     *
     * 1.5/2 小    0  $min=-2,$max=-1.5      1
     * 1.5/2 小    1  $min=-1,$max=-0.5      1
     * 1.5/2 小    2  $min=0,$max=0.5      4
     * 1.5/2 小    3  $min=1,$max=1.5         -1
     */
    public function zq_inball_bigsmall($score, $condition, $type, $matchResultWord)
    {
        $score = intval($score);
        $returnDatas['matchResult'] = $matchResultWord;
        $condArr = explode("/", $condition);

        if (count($condArr) == 1) {
            if ($score == $condArr['0']) {
                $returnDatas['result'] = 2;
            } else {
                if ($type == 2) {
                    if ($score > $condArr['0']) {
                        $returnDatas['result'] = 1;
                    } else {
                        $returnDatas['result'] = -1;
                    }
                } else {
                    if ($score < $condArr['0']) {
                        $returnDatas['result'] = 1;
                    } else {
                        $returnDatas['result'] = -1;
                    }
                }
            }
            return $returnDatas;
        }

        $smin = $score - $condArr['0'];
        $smax = $score - $condArr['1'];
        if ($smin > $smax) {
            $this->doexchage($smin, $smax);
        }

        if ($type == 2) {
            if ($smin > 0 && $smax > 0) {
                $returnDatas['result'] = 1;
            } elseif ($smin < 0 && $smax < 0) {
                $returnDatas['result'] = -1;
            } else {
                if ($smax > 0) {
                    $returnDatas['result'] = 3;
                } else {
                    $returnDatas['result'] = 4;
                }
            }
        } else {
            if ($smin > 0 && $smax > 0) {
                $returnDatas['result'] = -1;
            } elseif ($smin < 0 && $smax < 0) {
                $returnDatas['result'] = 1;
            } else {
                if ($smin < 0) {
                    $returnDatas['result'] = 3;
                } else {
                    $returnDatas['result'] = 4;
                }
            }
        }
        return $returnDatas;
    }


    //独赢
    public function zq_duying($score, $condition, $matchResultWord)
    {
        $fen = (strtolower($condition) != 'x') ? intval($condition) : 0;
        $smin = $score - $fen;
        $returnDatas['matchResult'] = $matchResultWord;

        if ($smin > 0) {
            $returnDatas['result'] = 1;
        } elseif ($smin == 0) {
            $returnDatas['result'] = 2;
        } else {
            $returnDatas['result'] = -1;
        }
        return $returnDatas;
    }

    //独赢  $halftype 1上半场  4全场      $homeguest=1主队 2客队   $homeguest 1主队赢 2客队赢 3平
    public function zq_duyingv2($homeguest, $halfalltype, $resultModel, $matchResultWord)
    {
        $home_score = intval($resultModel->home_score);
        $guest_score = intval($resultModel->guest_score);
        $home_score_half = intval($resultModel->u_home_score);
        $guest_score_half = intval($resultModel->u_guest_score);

        $returnDatas['matchResult'] = $matchResultWord;
        if ($halfalltype == 4) {
            if ($homeguest == 1) {
                if ($home_score > $guest_score) {
                    $returnDatas['result'] = 1;
                } else {
                    $returnDatas['result'] = -1;
                }
            } elseif ($homeguest == 2) {
                if ($guest_score > $home_score) {
                    $returnDatas['result'] = 1;
                } else {
                    $returnDatas['result'] = -1;
                }
            } else {
                if ($guest_score == $home_score) {
                    $returnDatas['result'] = 1;
                } else {
                    $returnDatas['result'] = -1;
                }
            }
        } else {
            if ($homeguest == 1) {
                if ($home_score_half > $guest_score_half) {
                    $returnDatas['result'] = 1;
                } else {
                    $returnDatas['result'] = -1;
                }
            } elseif ($homeguest == 2) {
                if ($guest_score_half > $home_score_half) {
                    $returnDatas['result'] = 1;
                } else {
                    $returnDatas['result'] = -1;
                }
            } else {
                if ($guest_score_half == $home_score_half) {
                    $returnDatas['result'] = 1;
                } else {
                    $returnDatas['result'] = -1;
                }
            }
        }
        return $returnDatas;
    }


    //单双判断  $type 1单  2双
    public function zq_doublesing($score, $condition, $matchResultWord)
    {
        $returnDatas['matchResult'] = $matchResultWord;
        $condition = trim($condition);
        $gret = (($score % 2) == 0) ? '双' : '单';

        if ($gret == $condition) {
            $returnDatas['result'] = 1;
        } else {
            $returnDatas['result'] = -1;
        }
        return $returnDatas;
    }

    //type ==1 返回第$sn节   ==2返回上下节,sn=1上半场,sn=2下半场    type==4全部按节数的数组
    public function lq_getResultBySn($recoreModesArray, $type = 4, $sn = 1)
    {
        $return = [];
        foreach ($recoreModesArray as $key => $val) {
            $nowsn = intval($val->match_process);
            $return[$nowsn] = $val;
        }
        if (!((count($return) == 4) && isset($return['1']) && isset($return['2']) && isset($return['3']) && isset($return['4']))) {
            throw new  \Exception("篮球结果不为4的数组，或match_process有错");
        }


        if ($type == 4) {
            return $return;
        }

        if ($type == 1) {
            return $return[$sn];
        }

        if ($type == 2) {
            if ($sn == 1) {
                $ret = [
                    $return[1],
                    $return[2],
                ];
            } else {
                $ret = [
                    $return[3],
                    $return[4],
                ];
            }
        }
        return $ret;
    }

    //篮球比分数据汇总，以方便后面使用
    public function lq_scoreDatas($recoreModesArray)
    {
        $arr = $this->lq_getResultBySn($recoreModesArray);
        $return = [
            'list' => $arr,
            'all' => ['home' => 0, 'guest' => 0, 'home_rate' => 0, 'guest_rate' => 0],
            'half' => ['home' => 0, 'guest' => 0, 'home_rate' => 0, 'guest_rate' => 0],
            'half2' => ['home' => 0, 'guest' => 0, 'home_rate' => 0, 'guest_rate' => 0],
        ];

        foreach ($arr as $key => $val) {
            $tmp_home = intval($val->home_score);
            $tmp_guest = intval($val->guest_score);
            $tmp_home_rate = intval($val->home_rate);
            $tmp_guest_rate = intval($val->guest_rate);

            $return['all']['home'] += $tmp_home;
            $return['all']['guest'] += $tmp_guest;
            $return['all']['home_rate'] += $tmp_home_rate;
            $return['all']['guest_rate'] += $tmp_guest_rate;

            if ($key == 1 || $key == 2) {
                $return['half']['home'] += $tmp_home;
                $return['half']['guest'] += $tmp_guest;
                $return['half']['home_rate'] += $tmp_home_rate;
                $return['half']['guest_rate'] += $tmp_guest_rate;
            } else {
                $return['half2']['home'] += $tmp_home;
                $return['half2']['guest'] += $tmp_guest;
                $return['half2']['home_rate'] += $tmp_home_rate;
                $return['half2']['guest_rate'] += $tmp_guest_rate;
            }
        }
        return $return;
    }


    private function doexchage(&$num1, &$num2)
    {
        $tmp = $num2;
        $num2 = $num1;
        $num1 = $tmp;
    }

}



