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_5pager extends Fragment {
    ImageView FinishBtn;
    EditText yarnlength_length1;
    EditText yarnlength_denier;
    TextView yarnlength_bon1;
    EditText yarnlength_length2;
    EditText yarnlength_dtex;
    TextView yarnlength_bon2;
    private boolean isFirstSelected = true; // 전역변수로 선언
    DecimalFormat myFormatter = new DecimalFormat("###,###");
    public Calcul2_5pager(){
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_calculator_2_5, container, false);
        return rootView;
    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        yarnlength_length1 = view.findViewById(R.id.yarnlength_length1);
        yarnlength_denier = view.findViewById(R.id.yarnlength_denier);
        yarnlength_bon1 = view.findViewById(R.id.yarnlength_bon1);

        yarnlength_length2 = view.findViewById(R.id.yarnlength_length2);
        yarnlength_dtex = view.findViewById(R.id.yarnlength_dtex);
        yarnlength_bon2 = view.findViewById(R.id.yarnlength_bon2);
        yarnlength_length1.requestFocus();
        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(4);
        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) {

            }
        });
        yarnlength_length1.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 (yarnlength_length1.getText().toString().length()!=0&&yarnlength_denier.getText().toString().length()!=0){
                    try {
                        float yarn_weight = Float.parseFloat(yarnlength_length1.getText().toString());
                        float yarn_denier = Float.parseFloat(yarnlength_denier.getText().toString());
                        double calcu1 = yarn_weight/9000*yarn_denier;
                        double calcu1round = Math.round(calcu1);
                        int calcul1roundint = (int)calcu1round;
                        String formattedStringPrice = myFormatter.format(calcul1roundint);
                        yarnlength_bon1.setText(formattedStringPrice+"g");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        yarnlength_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 (yarnlength_length1.getText().toString().length()!=0&&yarnlength_denier.getText().toString().length()!=0){
                    try {
                        float yarn_weight = Float.parseFloat(yarnlength_length1.getText().toString());
                        float yarn_denier = Float.parseFloat(yarnlength_denier.getText().toString());
                        double calcu1 = yarn_weight/9000*yarn_denier;
                        double calcu1round = Math.round(calcu1);
                        int calcul1roundint = (int)calcu1round;
                        String formattedStringPrice = myFormatter.format(calcul1roundint);
                        yarnlength_bon1.setText(formattedStringPrice+"g");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        yarnlength_length2.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 (yarnlength_length2.getText().toString().length()!=0&&yarnlength_dtex.getText().toString().length()!=0){
                    try{
                        float yarn_weight = Float.parseFloat(yarnlength_length2.getText().toString());
                        float yarn_dtex = Float.parseFloat(yarnlength_dtex.getText().toString());
                        double calcu1 = yarn_weight/10000*yarn_dtex;
                        double calcu1round = Math.round(calcu1);
                        int calcul1roundint = (int)calcu1round;
                        String formattedStringPrice = myFormatter.format(calcul1roundint);
                        yarnlength_bon2.setText(formattedStringPrice+"g");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        yarnlength_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 (yarnlength_length2.getText().toString().length()!=0&&yarnlength_dtex.getText().toString().length()!=0){
                    try{
                        float yarn_weight = Float.parseFloat(yarnlength_length2.getText().toString());
                        float yarn_dtex = Float.parseFloat(yarnlength_dtex.getText().toString());
                        double calcu1 = yarn_weight/10000*yarn_dtex;
                        double calcu1round = Math.round(calcu1);
                        int calcul1roundint = (int)calcu1round;
                        String formattedStringPrice = myFormatter.format(calcul1roundint);
                        yarnlength_bon2.setText(formattedStringPrice+"g");
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
    }
}
