package com.Tricot.calcutator.Calculator2pager.Calculator2;

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.Calculation2_pager;

import java.text.DecimalFormat;

public class Calcul2_4pager extends Fragment {
    ImageView FinishBtn;
    EditText weight;
    EditText totalyarn_denier;
    TextView totalyarn_length1;
    EditText totalyarn_weight2;
    EditText totalyarn_dtex;
    EditText beamWcount1;
    EditText beamWcount2;
    TextView beamWlength1;
    TextView beamWlength2;
    TextView totalyarn_length2;
    String formattedStringPrice;
    int saveNum;
    int saveNum2;
    private boolean isFirstSelected = true; // 전역변수로 선언
    public Calcul2_4pager(){
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_calculator_2_4, container, false);
        return rootView;
    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        weight = view.findViewById(R.id.totalyarn_weight1);
        totalyarn_denier = view.findViewById(R.id.totalyarn_denier);
        totalyarn_length1 = view.findViewById(R.id.totalyarn_length1);
        totalyarn_weight2 = view.findViewById(R.id.totalyarn_weight2);
        totalyarn_dtex = view.findViewById(R.id.totalyarn_dtex);
        totalyarn_length2 = view.findViewById(R.id.totalyarn_length2);
        beamWcount1=view.findViewById(R.id.Beam_w_count);
        beamWcount2=view.findViewById(R.id.Beam_w_count2);
        beamWlength1=view.findViewById(R.id.Beam_W_length);
        beamWlength2=view.findViewById(R.id.Beam_W_length2);
        FinishBtn=view.findViewById(R.id.backImg);
        FinishBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        weight.requestFocus();

        Spinner spinner = (Spinner)view.findViewById(R.id.spinner);
        spinner.setSelection(3);
        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",2);
                        intent.putExtra("pages",0);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==1){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",1);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==2){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",2);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==3){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",3);
                        startActivity(intent);
                    }else if(position==4){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",4);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==5){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",5);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==6){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",6);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==7){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",7);
                        startActivity(intent);
                        getActivity().finish();
                    }else if(position==8){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",8);
                        startActivity(intent);
                        getActivity().finish();
                    }
                    else if(position==9){
                        Intent intent = new Intent(getActivity(), Calculation2_pager.class);
                        intent.putExtra("pages",9);
                        startActivity(intent);
                        getActivity().finish();
                    }
                }


            }

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

            }
        });
        final DecimalFormat myFormatter= new DecimalFormat("###,###");

        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(weight.getText().toString().length()!=0&&totalyarn_denier.getText().toString().length()!=0){
                    try{
                        float total_weight = Float.parseFloat(weight.getText().toString());
                        float total_denier = Float.parseFloat(totalyarn_denier.getText().toString());
                        double calcu1 = total_weight*9000/total_denier;
                        double calcu1round = Math.round(calcu1);
                        int calcu1roundint = (int)calcu1round;
                        formattedStringPrice=myFormatter.format(calcu1roundint);
                        saveNum= calcu1roundint;
                        totalyarn_length1.setText(formattedStringPrice+"m");


                        int BeamCount=Integer.parseInt(beamWcount1.getText().toString());
                        double calcul2= saveNum/BeamCount;
                        double calculround2=Math.round(calcul2);
                        formattedStringPrice=myFormatter.format(calculround2);
                        beamWlength1.setText(formattedStringPrice+"meter");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        totalyarn_denier.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(weight.getText().toString().length()!=0&&totalyarn_denier.getText().toString().length()!=0){
                    try{
                        float total_weight = Float.parseFloat(weight.getText().toString());
                        float total_denier = Float.parseFloat(totalyarn_denier.getText().toString());
                        double calcu1 = total_weight*9000/total_denier;
                        double calcu1round = Math.round(calcu1);
                        int calcu1roundint = (int)calcu1round;
                        formattedStringPrice=myFormatter.format(calcu1roundint);
                        saveNum= calcu1roundint;
                        totalyarn_length1.setText(formattedStringPrice+"m");

                        int BeamCount=Integer.parseInt(beamWcount1.getText().toString());
                        double calcul2= saveNum/BeamCount;
                        double calculround2=Math.round(calcul2);
                        formattedStringPrice=myFormatter.format(calculround2);
                        beamWlength1.setText(formattedStringPrice+"meter");

                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        totalyarn_weight2.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(totalyarn_weight2.getText().toString().length()!=0&&totalyarn_dtex.getText().toString().length()!=0){
                    try{
                        float total_weight = Float.parseFloat(totalyarn_weight2.getText().toString());
                        float total_dtex = Float.parseFloat(totalyarn_dtex.getText().toString());
                        double calcu1 = total_weight*10000/total_dtex;
                        double calcu1round = Math.round(calcu1);
                        int calcu1roundint = (int)calcu1round;
                        formattedStringPrice=myFormatter.format(calcu1roundint);
                        saveNum2= calcu1roundint;
                        totalyarn_length2.setText(formattedStringPrice+"m");
                        int BeamCount=Integer.parseInt(beamWcount2.getText().toString());
                        double calcul2= saveNum2/BeamCount;
                        double calculround2=Math.round(calcul2);
                        formattedStringPrice=myFormatter.format(calculround2);
                        beamWlength2.setText(formattedStringPrice+"meter");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        totalyarn_dtex.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(totalyarn_weight2.getText().toString().length()!=0&&totalyarn_dtex.getText().toString().length()!=0){
                    try{
                        float total_weight = Float.parseFloat(totalyarn_weight2.getText().toString());
                        float total_dtex = Float.parseFloat(totalyarn_dtex.getText().toString());
                        double calcu1 = total_weight*10000/total_dtex;
                        double calcu1round = Math.round(calcu1);
                        int calcu1roundint = (int)calcu1round;
                        formattedStringPrice=myFormatter.format(calcu1roundint);
                        saveNum2= calcu1roundint;
                        totalyarn_length2.setText(formattedStringPrice+"m");

                        int BeamCount=Integer.parseInt(beamWcount2.getText().toString());
                        double calcul2= saveNum2/BeamCount;
                        double calculround2=Math.round(calcul2);
                        formattedStringPrice=myFormatter.format(calculround2);
                        beamWlength2.setText(formattedStringPrice+"meter");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });

        beamWcount1.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) {
                try{
                    int BeamCount=Integer.parseInt(beamWcount1.getText().toString());
                    float calcul= saveNum/BeamCount;
                    float calculround=Math.round(calcul);
                    formattedStringPrice=myFormatter.format(calculround);
                    beamWlength1.setText(formattedStringPrice+"meter");
                }catch (NumberFormatException e){

                }catch (NullPointerException e){

                }
            }
        });

        beamWcount2.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) {
                try{
                    int BeamCount=Integer.parseInt(beamWcount2.getText().toString());
                    double calcul= saveNum2/BeamCount;
                    double calculround=Math.round(calcul);
                    formattedStringPrice=myFormatter.format(calculround);
                    beamWlength2.setText(formattedStringPrice+"meter");
                }catch (NumberFormatException e){

                }catch (NullPointerException e){

                }
            }
        });
    }
}
