package com.example.generator;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Tab1 extends Fragment {

    private static final String ARG_POSITION = "tab_position";

    public Tab1() {
        // Required empty public constructor
    }

    public static Tab1 newInstance(int position) {
        Tab1 fragment = new Tab1();
        Bundle args = new Bundle();
        args.putInt(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);

        int tabPosition = getArguments() != null ? getArguments().getInt(ARG_POSITION, 0) : 0;

        TextView elementTitle = view.findViewById(R.id.text1);
        elementTitle.setText("Element " + (tabPosition + 1));

        EditText numOfButton = view.findViewById(R.id.numOfButton);
        View[] objectives = {
                view.findViewById(R.id.toAdd),
                view.findViewById(R.id.toAdd2),
                view.findViewById(R.id.toAdd3),
                view.findViewById(R.id.toAdd4),
                view.findViewById(R.id.toAdd5),
                view.findViewById(R.id.toAdd6),
                view.findViewById(R.id.toAdd7),
                view.findViewById(R.id.toAdd8),
                view.findViewById(R.id.toAdd9)
        };

        numOfButton.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String input = s.toString();
                EventsActivity.Data.setNumScoreOb(input);

                int score = input.isEmpty() ? 0 : Integer.parseInt(input);

                for (int o = 0; o < objectives.length; o++) {
                    objectives[o].setVisibility(o < score ? View.VISIBLE : View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });

        return view;
    }
}