
const regions = [
    {id:1, name:"北海道", kana:"ホッカイドウ", en:"hokkaido", neighbor:[2]},
    {id:2, name:"東北",   kana:"トウホク",     en:"tohoku",   neighbor:[1]},
    {id:3, name:"関東",   kana:"カントウ",     en:"kanto",    neighbor:[2, 4]},
    {id:4, name:"中部",   kana:"チュウブ",     en:"chubu",    neighbor:[2, 3, 5]},
    {id:5, name:"近畿",   kana:"キンキ",       en:"kinki",    neighbor:[4, 6, 7]},
    {id:6, name:"中国",   kana:"チュウゴク",   en:"chugoku",  neighbor:[5, 7, 8]},
    {id:7, name:"四国",   kana:"シコク",       en:"shikoku",  neighbor:[5, 6, 8]},
    {id:8, name:"九州",   kana:"キュウシュウ", en:"kyusyu",   neighbor:[6, 7]}
];

const prefectures = [
    {id:1,  region:1, name:"北海道",   short:"北海道", kana:"ホッカイドウ", en:"hokkaido",  neighbor:[2], data: [{before: 1, after: 7}, {before: 40, after: 99}], dataEmpty: []}, //
    {id:2,  region:2, name:"青森県",   short:"青森",   kana:"アオモリ",     en:"aomori",    neighbor:[1, 3, 5], data: [{before: 30, after: 39}], dataEmpty: [32]}, //
    {id:3,  region:2, name:"岩手県",   short:"岩手",   kana:"イワテ",       en:"iwate",     neighbor:[2, 4, 5], data: [{before: 20, after: 29}], dataEmpty: []}, //
    {id:4,  region:2, name:"宮城県",   short:"宮城",   kana:"ミヤギ",       en:"miyagi",    neighbor:[3, 5, 6, 7], data: [{before: 980, after: 989}], dataEmpty: []}, //
    {id:5,  region:2, name:"秋田県",   short:"秋田",   kana:"アキタ",       en:"akita",     neighbor:[2, 3, 4, 6], data: [{before: 10, after: 19}], dataEmpty: []}, //
    {id:6,  region:2, name:"山形県",   short:"山形",   kana:"ヤマガタ",     en:"yamagata",  neighbor:[4, 5, 7, 15], data: [{before: 990, after: 999}], dataEmpty: []}, //
    {id:7,  region:2, name:"福島県",   short:"福島",   kana:"フクシマ",     en:"fukushima", neighbor:[4, 6, 8, 9, 10, 15], data: [{before: 960, after: 979}], dataEmpty: [978]},
    {id:8,  region:3, name:"茨城県",   short:"茨城",   kana:"イバラキ",     en:"ibaraki",   neighbor:[7, 9, 11, 12], data: [{before: 300, after: 319}], dataEmpty: []}, //
    {id:9,  region:3, name:"栃木県",   short:"栃木",   kana:"トチギ",       en:"tochigi",   neighbor:[8, 7, 10, 11], data: [{before: 320, after: 329}], dataEmpty: []}, //
    {id:10, region:3, name:"群馬県",   short:"群馬",   kana:"グンマ",       en:"gunma",     neighbor:[7, 9, 11, 15, 20], data: [{before: 370, after: 379}], dataEmpty: []}, //
    {id:11, region:3, name:"埼玉県",   short:"埼玉",   kana:"サイタマ",     en:"saitama",   neighbor:[8, 9, 10, 12, 13, 19, 20], data: [{before: 330, after: 369}], dataEmpty: []}, //
    {id:12, region:3, name:"千葉県",   short:"千葉",   kana:"チバ",         en:"chiba",     neighbor:[8, 11, 13, 14], data: [{before: 260, after: 299}], dataEmpty: [268,269,280,281, 291]},//
    {id:13, region:3, name:"東京都",   short:"東京",   kana:"トウキョウ",   en:"tokyo",     neighbor:[11, 12, 14, 19], data: [{before: 100, after: 302}, {before: 330, after: 369}], dataEmpty: [117, 118,119,122,126,127,128,129,138,139,147,148,149,159,172,199,200,209,217,218,219,268,269,280,281, 291]},
    {id:14, region:3, name:"神奈川県", short:"神奈川", kana:"カナガワ",     en:"kanagawa",  neighbor:[12, 13, 19, 22], data: [{before: 210, after: 259}], dataEmpty: [217,218,219]},
    {id:15, region:4, name:"新潟県",   short:"新潟",   kana:"ニイガタ",     en:"niigata",   neighbor:[6, 7, 10, 16, 20], data: [{before: 940, after: 959}], dataEmpty: []}, //
    {id:16, region:4, name:"富山県",   short:"富山",   kana:"トヤマ",       en:"toyama",    neighbor:[15, 17, 20, 21], data: [{before: 930, after: 939}], dataEmpty: []}, //
    {id:17, region:4, name:"石川県",   short:"石川",   kana:"イシカワ",     en:"ishikawa",  neighbor:[16, 18, 21], data: [{before: 920, after: 929}], dataEmpty: []}, //
    {id:18, region:4, name:"福井県",   short:"福井",   kana:"フクイ",       en:"fukui",     neighbor:[17, 21, 25, 26], data: [{before: 910, after: 919}], dataEmpty: []}, //
    {id:19, region:4, name:"山梨県",   short:"山梨",   kana:"ヤマナシ",     en:"yamanashi", neighbor:[11, 13, 14, 20, 22], data: [{before: 400, after: 409}], dataEmpty: []}, //
    {id:20, region:4, name:"長野県",   short:"長野",   kana:"ナガノ",       en:"nagano",    neighbor:[10, 11, 15, 16, 19, 21, 22, 23], data: [{before: 380, after: 399}], dataEmpty: []}, //
    {id:21, region:4, name:"岐阜県",   short:"岐阜",   kana:"ギフ",         en:"gifu",      neighbor:[16, 17, 18, 20, 23, 24, 25], data: [{before: 443, after: 512}], dataEmpty: [499]},
    {id:22, region:4, name:"静岡県",   short:"静岡",   kana:"シズオカ",     en:"shizuoka",  neighbor:[14, 19, 20, 23], data: [{before: 410, after: 439}], dataEmpty: [423,429]},
    {id:23, region:4, name:"愛知県",   short:"愛知",   kana:"アイチ",       en:"aichi",     neighbor:[20, 21, 22, 24], data: [{before: 440, after: 507}], dataEmpty: [499]},
    {id:24, region:5, name:"三重県",   short:"三重",   kana:"ミエ",         en:"mie",       neighbor:[21, 23, 25, 26, 29, 30], data: [{before: 510, after: 519}], dataEmpty: []}, //
    {id:25, region:5, name:"滋賀県",   short:"滋賀",   kana:"シガ",         en:"shiga",     neighbor:[18, 21, 24, 26], data: [{before: 520, after: 529}, {before: 601, after: 606}], dataEmpty: []}, //
    {id:26, region:5, name:"京都府",   short:"京都",   kana:"キョウト",     en:"kyoto",     neighbor:[18, 24, 25, 27, 28, 29], data: [{before: 600, after: 630}], dataEmpty: [608,609,628]},
    {id:27, region:5, name:"大阪府",   short:"大阪",   kana:"オオサカ",     en:"osaka",     neighbor:[26, 28, 29, 30], data: [{before: 530, after: 618}, {before: 658, after: 666}], dataEmpty: [548,608,609,628]}, //
    {id:28, region:5, name:"兵庫県",   short:"兵庫",   kana:"ヒョウゴ",     en:"hyogo",     neighbor:[26, 27, 30, 31, 33, 36, 37], data: [{before: 650, after: 679}], dataEmpty: []}, //
    {id:29, region:5, name:"奈良県",   short:"奈良",   kana:"ナラ",         en:"nara",      neighbor:[24, 26, 27, 30], data: [{before: 630, after: 639}], dataEmpty: []}, //
    {id:30, region:5, name:"和歌山県", short:"和歌山", kana:"ワカヤマ",     en:"wakayama",  neighbor:[24, 27, 28, 29, 36], data: [{before: 640, after: 649}], dataEmpty: []}, //
    {id:31, region:6, name:"鳥取県",   short:"鳥取",   kana:"トットリ",     en:"tottori",   neighbor:[28, 32, 33, 34], data: [{before: 680, after: 683}], dataEmpty: []}, //
    {id:32, region:6, name:"島根県",   short:"島根",   kana:"シマネ",       en:"shimane",   neighbor:[31, 34, 35], data: [{before: 684, after: 699}], dataEmpty: [686,687,688]},
    {id:33, region:6, name:"岡山県",   short:"岡山",   kana:"オカヤマ",     en:"okayama",   neighbor:[28, 31, 34, 37], data: [{before: 700, after: 719}], dataEmpty: []}, //
    {id:34, region:6, name:"広島県",   short:"広島",   kana:"ヒロシマ",     en:"hiroshima", neighbor:[31, 32, 33, 35, 37, 38], data: [{before: 720, after: 739}], dataEmpty: [724]}, //
    {id:35, region:6, name:"山口県",   short:"山口",   kana:"ヤマグチ",     en:"yamaguchi", neighbor:[32, 34, 38, 40, 44], data: [{before: 740, after: 759}], dataEmpty: [748]}, //
    {id:36, region:7, name:"徳島県",   short:"徳島",   kana:"トクシマ",     en:"tokushima", neighbor:[28, 30, 37, 38, 39], data: [{before: 770, after: 779}], dataEmpty: []}, //
    {id:37, region:7, name:"香川県",   short:"香川",   kana:"カガワ",       en:"kagawa",    neighbor:[28, 33, 34, 36, 38], data: [{before: 760, after: 769}], dataEmpty: []},//
    {id:38, region:7, name:"愛媛県",   short:"愛媛",   kana:"エヒメ",       en:"ehime",     neighbor:[33, 34, 35, 36, 37, 39, 44], data: [{before: 790, after: 799}], dataEmpty: []},
    {id:39, region:7, name:"高知県",   short:"高知",   kana:"コウチ",       en:"kochi",     neighbor:[36, 38], data: [{before: 780, after: 789}], dataEmpty: []},//
    {id:40, region:8, name:"福岡県",   short:"福岡",   kana:"フクオカ",     en:"fukuoka",   neighbor:[35, 41, 42, 43, 44], data: [{before: 800, after: 839}], dataEmpty: []},
    {id:41, region:8, name:"佐賀県",   short:"佐賀",   kana:"サガ",         en:"saga",      neighbor:[40, 42], data: [{before: 840, after: 849}], dataEmpty: []}, //
    {id:42, region:8, name:"長崎県",   short:"長崎",   kana:"ナガサキ",     en:"nagasaki",  neighbor:[41, 43], data: [{before: 850, after: 859}], dataEmpty: []},//
    {id:43, region:8, name:"熊本県",   short:"熊本",   kana:"クマモト",     en:"kumamoto",  neighbor:[40, 41, 42, 44, 45, 46], data: [{before: 860, after: 869}], dataEmpty: []},//
    {id:44, region:8, name:"大分県",   short:"大分",   kana:"オオイタ",     en:"oita",      neighbor:[35, 38, 40, 43, 45], data: [{before: 870, after: 879}], dataEmpty: []},//
    {id:45, region:8, name:"宮崎県",   short:"宮崎",   kana:"ミヤザキ",     en:"miyazaki",  neighbor:[43, 44, 46], data: [{before: 880, after: 889}], dataEmpty: []},//
    {id:46, region:8, name:"鹿児島県", short:"鹿児島", kana:"カゴシマ",     en:"kagoshima", neighbor:[43, 45, 47], data: [{before: 890, after: 899}], dataEmpty: []},//
    {id:47, region:8, name:"沖縄県",   short:"沖縄",   kana:"オキナワ",     en:"okinawa",   neighbor:[46], data: [{before: 900, after: 907}], dataEmpty: []}//
];

exports.getAddressByZip = function(code, callback) {
    if(code.includes("-")) {
        code = code.replace("-", "")
    }
    if(code.length > 7) {
        callback({
            status: 404,
            region: '',
            prefecture:'',
            city:  '',
            area:  '',
            street:  ''
        })
    } else if (code.length < 7) {
        callback({
            status: 404,
            region: '',
            prefecture:'',
            city:  '',
            area:  '',
            street:  ''
        })
    }
    let prefCode = code.substring(0, 3)
    const data = require(`./zipdata/zip-${prefCode}`) || {}
    try {
        const [ prefId, city, area, street ] = data[code]
        const prefectureData = prefectures.find(prefecture => {
            if(prefecture.id === prefId) {
                return prefecture
            }
        })
        const region = regions.find(region => {
            if(region.id === prefectureData.region) {
                return region
            }
        })
        callback({
            status: 200,
            region: region.name,
            prefecture: prefectureData.name,
            city: city || '',
            area: area || '',
            street: street || ''
        })
    } catch (err) {
        callback({
            status: 404,
            region: '',
            prefecture:'',
            city:  '',
            area:  '',
            street:  ''
        })
    }
}

function getListDataLocal(dataLocal, dataEmpty){
    let listDataLocal = []
    let listDataEmpty = []
    dataEmpty.forEach(elm => {
        let textResult = ''
        if(elm.toString().length <3){
            if(elm.toString().length ===1) {
                textResult = '00' + elm.toString()
            }else{
                textResult = '0'+ elm.toString()
            }
        }else{
            textResult = elm.toString()
        }
        listDataEmpty.push(textResult)
    })
    dataLocal.map(async elm => {
        for(let i = elm.before ; i <= elm.after; i++){
            let itemCheckList = ''
            if(i.toString().length <3){
                if(i.toString().length ===1) {
                    itemCheckList = '00' + i.toString()
                }else{
                    itemCheckList = '0'+ i.toString()
                }
            }else{
                itemCheckList = i.toString()
            }
            if(!listDataEmpty.includes(itemCheckList)){
                let data = require(`./zipdata/zip-${itemCheckList}`)

                if(data && Object.keys(data).length > 0){
                    Object.keys(data).map(keyCode => {
                        let dataAddress = data[keyCode]
                        let item = {
                            postCode : keyCode,
                            city: dataAddress[1],
                            area: dataAddress[2] || '',
                            street: dataAddress[3] || '',
                        }
                        listDataLocal.push(item)
                    })
                }
            }

        }
    })
    return listDataLocal
}

exports.getPostCodeByAddress = async function (address, callback){
    let result = ''
    if(address){
        let textPrefecture = address.slice(0,4) || ''
        let findPrefecture = prefectures.find(elm => textPrefecture.includes(elm.name))
        if(findPrefecture){
            let dataLocal = findPrefecture.data || []
            let dataEmpty = findPrefecture.dataEmpty || []
            let listDataLocal = await getListDataLocal(dataLocal,dataEmpty) || []
            let addressFind = listDataLocal.filter(elm =>
                {
                    let addressElm = elm.city.trim()+ elm.area.trim()+ elm.street.trim()

                    return address.includes(addressElm)
                })
            if(addressFind.length > 0){
                let addressResult = addressFind[addressFind.length - 1]
                result  = addressResult.postCode || ''
            }else{
                result = ''
            }
        }
    }
    callback({
        status: 200,
        postcode: result
    })
}

exports.getPostCodeByPrefecture = async function (address, callback){
    let result = ''
    if(address){
        let findPrefecture = prefectures.find(elm => elm.name === address.prefecture)
        if(findPrefecture){
            let dataLocal = findPrefecture.data || []
            let dataEmpty = findPrefecture.dataEmpty || []
            let listDataLocal = await getListDataLocal(dataLocal,dataEmpty) || []
            let addressFind = listDataLocal.filter(elm =>
            {
                return address.city.trim() === elm.city.trim() && address.area.trim() === elm.area.trim() && address.street.trim() === elm.street.trim()
            })
            if(addressFind.length > 0){
                let addressResult = addressFind[addressFind.length - 1]
                result  = addressResult.postCode || ''
            }else{
                result = ''
            }
        }
    }
    callback({
        status: 200,
        postcode: result
    })
}

exports.getAllPrefectures = function () {
    return prefectures
}

exports.getAllRegions = function () {
    return regions
}


