package de.apps4ics.mountainnavigation;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.preference.DialogPreference;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.LinearLayout;

/**
 * This file is part of MountainNavigation.
 *
 * MountainNavigation is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MountainNavigation is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MountainNavigation.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright Copyright (c) 2016 Vinzenz Rosenkanz <vinzenz.rosenkranz@gmail.com>
 *
 * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
 */
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener {
    private Context context;
    private int maxValue;
    private int minValue;
    private int currentValue;
    private int defaultStart;
    private boolean isDays;
    private String text;

    private SeekBar seekBar;
    private TextView infoText;

    private final static int DEFAULT_MAX_VALUE = 100;
    private final static int DEFAULT_MIN_VALUE = 0;
    private final static String DEFAULT_VALUE = "defaultValue";
    private static final String AND_NAMESPACE = "http://schemas.android.com/apk/res/android";

    public SeekBarPreference(Context context) {
        super(context, null);
    }

    public SeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.defaultStart = attrs.getAttributeIntValue(AND_NAMESPACE, DEFAULT_VALUE, DEFAULT_MIN_VALUE);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MinSeekBar);
        this.text = ta.getString(R.styleable.MinSeekBar_android_text);
        this.maxValue = ta.getInt(R.styleable.MinSeekBar_android_max, DEFAULT_MAX_VALUE);
        this.minValue = ta.getInt(R.styleable.MinSeekBar_min, DEFAULT_MIN_VALUE);
        this.isDays = ta.getBoolean(R.styleable.MinSeekBar_isdays, false);
    }

    private String getInfoText(int value) {
        if(isDays) {
            return MainActivity.getRes().getQuantityString(R.plurals.number_of_days_plurals, value, value);
        } else {
            return String.format(text, value);
        }
    }

    @Override
    protected View onCreateDialogView() {
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(15, 15, 15, 15);

        infoText = new TextView(context);
        if(text != null) {
            infoText.setText(getInfoText(defaultStart + minValue));
        }
        infoText.setGravity(Gravity.CENTER_HORIZONTAL);
        infoText.setTextSize(32);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(infoText, params);

        seekBar = new SeekBar(context);
        if(shouldPersist()) currentValue = getPersistedInt(defaultStart);
        seekBar.setMax(maxValue);
        seekBar.setProgress(currentValue + minValue);
        seekBar.setOnSeekBarChangeListener(this);
        layout.addView(seekBar, params);
        return layout;
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        super.onSetInitialValue(restorePersistedValue, defaultValue);
        if(restorePersistedValue) {
            if(shouldPersist()) {
                currentValue = getPersistedInt(defaultStart);
            } else {
                currentValue = 0;
            }
        } else {
            currentValue = (int) defaultValue;
        }
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);
        seekBar.setMax(maxValue);
        seekBar.setProgress(currentValue);
        infoText.setText(getInfoText(currentValue + minValue));
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        infoText.setText(getInfoText(progress + minValue));
        if(shouldPersist()) persistInt(progress);
        callChangeListener(progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}