package com.Tricot.calcutator.Calculator1pager.Calculator1;

import android.content.Intent;
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.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

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

import com.Tricot.calcutator.CalculatorMenu.Bar_select;
import com.Tricot.calcutator.R;
import com.Tricot.calcutator.data.Calculation1_pager;

public class Calcul12pager extends Fragment {
    ImageView FinishBtn;
    EditText A_gear;
    EditText B_gear;
    private boolean isFirstSelected = true; // 전역변수로 선언
    public Calcul12pager(){

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.content_calculator_1_12, container, false);
        return rootView;
    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        A_gear = view.findViewById(R.id.A_gear);
        B_gear = view.findViewById(R.id.B_gear);
        FinishBtn=view.findViewById(R.id.backImg);
        FinishBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        Spinner spinner = (Spinner)view.findViewById(R.id.spinner);
        spinner.setSelection(11);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("-------------------------");
                System.out.println(position);
                System.out.println("-------------------------");

                if(isFirstSelected){
                    isFirstSelected=false;
                }else{
                    if(position==0){
                        Intent intent = new Intent(getActivity(), Bar_select.class);
                        intent.putExtra("menu",1);
                        intent.putExtra("pages",0);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==1){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",1);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==2){
                        Intent intent = new Intent(getActivity(), Bar_select.class);
                        intent.putExtra("pages",2);
                        intent.putExtra("menu",1);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==3){
                        Intent intent = new Intent(getActivity(), Bar_select.class);
                        intent.putExtra("pages",3);
                        intent.putExtra("menu",1);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==4){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",4);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==5){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",5);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==6){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",6);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==7){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",7);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==8){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",8);
                        startActivity(intent);
                        getActivity().finish();
                    }
                    else if(position==9){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",9);
                        startActivity(intent);
                        getActivity().finish();
                    }
                    else if(position==10){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",10);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==11){
                        Intent intent = new Intent(getActivity(), Calculation1_pager.class);
                        intent.putExtra("pages",11);
                        startActivity(intent);
                        getActivity().finish();
                    }
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        A_gear.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) { }
            @Override
            public void afterTextChanged(Editable s) {
                if(A_gear.getText().toString().length()!=0&&B_gear.getText().toString().length()!=0){
                    ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                }
            }
        });
        B_gear.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) { }
            @Override
            public void afterTextChanged(Editable s) {
                if(A_gear.getText().toString().length()!=0&&B_gear.getText().toString().length()!=0){
                    ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                }
            }
        });
    }
}
