<?php

namespace App\Models\Char;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;
use App\Models\Char\CharBase;

class CharItem extends Model
{
    protected $connection = "Char";
    protected $table = "Table_CharItem";
    protected $primaryKey = "CharItemID";
    protected $dates = [];
    protected $fillable =
            [
              "Owner", "Slot", "Deleted", "ItemSerial", "StrRecordKind", "Account", "RecId", "Amount", "ClientSlot", "ClientEquip", "Durability",
              "ReinforceLevel", "SellerDbKey", "SellerName", "SellPrice", "RegisterDate", "SoldDate", "bSold", "MailDbKey", "Belong", "RemoveBelongCount",
              "Quality", "QUalityEffect", "LinkItemSerial", "Storage", "FellowDbKey", "Opened", "Toggle", "SSProperty", "SSEnabled01", "SSRecId01", "SSEnabled02",
              "SSRecId02", "SSEnabled03", "SSRecId03", "StrPlus", "DexPlus", "IntPlus", "VtlPlus", "MtlPlus", "Color", "SubColor", "ProducerName", "ROEffect01",
              "ROEffectValue01", "ROEffect02", "ROEffectValue02", "ROEffect03", "ROEffectValue03", "ROEffect04", "ROEffectValue04", "ROEffect05", "ROEffectValue05",
              "ROEffect06", "ROEffectValue06", "SealedFellowEffect01", "SealedFellowEValue01", "SealedFellowEffect02",
              "SealedFellowEValue02",
              "SealedFellowSlotCount",
              "SealedFellow01",
              "SealedFellow01_Effect01",
              "SealedFellow01_EValue01",
              "SealedFellow01_Effect02",
              "SealedFellow01_EValue02",
              "SealedFellow02",
              "SealedFellow02_Effect01",
              "SealedFellow02_EValue01",
              "SealedFellow02_Effect02",
              "SealedFellow02_EValue02",
              "SealedFellow03",
              "SealedFellow03_Effect01",
              "SealedFellow03_EValue01",
              "SealedFellow03_Effect02",
              "SealedFellow03_EValue02",
              "ExpirationDate",
              "CharItemID",
              "ItemGrade",
              "bSeize",
              "SealedFellowEffect03",
              "SealedFellowEValue03",
              "SealedFellow01_Effect03",
              "SealedFellow01_EValue03",
              "SealedFellow02_Effect03",
              "SealedFellow02_EValue03",
              "SealedFellow03_EValue03",
              "SealedFellowEffect04",
              "SealedFellowEValue04",
              "SealedFellow01_Effect04",
              "SealedFellow01_EValue04",
              "SealedFellow02_Effect04",
              "SealedFellow02_EValue04",
              "SealedFellow03_Effect04",
              "SealedFellow03_EValue04",
              "SealedFellow01_ExpirationDate",
              "SealedFellow02_ExpirationDate",
              "SealedFellow03_ExpirationDate",
              "CarvedPeriodDate",
              "OverriseCount",
              "ReverseReinforceAttemptCount",
              "ReverseReinforceCount",
              "SealedFellow04",
              "SealedFellow04_Effect01",
              "SealedFellow04_EValue01",
              "SealedFellow04_Effect02",
              "SealedFellow04_EValue02",
              "SealedFellow04_Effect03",
              "SealedFellow04_EValue03",
              "SealedFellow04_Effect04",
              "SealedFellow04_EValue04",
              "SealedFellow04_ExpirationDate",
              "ROEffect07",
              "ROEffectValue07",
              "SealedFellow01_ReinforceLv",
              "SealedFellow02_ReinforceLv",
              "SealedFellow03_ReinforceLv",
              "SealedFellow04_ReinforceLv",
              "KeyValueA",
              "WeaponAwakenAttackValue",
              "WeaponAwakenSkillRecID",
              "WeaponAwakenSkillLevel", 
              "WeaponAwakenEffectParam",
              "WeaponAwakenEffectValue",
              "WeaponAwakenRecID1",
              "WeaponAwakenCount1",
              "WeaponAwakenCount2",
              "Exp",
              "Level"
            ];

    public $timestamps = false;

    public static function UPDATEINVENTORYORDER($CharID = - 1)
    {
        $Characters = null;
        $totalAccounts = 0;
        if($CharID == -1){
            $Characters = CharBase::get();
        }
        else{
            $Characters = CharBase::where('DBKey', $CharID)->get();
        }

        foreach ($Characters as $Character) {
            $slot = 0;
            $Items = self::where('Owner', $Character->DBKey)->get();
            foreach ($Items as $item) {
                $item->Slot = $slot;
                $item->update();
                $slot++;
            }
            $totalAccounts++;
        }
        return $totalAccounts;
    }

    //mn = market, in = inventario
    public static function CHECKINVENTORYORDER($CharID = - 1, $StrRecordKind = 'in')
    {
        $ID = 0;
        $Data[] = [];
        $totalAccounts = 0;
        $Characters = null;
        if($CharID == -1){
            $Characters = CharBase::get();
        }
        else{
            $Characters = CharBase::where('DBKey', $CharID)->get();
        }

        foreach ($Characters as $key => $Character) {
            $check = true;
            $slot = 0;
            $Items = self::where('Owner', $Character->DBKey)->where('StrRecordKind', $StrRecordKind)->get();
            foreach ($Items as $item) {
                if($item->Slot != $slot){

                    $item->Slot = $slot;
                    $item->update();

                    $Data[$ID] = [
                        'account' => $item->Owner,
                        'Name' => $Character->Name
                    ];
                    $ID++;
                }
                $slot++;
            }
        }

        return $Data;
    }


}
