<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Band;
use App\Models\BandBlood;
use App\Models\BandHrrest;
use App\Models\BandHrv;
use App\Models\BandOriginBattery;
use App\Models\BandOriginBlood;
use App\Models\BandOriginHrrest;
use App\Models\BandOriginHrv;
use App\Models\BandOriginSleep;
use App\Models\BandOriginSyn;
use App\Models\BandOriginTemp;
use App\Models\BandSleep;
use App\Models\BandSyn;
use App\Models\BandTemp;
use App\Models\SplitModel;
use App\Models\User;
use Illuminate\Http\Request;

class BandController extends Controller
{
    use ResponseJson;

    public function sleepData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginSleep::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);
        }
        return $this->success('ok', $sleeps);
    }

    public function hrrestData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginHrrest::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);
        }
        return $this->success('ok', $sleeps);
    }

    public function tempData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginTemp::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);

        }
        return $this->success('ok', $sleeps);
    }

    public function hrvData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginHrv::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);

        }
        return $this->success('ok', $sleeps);
    }

    public function bloodData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginBlood::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);

        }
        return $this->success('ok', $sleeps);
    }

    public function batteryData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginBattery::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);

        }
        return $this->success('ok', $sleeps);
    }
    public function synData(User $user)
    {
        $suffix = SplitModel::getMonthSuffix();
        $start_time = date("Y-m-01");
        $end_time = date("Y-m-01", strtotime("+1 month"));
        $sleeps = BandOriginSyn::suffix($suffix)->owner($user->id)->time($start_time, $end_time)->orderByDesc("date")->paginate();
        foreach ($sleeps as $sleep) {
            $band = Band::withTrashed()->where("id", $sleep->band_id)->first();
            $sleep->mac = $band ? $band->mac : "";
            if (is_array($sleep->value)) {
                continue;
            }
            $sleep->value = json_decode($sleep->value);

        }
        return $this->success('ok', $sleeps);
    }

    public function userBandList(User $user)
    {
        $bandList = Band::where('user_id', $user->id)->get();
        return $this->success('ok', $bandList);
    }

    public function unBindUserBand($id)
    {
        Band::where('id', $id)->delete();
        return $this->success('ok');
    }
}
