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 Calcul9pager extends Fragment {
    ImageView FinishBtn;
    EditText Raw_weight;
    EditText Shearing_loss;
    EditText Processing_axis;
    private boolean isFirstSelected = true; // 전역변수로 선언
    public Calcul9pager(){

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.content_calculator_1_9, container, false);
        return rootView;
    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        Raw_weight = view.findViewById(R.id.Raw_weight);
        Shearing_loss = view.findViewById(R.id.Shearing_loss);
        Processing_axis = view.findViewById(R.id.Processing_axis);
        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(8);
        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) {

            }
        });

        Raw_weight.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(Raw_weight.getText().toString().length()!=0&&Shearing_loss.getText().toString().length()!=0&&Processing_axis.getText().toString().length()!=0){
                    ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                }
            }
        });
        Shearing_loss.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(Raw_weight.getText().toString().length()!=0&&Shearing_loss.getText().toString().length()!=0&&Processing_axis.getText().toString().length()!=0){
                    ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                }
            }
        });
        Processing_axis.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(Raw_weight.getText().toString().length()!=0&&Shearing_loss.getText().toString().length()!=0&&Processing_axis.getText().toString().length()!=0){
                    ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                }
            }
        });
    }

}
