package com.ryandejesus.zapposweatherapp.Fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ryandejesus.zapposweatherapp.R;

/**
 * Created by ryandejesus on 9/25/14.
 */
public class BaseWeatherFragment extends Fragment{

    protected View rootView;
    protected TextView vCityName;
    protected TextView vTemperature;
    protected TextView vHighTemp;
    protected TextView vLowTemp;
    protected TextView vChanceOfRain;
    protected ListView lForecast;
    protected ProgressBar progressBar;
    protected ImageView vCurrentConditions;
    protected ImageView vUpArrowCurrentConditionHigh;
    protected ImageView vDownArrowCurrentConditionLow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.weather_fragment, container, false);
        progressBar = (ProgressBar) rootView.findViewById(R.id.weather_progress_bar);
        vCityName = (TextView) rootView.findViewById(R.id.city_name);
        vTemperature = (TextView) rootView.findViewById(R.id.temperature);
        vCurrentConditions = (ImageView) rootView.findViewById(R.id.weather_conditions);
        vHighTemp = (TextView) rootView.findViewById(R.id.temperature_range_high_value);
        vLowTemp = (TextView) rootView.findViewById(R.id.temperature_range_low_down);
        vUpArrowCurrentConditionHigh = (ImageView) rootView.findViewById(R.id.temperature_range_high_up_arrow);
        vDownArrowCurrentConditionLow = (ImageView) rootView.findViewById(R.id.temperature_range_low_down_arrow);

        configureWeatherForecastListView();

        setListeners();

        return rootView;
    }

    private void configureWeatherForecastListView(){
        lForecast = (ListView) rootView.findViewById(R.id.forecast_list_view);
        lForecast.setDivider(null);
        lForecast.setClickable(false);
    }

    private void setListeners(){
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                hideKeyBoard(view);
                return false;
            }
        });

        lForecast.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                hideKeyBoard(view);
                return false;
            }
        });

    }

    private void hideKeyBoard(View currentFocus){
        InputMethodManager imm = (InputMethodManager) getActivity().getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
    }

    @Override
    public void onStart(){
        super.onStart();
    }

    public void hideWeatherView(){
        if(rootView != null)
            rootView.setVisibility(View.INVISIBLE);
    }

    public void saveConditions(){
        Log.i("BaseWeatherFragment", "saveConditions()");
        // Retrieve the applications SharedPreference file to save to
        SharedPreferences tempSP = getActivity().getApplicationContext().getSharedPreferences("zapposweatherapp", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = tempSP.edit();

        editor.putString("vTemperature", vTemperature.getText().toString());
        editor.putString("vCityName", vCityName.getText().toString());
        editor.putString("vHighTemp", vHighTemp.getText().toString());
        editor.commit();
    }

    public void displayWeatherView(){
        rootView.setVisibility(View.VISIBLE);
    }

    public void showProgressBar(){
        progressBar.setVisibility(View.VISIBLE);
    }

    public void hideProgressBar(){
        progressBar.setVisibility(View.GONE);
    }
}
