/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gnd.ui.settings;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceFragmentCompat;
import com.google.android.gnd.MainActivity;
import com.google.android.gnd.R;
import com.google.android.gnd.databinding.SettingsFragBinding;

/**
 * Fragment containing app preferences saved as shared preferences.
 *
 * <p>NOTE: It uses {@link PreferenceFragmentCompat} instead of {@link
 * com.google.android.gnd.ui.common.AbstractFragment}, so dagger can't inject into it like it does
 * in other fragments.
 *
 * <p>TODO: Create dagger module and support injection into this fragment.
 */
public class SettingsFragment extends PreferenceFragmentCompat
    implements OnPreferenceChangeListener, OnPreferenceClickListener {

  @Override
  public View onCreateView(
      @NonNull LayoutInflater inflater,
      @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    SettingsFragBinding binding = SettingsFragBinding.inflate(inflater, container, true);
    binding.setLifecycleOwner(this);
    ((MainActivity) getActivity()).setActionBar(binding.settingsToolbar, true);
    return super.onCreateView(inflater, container, savedInstanceState);
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // TODO: Set dynamically by calculating the actual height of toolbar
    int topMargin = 108;

    LayoutParams params = (LayoutParams) getListView().getLayoutParams();
    params.setMargins(0, topMargin, 0, 0);
    getListView().setLayoutParams(params);
  }

  @Override
  public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.preferences, rootKey);
    for (String key : Keys.ALL_KEYS) {
      Preference preference = findPreference(key);
      if (preference == null) {
        throw new IllegalArgumentException("Key not found in preferences.xml: " + key);
      }

      preference.setOnPreferenceChangeListener(this);
      preference.setOnPreferenceClickListener(this);
    }
  }

  @Override
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    switch (preference.getKey()) {
      case Keys.UPLOAD_MEDIA:
      case Keys.OFFLINE_AREAS:
        Toast.makeText(getContext(), "Not yet implemented", Toast.LENGTH_SHORT).show();
        break;
    }
    return true;
  }

  @Override
  public boolean onPreferenceClick(Preference preference) {
    switch (preference.getKey()) {
      case Keys.VISIT_WEBSITE:
        openUrl(preference.getSummary().toString());
        break;
      case Keys.FEEDBACK:
        Toast.makeText(getContext(), "Not yet implemented", Toast.LENGTH_SHORT).show();
        break;
      default:
        return false;
    }
    return true;
  }

  private void openUrl(String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    startActivity(intent);
  }
}

@@ -3506,24 +3506,54 @@
      break;%0A
+      default:%0A        break;%0A
     %7D%0A    re

Add missing default