<?php

namespace App\Console\Commands\Converters;

use Storage;
use App\Models\MstStationPortal;
use Illuminate\Support\Arr;
use App\Models\MstAddressPortal;

class AtHomeConverter
{
    const DIRECTORY = 'import/athome';

    public static $numberConverter = ["1" => "１", "2" => "２", '3' => "３" , '4' => "４", '5' => '５', '6' => '６', '7' => '７', '8' => '８', '9' => '９', '10' => '１０' ];
    public static $textJpToNumber = ["一" => "１", "二" => "２", '三' => "３" , '四' => "４", '五' => '５', '六' => '６', '七' => '７', '八' => '８', '九' => '９', '十' => '１０' ];

    public static function filterMemo($text)
    {
        $excepts = ['激安', '―'];

        return str_replace($excepts, ',', $text);
    }

    public static function getPrefectureCode($name)
    {
        //$filename = 'prefectures.json';

        //$prefectures = self::getAddressDataFromFile($filename);
        $sameName = self::sameAddressName($name);
        $prefecture = MstAddressPortal::where('type', 3)->whereIn('pref_name', [$name, $sameName])->first();
        return Arr::get($prefecture, 'pref_id');
    }

    public static function getCityCode($prefectureCode, $name)
    {
        //$filename = 'cities/'.$prefectureCode.'.json';
        $sameName = self::sameAddressName($name);

        $city = MstAddressPortal::where('type', 3)->where('pref_id', $prefectureCode)->whereIn('area_name', [$name, $sameName])->first();

        return Arr::get($city, 'area_id');
    }

    public static function getTownCode($prefectureCode, $cityCode, $name, $address4)
    {
        //$filename = 'towns/'.$prefectureCode.'/'.$cityCode.'.json';
        $sameName = self::sameAddressName($name);
        $townInAddr4 = str_replace('　', '', $address4);
        $townInAddr4 = str_replace(' ', '', $townInAddr4);
        $townListName = [];
        if (str_contains($address4, '丁目')){
            $explodes = explode('丁目', $townInAddr4);
            $townInAddr4 = $explodes[0] ?? $townInAddr4;
            $townInAddr4 = self::$numberConverter[$townInAddr4] ?? $townInAddr4;
            $townInAddr4 = self::$textJpToNumber[$townInAddr4] ?? $townInAddr4;
            $townInAddr4 .= '丁目';
            $townListName[] = $sameName.$townInAddr4;
        }

        $townListName[] = $sameName;
        $towns = MstAddressPortal::where('type', 3)->where('pref_id', $prefectureCode)->where('area_id', $cityCode)
            ->whereIn('town_name', $townListName)
            ->orderByRaw('FIELD(town_name, '.'"'.implode('", "', $townListName).'")')
            ->get();

        if (count($towns) > 0){
            return $towns[0];
        }

        return MstAddressPortal::where('type', 3)->where('pref_id', $prefectureCode)->where('area_id', $cityCode)
            ->where('town_name', 'like',  '%'.$sameName.'%')
            ->first();


//        foreach ($towns as $tw){
//            if($tw->area_id == $cityCode && strpos($tw->town_name, $sameName) !== false){
//                $town[] = $tw;
//            }
//        }

//        if($address4 != ''){
//            $add4 = explode('-',$address4);
//            if(count($add4)>1){
//                $address4 = $add4[0];
//            }
//        }
//        $townname = self::sameAddressName1($sameName.$address4);
//        if(!empty($town)){
//            $town_true = [];
//            foreach ($town as $tw){
//                if(strpos($tw->town_name, $townname) !== false){
//                    $town_true[] = $tw;
//                    break;
//                }
//            }
//            if(empty($town_true)){
//                $townname = self::sameAddressName1($sameName);
//                foreach ($town as $tw){
//                    if(strpos($tw->town_name, $townname) !== false){
//                        $town_true[] = $tw;
//                        break;
//                    }
//                }
//            }
//            $town = $town_true;
//        }

    //    return $town[0];
    }

    public static function getTrafficLineCode($name, $station = null)
    {
//        $sameName = str_replace('JR', 'ＪＲ', $name);
//        $maps = [
//            '京都地下鉄東西線' => '京都市営地下鉄東西線',
//            '京阪京津線' => '京阪電鉄京津線',
//            '嵯峨野観光線' => '嵯峨野観光鉄道',
//            '京阪石山坂本線' => '京阪電鉄石山坂本線',
//            '京阪本線' => '京阪電鉄鴨東線',
//            '叡山鞍馬線' => '叡山電鉄',
//            '叡山本線' => '叡山電鉄',
//            'ＪＲ山陰本線・嵯峨野線' => 'ＪＲ山陰本線',
//            'ＪＲ東海道・山陽本線' => 'ＪＲ東海道本線(米原～神戸)',
//            'ＪＲ根岸線' => 'ＪＲ京浜東北・根岸線',
//            '横浜シーサイドライン' => '金沢シーサイドライン',
//            '横浜市営グリーンライン' => '横浜市グリーンライン',
//            '横浜市営ブルーライン' => '横浜市ブルーライン',
//            'ＪＲ京浜東北線' => 'ＪＲ京浜東北・根岸線',
//            'ＪＲ京都線' => 'ＪＲ東海道本線(米原～神戸)',
//            '神戸電鉄神戸高速線' => '神戸高速南北線',
//            '阪急神戸線' => '阪急電鉄神戸線',
//            '阪急宝塚線' => '阪急電鉄宝塚線',
//            '阪急箕面線' => '阪急電鉄箕面線',
//            '阪急千里線' => '阪急電鉄千里線',
//            'ＪＲおおさか東線' => 'おおさか東線',
//            'ＪＲ和田岬線' => 'ＪＲ山陽本線(神戸～下関)',
//            '阪神本線' => '阪神電鉄本線',
//            '神戸高速鉄道東西線' => '神戸高速東西線',
//            '阪神なんば線' => '阪神電鉄阪神なんば線',
//            '地下鉄西神・山手線' => '神戸市営地下鉄西神山手線',
//            '地下鉄海岸線' => '神戸市営地下鉄海岸線',
//            '地下鉄北神線' => '神戸市営地下鉄北神線',
//            'ポートアイランド線' => '神戸新交通ポートアイランド線',
//            '六甲アイランド線' => '神戸新交通六甲アイランド線',
//            '地下鉄御堂筋線' => '大阪メトロ御堂筋線',
//            '地下鉄今里筋線' => '大阪メトロ今里筋線',
//            '北大阪急行' => '北大阪急行南北線',
//            'ＪＲ東海道本線' => 'ＪＲ東海道本線(東京～熱海)',
//            '京福嵐山本線' => '京福電鉄嵐山本線',
//            '京福北野本線' => '京福電鉄北野線',
//            'ＪＲ神戸線' => 'ＪＲ東海道本線(米原～神戸)',
//            '阪急伊丹線' => '阪急電鉄伊丹線',
//            '阪神武庫川線' => '阪神電鉄武庫川線',
//            '能勢電鉄妙見線' => '能勢電鉄',
//            '能勢電鉄日生線' => '能勢電鉄',
//            '阪急甲陽線' =>  '阪急電鉄甲陽線',
//            '阪急今津線' =>  '阪急電鉄今津線'
//        ];
//        $lineStationMaps = [
//            'ＪＲ神戸線,塚本駅' => 'ＪＲ東海道本線(米原～神戸)',
//            'ＪＲ神戸線,神戸駅' => 'ＪＲ東海道本線(米原～神戸)',
//            'ＪＲ神戸線,甲南山手駅' => 'ＪＲ東海道本線(米原～神戸)',
//            'ＪＲ神戸線,魚住駅' => 'ＪＲ山陽本線(神戸～下関)',
//            'ＪＲ神戸線,兵庫駅' => 'ＪＲ山陽本線(神戸～下関)',
//        ];
//
//        $sameName = $lineStationMaps[$sameName.','.$station] ?? $sameName;
//        $sameName = $maps[$sameName] ?? $sameName;
//
//        $trafficLine = [];
//        $qr_main_trafic_station = MstStationPortal::where('type', 3)->where('line_name', $sameName);
//        if($qr_main_trafic_station->count()>0){
//            $trafficLine = $qr_main_trafic_station->get();
//        }
//
//        return $trafficLine;
        return MstStationPortal::findByNickName($name, $station, 3);
//        $res = [];
//        if (isset($portalStation)){
//            $res = [$portalStation];
//        }
//        return $res;
    }

    public static function sameAddressName($name)
    {
        if(strpos($name, '丁目')){
            $search = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];
            $replace = ["１", "２", "３", "４", "５", "６", "７", "８", "９", "１０"];

            return str_replace($search, $replace, $name);
        }else{
            return $name;
        }

    }

    public static function sameAddressName1($name)
    {
        $search = ['1', '2', '3', '4'];
        $replace = ['１', '２', '３', '４'];

        return str_replace($search, $replace, $name);
    }

    public static function getAddressDataFromFile($filename)
    {
        $filename = 'address/'.$filename;

        $address = self::getDataFromFile($filename);

        return $address ? $address : [];
    }

    public static function getDataFromFile($filename)
    {
        return Converter::getDataFromFile($filename, self::DIRECTORY);
    }
}
