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 android.widget.TextView;

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;

import java.text.DecimalFormat;

public class Calcul11pager extends Fragment {
    ImageView FinishBtn;
    EditText a21;
    EditText b21;
    EditText a30;
    EditText b30;
    EditText a40;
    EditText b40;
    EditText an21;
    EditText bn21;
    EditText an30;
    EditText bn30;
    EditText an40;
    EditText bn40;
    TextView rack21;
    TextView rack30;
    TextView rack40;
    TextView rackn21;
    TextView rackn30;
    TextView rackn40;
    private boolean isFirstSelected = true; // 전역변수로 선언
    DecimalFormat myFormatter = new DecimalFormat("###,###");
    public Calcul11pager(){

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.content_calculator_1_11, container, false);
        return rootView;
    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        a21 = view.findViewById(R.id.a21);
        b21 = view.findViewById(R.id.b21);
        a30 = view.findViewById(R.id.a30);
        b30 = view.findViewById(R.id.b30);
        a40 = view.findViewById(R.id.a40);
        b40 = view.findViewById(R.id.b40);
        an21 = view.findViewById(R.id.an21);
        bn21 = view.findViewById(R.id.bn21);
        an30 = view.findViewById(R.id.an30);
        bn30 = view.findViewById(R.id.bn30);
        an40 = view.findViewById(R.id.an40);
        bn40 = view.findViewById(R.id.bn40);
        rack21 = view.findViewById(R.id.rack21);
        rack30 = view.findViewById(R.id.rack30);
        rack40 = view.findViewById(R.id.rack40);
        rackn21 = view.findViewById(R.id.rackn21);
        rackn30 = view.findViewById(R.id.rackn30);
        rackn40 = view.findViewById(R.id.rackn40);
        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(10);
        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) {

            }
        });

        a21.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(a21.getText().toString().length()!=0&&b21.getText().toString().length()!=0){
                    try {
                        float a21s = Float.parseFloat(a21.getText().toString());
                        float b21s = Float.parseFloat(b21.getText().toString());
                        double calcul1 = a21s / b21s * 626.1;
                        double calculround1 = Math.round(calcul1);
                        int calculch1 = (int) calculround1;
                        String formattedStringPrice = myFormatter.format(calculch1);
                        rack21.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        b21.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(a21.getText().toString().length()!=0&&b21.getText().toString().length()!=0){
                    try {
                        float a21s = Float.parseFloat(a21.getText().toString());
                        float b21s = Float.parseFloat(b21.getText().toString());
                        double calcul1 = a21s / b21s * 626.1;
                        double calculround1 = Math.round(calcul1);
                        int calculch1 = (int) calculround1;
                        String formattedStringPrice = myFormatter.format(calculch1);
                        rack21.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        a30.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(a30.getText().toString().length()!=0&&b30.getText().toString().length()!=0){
                    try {
                        float a30s = Float.parseFloat(a30.getText().toString());
                        float b30s = Float.parseFloat(b30.getText().toString());
                        double calcul2 = a30s / b30s * 834.9;
                        double calculround2 = Math.round(calcul2);
                        int calculch2 = (int) calculround2;
                        String formattedStringPrice = myFormatter.format(calculch2);
                        rack30.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        b30.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(a30.getText().toString().length()!=0&&b30.getText().toString().length()!=0){
                    try {
                        float a30s = Float.parseFloat(a30.getText().toString());
                        float b30s = Float.parseFloat(b30.getText().toString());
                        double calcul2 = a30s / b30s * 834.9;
                        double calculround2 = Math.round(calcul2);
                        int calculch2 = (int) calculround2;
                        String formattedStringPrice = myFormatter.format(calculch2);
                        rack30.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        a40.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(a40.getText().toString().length()!=0&&a40.getText().toString().length()!=0){
                    try {
                        float a40s = Float.parseFloat(a40.getText().toString());
                        float b40s = Float.parseFloat(a40.getText().toString());
                        double calcul3 = a40s / b40s * 1240;
                        double calculround3 = Math.round(calcul3);
                        int calculch3 = (int) calculround3;
                        String formattedStringPrice = myFormatter.format(calculch3);
                        rack40.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        b40.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(a40.getText().toString().length()!=0&&a40.getText().toString().length()!=0){
                    try {
                        float a40s = Float.parseFloat(a40.getText().toString());
                        float b40s = Float.parseFloat(a40.getText().toString());
                        double calcul3 = a40s / b40s * 1240;
                        double calculround3 = Math.round(calcul3);
                        int calculch3 = (int) calculround3;
                        String formattedStringPrice = myFormatter.format(calculch3);
                        rack40.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        an21.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(an21.getText().toString().length()!=0&&bn21.getText().toString().length()!=0){
                    try {
                        float a21 = Float.parseFloat(an21.getText().toString());
                        float b21 = Float.parseFloat(bn21.getText().toString());
                        double calcul1 = a21 / b21 * 1408.8;
                        double calculround1 = Math.round(calcul1);
                        int calculch1 = (int) calculround1;
                        String formattedStringPrice = myFormatter.format(calculch1);
                        rackn21.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        bn21.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(an21.getText().toString().length()!=0&&bn21.getText().toString().length()!=0){
                    try {
                        float a21 = Float.parseFloat(an21.getText().toString());
                        float b21 = Float.parseFloat(bn21.getText().toString());
                        double calcul1 = a21 / b21 * 1408.8;
                        double calculround1 = Math.round(calcul1);
                        int calculch1 = (int) calculround1;
                        String formattedStringPrice = myFormatter.format(calculch1);
                        rackn21.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        an30.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(an30.getText().toString().length()!=0&&bn30.getText().toString().length()!=0){
                    try {
                        float a30 = Float.parseFloat(an30.getText().toString());
                        float b30 = Float.parseFloat(bn30.getText().toString());
                        double calcul2 = a30 / b30 * 1878.4;
                        double calculround2 = Math.round(calcul2);
                        int calculch2 = (int) calculround2;
                        String formattedStringPrice = myFormatter.format(calculch2);
                        rackn30.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        bn30.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(an30.getText().toString().length()!=0&&bn30.getText().toString().length()!=0){
                    try {
                        float a30 = Float.parseFloat(an30.getText().toString());
                        float b30 = Float.parseFloat(bn30.getText().toString());
                        double calcul2 = a30 / b30 * 1878.4;
                        double calculround2 = Math.round(calcul2);
                        int calculch2 = (int) calculround2;
                        String formattedStringPrice = myFormatter.format(calculch2);
                        rackn30.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        an40.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(an40.getText().toString().length()!=0&&bn40.getText().toString().length()!=0){
                    try {
                        float a40 = Float.parseFloat(an40.getText().toString());
                        float b40 = Float.parseFloat(bn40.getText().toString());
                        double calcul3 = a40 / b40 * 2780;
                        double calculround3 = Math.round(calcul3);
                        int calculch3 = (int) calculround3;
                        String formattedStringPrice = myFormatter.format(calculch3);
                        rackn40.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
        bn40.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(an40.getText().toString().length()!=0&&bn40.getText().toString().length()!=0){
                    try {
                        float a40 = Float.parseFloat(an40.getText().toString());
                        float b40 = Float.parseFloat(bn40.getText().toString());
                        double calcul3 = a40 / b40 * 2780;
                        double calculround3 = Math.round(calcul3);
                        String formattedStringPrice = myFormatter.format(calculround3);
                        rackn40.setText(formattedStringPrice + "R/mm");
                    } catch (NumberFormatException e) {
                    }
                }
            }
        });
    }
}
