package com.tsi.navi.activity.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Checkable;
import android.widget.CheckedTextView;
import android.widget.Spinner;

import androidx.annotation.NonNull;

import com.naver.maps.map.NaverMap;
import com.tsi.navi.R;
import com.tsi.navi.TsiNaviApplication;
import com.tsi.navi.util.BoundaryUtils;

import org.jetbrains.annotations.NotNull;

public class MapSettingDialog  extends BaseDialog {

    private NaverMap naverMap;
    private BoundaryUtils mapUtils;

    public MapSettingDialog(@NonNull Context context, @NonNull IDialogClickListener clickListener, @NotNull NaverMap naverMap, @NotNull BoundaryUtils mapUtils) {
        super(context, clickListener);
        this.naverMap = naverMap;
        this.mapUtils = mapUtils;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_map_setting);
        setupButton(R.string.dialog_title_map_settings, false);

        setupMapSetting();
    }

    public void setupMapSetting() {
        // 지도 유형
        ArrayAdapter<CharSequence> adapterType = ArrayAdapter.createFromResource(context, R.array.map_types, android.R.layout.simple_spinner_item);
        adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner spinner = findViewById(R.id.map_types);
        spinner.setAdapter(adapterType);
        spinner.setSelection(adapterType.getPosition(TsiNaviApplication.getMapConfig().getType()));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                CharSequence mapType = adapterType.getItem(position);
                if (mapType != null) {
                    naverMap.setMapType(NaverMap.MapType.valueOf(mapType.toString()));
                    TsiNaviApplication.getMapConfig().setType(mapType.toString());
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        // 지도레벨
        ArrayAdapter<CharSequence> adapterRunZoom = ArrayAdapter.createFromResource(context, R.array.map_run_zoom, android.R.layout.simple_spinner_item);
        adapterRunZoom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner spinnerRunZoom = findViewById(R.id.map_run_zoom_level);
        spinnerRunZoom.setAdapter(adapterRunZoom);
        spinnerRunZoom.setSelection(adapterRunZoom.getPosition(String.valueOf(TsiNaviApplication.getMapConfig().getTrackingZoom())));
        spinnerRunZoom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                CharSequence mapZoom = adapterRunZoom.getItem(position);
                if (mapZoom != null) {
                    TsiNaviApplication.getMapConfig().setTrackingZoom((Integer.parseInt(mapZoom.toString())));
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        // 야간 모드
        CheckedTextView nightMode = findViewById(R.id.toggle_night_mode);
        if (nightMode != null) {
            nightMode.setChecked(TsiNaviApplication.getMapConfig().isNightMode());
            nightMode.setOnClickListener(v -> {
                Checkable checkable = (Checkable)v;
                boolean checked = !checkable.isChecked();
                checkable.setChecked(checked);
                naverMap.setNightModeEnabled(checked);
                TsiNaviApplication.getMapConfig().setNightMode(checked);
                //naverMap.setBackgroundColor(TsiNaviApplication.getMapConfig().isNightMode() ? NaverMap.DEFAULT_BACKGROUND_COLOR_DARK : NaverMap.DEFAULT_BACKGROUND_COLOR_LIGHT);
                naverMap.setBackgroundResource(TsiNaviApplication.getMapConfig().isNightMode() ? NaverMap.DEFAULT_BACKGROUND_DRWABLE_DARK : NaverMap.DEFAULT_BACKGROUND_DRWABLE_LIGHT);
            });
        }
    }
}
