package info.knacki.pass.ui.alertPrompt.views;

import android.content.Context;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import info.knacki.pass.R;
import info.knacki.pass.ui.alertPrompt.AlertPrompt;

public class ConflictView extends HorizontalScrollView {
    protected final HashMap<String, ConflictState> fConflicts;
    protected final AlertPrompt fDialog;

    private class ConflictLine extends LinearLayout {
        public final RadioButton useMineButton;
        public final RadioButton useTheirButton;

        public ConflictLine(Context context, String text) {
            super(context);
            setOrientation(VERTICAL);

            TextView label = new TextView(context);
            label.setText(text);
            addView(label);

            LinearLayout controls = new LinearLayout(context);
            controls.setOrientation(LinearLayout.HORIZONTAL);
            RadioGroup input = new RadioGroup(context);
            input.setOrientation(LinearLayout.HORIZONTAL);
            useMineButton = new RadioButton(context);
            useTheirButton = new RadioButton(context);
            input.addView(useMineButton);
            input.addView(useTheirButton);
            useMineButton.setText(R.string.useMine);
            useTheirButton.setText(R.string.useTheir);
            controls.addView(input);
            addView(controls);
        }
    }

    public ConflictView(Context context, AlertPrompt parentDialog, Set<String> conflicts) {
        super(context);
        fConflicts = new HashMap<>();
        fDialog = parentDialog;
        LinearLayout rootLayout = new LinearLayout(context);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        rootLayout.setPadding(15, 0, 15, 0);
        addView(rootLayout);
        for (String i: conflicts) {
            ConflictLine uiLine = new ConflictLine(context, i);
            createListeners(uiLine.useMineButton, uiLine.useTheirButton, i);
            rootLayout.addView(uiLine);
        }
    }

    private void createListeners(RadioButton mine, RadioButton their, final String name) {
        fConflicts.put(name, new ConflictState());
        mine.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                // name added just before
                //noinspection ConstantConditions
                fConflicts.get(name).UseMine();
                UpdateButtonState();
            }
        });
        their.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                // name added just before
                //noinspection ConstantConditions
                fConflicts.get(name).UseTheir();
                UpdateButtonState();
            }
        });
    }

    public ConflictViewResult GetResult() {
        ConflictViewResult res = new ConflictViewResult(new HashSet<>(), new HashSet<>());
        for (Map.Entry<String, ConflictState> i: fConflicts.entrySet()) {
            if (i.getValue().fUseMine)
                res.fUseMine.add(i.getKey());
            if (i.getValue().fUseTheir)
                res.fUseTheir.add(i.getKey());
        }
        return res;
    }

    public boolean UpdateButtonState() {
        for (ConflictState i : fConflicts.values()) {
            if (!i.IsValid()) {
                fDialog.setPositiveButtonEnabled(false);
                return false;
            }
        }
        fDialog.setPositiveButtonEnabled(true);
        return true;
    }

    private class ConflictState {
        public boolean fUseTheir = false;
        public boolean fUseMine = false;

        void UseTheir() {
            fUseTheir = true;
            fUseMine = false;
        }

        void UseMine() {
            fUseTheir = false;
            fUseMine = true;
        }

        public boolean IsValid() {
            return fUseTheir || fUseMine;
        }
    }

    public class ConflictViewResult {
        public final Set<String> fUseMine;
        public final Set<String> fUseTheir;

        ConflictViewResult(Set<String> useMine, Set<String> useTheir) {
            fUseMine = useMine;
            fUseTheir = useTheir;
        }
    }
}
