package info.knacki.pass.generator.ui;

import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

import info.knacki.pass.R;
import info.knacki.pass.generator.PasswordGenerator;
import info.knacki.pass.ui.widget.Checkbox;

public class PasswordGeneratorWizard extends LinearLayout implements PasswordGenerator.PasswordGeneratorParams {
    public final AppCompatSpinner fDifficulty;
    public final Checkbox fAlpha;
    public final Checkbox fCapitalize;
    public final Checkbox fNum;
    public final Checkbox fSpecial;
    public final NumberPicker fLength;

    public static final int PRESET_NUM =0;
    public static final int PRESET_WEAK =1;
    public static final int PRESET_AVG =2;
    public static final int PRESET_STRONG =3;

    @Override
    public boolean hasNumbers() {
        return fNum.isChecked();
    }

    @Override
    public boolean hasLetters() {
        return fAlpha.isChecked();
    }

    @Override
    public boolean hasCapitalize() {
        return fCapitalize.isChecked();
    }

    @Override
    public boolean hasSpecialChar() {
        return fSpecial.isChecked();
    }

    @Override
    public int length() {
        return fLength.getValue();
    }

    private class ChangeListener implements Checkbox.OnCheckedChangeListener, NumberPicker.OnValueChangeListener {
        private boolean locked = true;

        @Override
        public void onCheckedChanged() {
            if (!locked)
                fDifficulty.setSelection(4, true);
        }

        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int i1) {
            if (!locked)
                fDifficulty.setSelection(4, true);
        }

        void setLocked(boolean val) {
            locked = val;
        }
    }
    protected final ChangeListener fChangeListener = new ChangeListener();

    public PasswordGeneratorWizard(Context c) {
        super(c);

        LayoutInflater.from(c).inflate(R.layout.password_generator_wizard, this, true);

        ArrayAdapter fDifficultyAdapter = new ArrayAdapter<>(c, android.R.layout.simple_spinner_item, c.getResources().getStringArray(R.array.fDifficulty));
        fDifficultyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        fDifficulty = findViewById(R.id.difficulty);
        fDifficulty.setAdapter(fDifficultyAdapter);
        fDifficulty.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                SelectPreset(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });

        fNum = ((Checkbox) findViewById(R.id.generate_numbers)).SetOnCheckedChangeListener(fChangeListener);
        fAlpha = ((Checkbox) findViewById(R.id.generate_alpha)).SetOnCheckedChangeListener(fChangeListener);
        fCapitalize = ((Checkbox) findViewById(R.id.generate_capitalize)).SetOnCheckedChangeListener(fChangeListener);
        fSpecial = ((Checkbox) findViewById(R.id.generate_special)).SetOnCheckedChangeListener(fChangeListener);

        fLength = findViewById(R.id.length);
        fLength.setMinValue(4);
        fLength.setMaxValue(21);
        fLength.setOnValueChangedListener(fChangeListener);

        fDifficulty.setSelection(2, false);
        fChangeListener.setLocked(false);
    }

    protected void SelectPreset(int pos) {
        fChangeListener.setLocked(true);
        switch (pos) {
            case PRESET_NUM:
                fAlpha.setChecked(false);
                fCapitalize.setChecked(false);
                fNum.setChecked(true);
                fSpecial.setChecked(false);
                fLength.setValue(4);
                break;

            case PRESET_WEAK:
                fAlpha.setChecked(true);
                fCapitalize.setChecked(false);
                fNum.setChecked(true);
                fSpecial.setChecked(false);
                fLength.setValue(8);
                break;

            case PRESET_AVG:
                fAlpha.setChecked(true);
                fCapitalize.setChecked(true);
                fNum.setChecked(true);
                fSpecial.setChecked(true);
                fLength.setValue(8);
                break;

            case PRESET_STRONG:
                fAlpha.setChecked(true);
                fCapitalize.setChecked(true);
                fNum.setChecked(true);
                fSpecial.setChecked(true);
                fLength.setValue(12);
                break;
        }
        fChangeListener.setLocked(false);
    }
}
