<?php
/*
*  現在アクティブなURLを判定し、アクティブな場合クラスを返す
*/
function isActiveUrl($url, $string = 'active')
{
    $url = ltrim($url, '/');
    return \Illuminate\Support\Facades\Request::is($url) ? $string : '';
}

/*
*  現在アクティブなURLを判定し、アクティブな場合クラスを返す
*/
function isTargetUrl($url)
{
    return \Illuminate\Support\Facades\Request::is($url);
}

/*
*  セレクトボックスで現在洗濯中の値かを判定する
*/
function isSelected($val, $selected = '', $string = 'selected')
{
    return ($val==$selected) ? $string : '';
}

/*
*  ラジオボックスで現在洗濯中の値かを判定する
*/
function isChecked($val, $num, $checked = '', $string = 'checked')
{
    // print_r([$val, $num, $checked]);
    if($checked === null && $num == 1) {
        return $string;
    }
    return ($val==$checked) ? $string : '';
}

function getArticleType($prop) {
    if(in_array($prop, [1,2,3])) {
        // Nha dat
        return 'land';
    } else if(in_array($prop, [4,5])) {
        // Chung cu
        return 'detached';
    } else if(in_array($prop, [6,7])) {
        // Chung cu
        return 'mansion';
    }
}

function getCurrentStatusName($val = 0, $property = -1) {
    $names = ["-", "更地", "古家有", "未造成", "居住中", "空家", "賃貸中", "建築中", "完成済", "未着工"];
    $names[10] = "居住中";
    $names[11] = "空室";
    $names[12] = "賃貸中";
    $names[13] = "不明";

    switch ($property) {
        case 1:
        case 2:
        case 3:
            if($val >= 1 && $val <= 3) {
                return $names[$val];
            } else {
                return $names[0];
            }
            break;
        case 4:
        case 5:
            if($val >= 4 && $val <= 9) {
                return $names[$val];
            } else {
                return $names[0];
            }
            break;
        case null:
            if($val >= 10 && $val <= 13) {
                return $names[$val];
            } else {
                return $names[0];
            }
            break;
    }

    return isset($names[$val]) ? $names[$val] : $names[0];
}

function getStatus($val) {
    $val--;
    $names = ["販売中", "商談中", "契約予定", "契約済", "売りやめ", "保留"];
    return isset($names[$val]) ? $names[$val] : $names[0];
}

function getWaterSupplyName($val = 0) {
    $names = ["-", "公営水道", "私設水道", "井戸"];
    return isset($names[$val]) ? $names[$val] : $names[0];
}

function getSpringName($item) {
    $names = ["未選択", "温泉使用料" ,"温泉権利金"];
    $name = isset($names[$item->spring]) ? $names[$item->spring] : $names[0];

    if($item->spring != 0) {
        $presences =  ["無", "有", "有（金額未定)"];
        $name .= " - ". (isset($presences[$item->spring_presence]) ? $presences[$item->spring_presence] : $presences[0]);

        if ($item->spring_presence == 1) {
            $units = ["未選択", "月", "年", "一括"];
            $unit = $units[$item->spring_cost_unit] ?? "";
            $name .= " - " . ($item->spring_cost ?? 0) . '円/' . $unit;
        }

        if(is_array($item->spring_kind)) {
            $kindName = ["加温", "加水", "運び湯", "循環装置使用", "循環ろ過装置使用"];
            $name .= '<br> - ';
            foreach ($item->spring_kind as $kind) {
                $name .= ($kindName[$kind-1]) . ', ';
            }
            $name = rtrim($name, ', ');
        }
    }
    return $name;
}

function getCouncilName($item) {
    $names = ["未選択", "町会費", "町内会費", "自治会費"];
    $name = (isset($names[$item->council]) ? $names[$item->council] : $names[0]);
    if($item->council != 0) {
        $presences =  ["無", "有", "有（金額未定)"];
        $name .= " - ". (isset($presences[$item->council_presence]) ? $presences[$item->council_presence] : $presences[0]);
        if ($item->council_presence == 1) {
            $units = ["未選択", "月", "年", "一括"];
            $unit = $units[$item->council_cost_unit] ?? "";
            $name .= " - " . ($item->council_cost ?? 0) . '円/' . $unit;
        }
    }
    return $name;
}


function getAnotherCost($item, $show = [1,2]) {
    $returnStr = '';

    $units = ['未選択', '月', '年', '一括'];

    if($item->another_cost_name1 && in_array(1, $show)) {
        $returnStr .= ($item->another_cost_name1).': '.($item->another_cost1).'円/'.($units[$item->another_cost_unit1]);
    }
    if($item->another_cost_name2 && in_array(2, $show)) {
        if($returnStr != '') {
            $returnStr .= '<br>';
        }
        $returnStr .= ($item->another_cost_name2).': '.($item->another_cost2).'円/'.($units[$item->another_cost_unit2]);
    }

    if(empty($returnStr)) {
        return '-';
    }

    return $returnStr;
}

function startCompare($str1, $str2) {
    if ($str1 == $str2) {
        return strlen($str1);
    }

    if (strlen($str1) < strlen($str2)) {
        $sort = $str1;
        $long = $str2;
    } else {
        $sort = $str2;
        $long = $str1;
    }
    return compareLeftRight($sort, $long);
}

function compareLeftRight($sort, $long) {
    $length = strlen($sort);
    if (compare($sort, $long)) return strlen($sort);
    for ($i_l = $length - 1; $i_l > 0; $i_l--) {
        $str = substr($sort,0, $i_l);
        if (compare($str, $long)) {
            return strlen($str);
        }
    }

    for ($i_r = 0; $i_r < $length; $i_r++) {
        $str = substr($sort, $i_r, $length);
        if (compare($str, $long)) {
            return strlen($str);
        }
    }

    return false;
}

function compare($sort, $long) {
    if ($sort == $long) return true;
    if (strrpos($long, $sort) === false) return false;

    return true;
}

function getLeaseholdKind($id) {
    $kinds = [
        "",
        "旧法賃借権", "普通賃借権", "一般定期賃借権", "建物譲渡特約付き定期賃借権",
        "旧法地上権", "普通地上権", "一般定期地上権", "建物譲渡特約付き定期地上権"
    ];

    return $kinds[$id] ?? "";
}

function selectedPhotoPropertyHomes($type, $selected, $property, $type_upload, $mansion) {
    $result = null;
    $floorPlan = [];
    if($mansion == 1) {
        $arr = config('const.PHOTO_PROPERTY_HOMES_MANSION_SELECTED');
        $photo_property_homes = config('const.PHOTO_PROPERTY_HOMES_MANSION');
    }else{
        if (empty($property)) {
            $arr = config('const.PHOTO_PROPERTY_HOMES_ROOM_SELECTED');
            $photo_property_homes = config('const.PHOTO_PROPERTY_HOMES_ROOM');
            $floorPlan = ["その他", "間取り", "その他", "その他"];
        } elseif ($property < 4) {
            $arr = config('const.PHOTO_PROPERTY_HOMES_LAND_SELECTED');
            $photo_property_homes = config('const.PHOTO_PROPERTY_HOMES_LAND');
            $floorPlan = ["その他", "その他", "その他", "その他"];
        } elseif ($property < 6) {
            $arr = config('const.PHOTO_PROPERTY_HOMES_HOUSE_SELECTED');
            $photo_property_homes = config('const.PHOTO_PROPERTY_HOMES_HOUSE');
            $floorPlan = ["その他", "間取り", "その他", "その他"];
        }
    }
    if ($type == config('const.TYPE_FACILITY_PORTAL') && $type_upload == config('const.TYPE_UPLOAD_BUKKEN')) {
        $result = array_keys($photo_property_homes, config('const.PHOTO_FACILITY_HOMES_SELECTED'));
    }

    if ($type == config('const.TYPE_IMAGE_PORTAL') && isset($arr[$selected - 1])) {
        $result = array_keys($photo_property_homes, $arr[$selected - 1]);
    }

    if ($type == config('const.TYPE_FLOOR_PLAN')) {
        if ($selected == 0) {
            $result = array_keys($photo_property_homes, $floorPlan[3]);
        } else if (isset($floorPlan[$selected - 1])) {
            $result = array_keys($photo_property_homes, $floorPlan[$selected - 1]);
        }
    }

    if ($type_upload == config('const.TYPE_UPLOAD_BUKKEN')) {
        return !empty($result) ? (int)$result[0] + 1 : '';
    }

    return $selected;
}

function selectedPhotoPropertyAthome($type, $selected, $property, $type_upload, $mansion) {
    $result = null;
    $floorPlan = [];
    if($mansion == 1){
        $arr = config('const.PHOTO_PROPERTY_ATHOME_MANSION_SELECTED');
        $photo_property = config('const.PHOTO_PROPERTY_ATHOME_MANSION');
    }else{
        if (empty($property)) {
            $arr = config('const.PHOTO_PROPERTY_ATHOME_ROOM_SELECTED');
            $photo_property = config('const.PHOTO_PROPERTY_ATHOME_ROOM');
            $floorPlan = ["その他", "間取り図（平面図）", "その他", "その他"];
        } elseif ($property < 4) {
            $arr = config('const.PHOTO_PROPERTY_ATHOME_LAND_SELECTED');
            $photo_property = config('const.PHOTO_PROPERTY_ATHOME_LAND');
            $floorPlan = ["その他", "その他", "その他", "その他"];
        } elseif ($property < 6) {
            $arr = config('const.PHOTO_PROPERTY_ATHOME_HOUSE_SELECTED');
            $photo_property = config('const.PHOTO_PROPERTY_ATHOME_HOUSE');
            $floorPlan = ["その他", "間取り図（平面図）", "その他", "その他"];
        }
    }

    if ($type != config('const.TYPE_FACILITY_UPLOAD') && isset($arr[$selected - 1])) {
        $result = array_keys($photo_property, $arr[$selected - 1]);
    }

    if ($type == config('const.TYPE_FLOOR_PLAN')) {
        if ($selected == 0) {
            $result = array_keys($photo_property, $floorPlan[3]);
        } else if (isset($floorPlan[$selected - 1])) {
            $result = array_keys($photo_property, $floorPlan[$selected - 1]);
        }
    }

    if ($type_upload == config('const.TYPE_UPLOAD_BUKKEN')) {
        return !empty($result) ? (int)$result[0] + 1 : '';
    }

    return $selected;
}

function selectedFacilityAthome($type, $selected) {
    $result = null;
    if ($type == config('const.TYPE_UPLOAD_BUKKEN')) {
        $arr = config('const.PHOTO_FACILITY_ATHOME_SELECTED');
        $photo_property = config('const.PHOTO_FACILITY_ATHOME');
        if (isset($arr[$selected - 1])) {
            $result = array_keys($photo_property, $arr[$selected - 1]);
        }
    }

    return !empty($result) ? (int)$result[0] + 1 : $selected;
}

function selectedFacilitySuumo($type, $selected) {
    $result = null;
    if ($type == config('const.TYPE_UPLOAD_BUKKEN')) {
        $arr = config('const.PHOTO_FACILITY_SUUMO_SELECTED');
        $photo_property = config('const.PHOTO_FACILITY_SUUMO');
        if (isset($arr[$selected - 1])) {
            $result = array_keys($photo_property, $arr[$selected - 1]);
        }
    }

    return !empty($result) ? (int)$result[0] + 1 : $selected;
}

function selectedPhotoPropertySuumo($property, $selected, $type_upload, $mansion, $type) {
    $result = null;
    $floorPlan = [];
    if($mansion == 1){
        $arr = config('const.PHOTO_PROPERTY_SUUMO_MANSION_SELECTED');
        $photo_property = config('const.PHOTO_PROPERTY_SUUMO_MANSION');
    }else{
        if (empty($property)) {
            $arr = config('const.PHOTO_PROPERTY_SUUMO_ROOM_SELECTED');
            $photo_property = config('const.PHOTO_PROPERTY_SUUMO_ROOM');
            $floorPlan = ["その他", "間取り図", "その他", "その他"];
        } elseif ($property < 4) {
            $arr = config('const.PHOTO_PROPERTY_SUUMO_LAND_SELECTED');
            $photo_property = config('const.PHOTO_PROPERTY_SUUMO_LAND');
            $floorPlan = ["区画図", "建物プラン例（間取り図）", "建物プラン例（間取り図）", "その他"];
        } elseif ($property < 6) {
            $arr = config('const.PHOTO_PROPERTY_SUUMO_HOUSE_SELECTED');
            $photo_property = config('const.PHOTO_PROPERTY_SUUMO_HOUSE');
            $floorPlan = ["区画図", "間取り図", "その他", "その他"];
        }
    }

    if ($type_upload == config('const.TYPE_UPLOAD_BUKKEN') && isset($arr[$selected - 1])) {
        $result = array_keys($photo_property, $arr[$selected - 1]);
    }

    if ($type == config('const.TYPE_FLOOR_PLAN')) {
        if ($selected == 0) {
            $result = array_keys($photo_property, $floorPlan[3]);
        } else if (isset($floorPlan[$selected - 1])) {
            $result = array_keys($photo_property, $floorPlan[$selected - 1]);
        }
    }

    if ($type_upload == config('const.TYPE_UPLOAD_BUKKEN')) {
        return !empty($result) ? (int)$result[0] + 1 : '';
    }

    return $selected;
}

function removeSpace($str){
    $name = str_replace(' ','', $str);
    return str_replace('　','', $name);
}

function isCompanyThree() {
    return in_array(auth()->user()->company_id, [2,3]);
}

function getDescriptionForTownName(){
    return in_array(auth()->user()->company_id, [2, 3]) ?  '※丁目は全角数字で入力' : '※数字は漢数字で入力';
}

function getDescriptionTax($tax): string
{
    if (!in_array(auth()->user()->company_id, [3])) return '';
    if (empty($tax)) return '';
    if ($tax === 1) return '';
    if ($tax === 2) return '(税込)';
    return '';
}

function printForCompanyThree($vertical):bool {
    return isCompanyThree() && $vertical !== 1;
}
