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.And_Application;
import com.Tricot.calcutator.CalculatorMenu.Bar_select;
import com.Tricot.calcutator.R;
import com.Tricot.calcutator.data.Calculation1_pager;

public class Calcul6pager extends Fragment {
    ImageView FinishBtn;
    int ym_switch;
    double change_num;
    EditText grams;
    TextView View1;
    TextView View2;
    TextView ViewTitle1;
    TextView ViewTitle2;
    EditText inch_1;
    EditText gram_3;
    EditText inch_3;
    private boolean isFirstSelected = true; // 전역변수로 선언

    public Calcul6pager(){

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.content_calculator_1_6, container, false);
        return rootView;
    }

    public void onViewCreated(final View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        grams = view.findViewById(R.id.gram_1);
        grams.requestFocus();
        View1=view.findViewById(R.id.yard_1);
        View2=view.findViewById(R.id.paper_weight1);
        ViewTitle1=view.findViewById(R.id.cal_1_6tv1);
        ViewTitle2=view.findViewById(R.id.cal_1_6tv2);
        gram_3=view.findViewById(R.id.gram_3);
        inch_1=view.findViewById(R.id.inch_1);
        inch_3=view.findViewById(R.id.inch_3);
        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(5);
        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) {

            }
        });


        grams.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(grams.getText().toString().length()!=0&&grams.getText().toString().length()!=0){
                    try{

                        EditText inchs = view.findViewById(R.id.inch_1);
                        TextView weight = view.findViewById(R.id.yard_1);
                        float gram = Float.parseFloat(grams.getText().toString());
                        float inch = Float.parseFloat(inchs.getText().toString());
                        double calcul = (gram/100)*inch*2.54*change_num/100;
                        double calculround = Math.round(calcul*10)/10.0;
                        if(ym_switch==0){
                            weight.setText(calculround+"g/m");
                        }else{
                            weight.setText(calculround+"g/y");
                        }
                    }catch (NumberFormatException e){
                    }
                }
            }

        });
        gram_3.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(gram_3.getText().toString().length()!=0&&gram_3.getText().toString().length()!=0){
                    try{
                        EditText grams = view.findViewById(R.id.gram_3);
                        EditText inchs = view.findViewById(R.id.inch_3);
                        TextView weight = view.findViewById(R.id.paper_weight1);
                        float gram = Float.parseFloat(grams.getText().toString());
                        float inch = Float.parseFloat(inchs.getText().toString());
                        double calcul = gram*inch*2.54*change_num/100;
                        double calculround = Math.round(calcul*10)/10.0;
                        if(ym_switch==0){
                            weight.setText(calculround+"g/m");
                        }else{
                            weight.setText(calculround+"g/y");
                        }
                    }catch (NumberFormatException e){
                    }

                }
            }

        });
        inch_1.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(inch_1.getText().toString().length()!=0&&inch_1.getText().toString().length()!=0){
                    try{

                        EditText inchs = view.findViewById(R.id.inch_1);
                        TextView weight = view.findViewById(R.id.yard_1);
                        float gram = Float.parseFloat(grams.getText().toString());
                        float inch = Float.parseFloat(inchs.getText().toString());
                        double calcul = (gram/100)*inch*2.54*change_num/100;
                        double calculround = Math.round(calcul*10)/10.0;
                        if(ym_switch==0){
                            weight.setText(calculround+"g/m");
                        }else{
                            weight.setText(calculround+"g/y");
                        }
                    }catch (NumberFormatException e){
                    }
                }
            }

        });
        inch_3.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(inch_3.getText().toString().length()!=0&&inch_3.getText().toString().length()!=0){
                    try{
                        EditText grams = view.findViewById(R.id.gram_3);
                        EditText inchs = view.findViewById(R.id.inch_3);
                        TextView weight = view.findViewById(R.id.paper_weight1);
                        float gram = Float.parseFloat(grams.getText().toString());
                        float inch = Float.parseFloat(inchs.getText().toString());
                        double calcul = gram*inch*2.54*change_num/100;
                        double calculround = Math.round(calcul*10)/10.0;
                        if(ym_switch==0){
                            weight.setText(calculround+"g/m");
                        }else{
                            weight.setText(calculround+"g/y");
                        }
                    }catch (NumberFormatException e){
                    }
                }
            }
        });
        ym_switch=((And_Application)getActivity().getApplication()).getYm_switch();
        if(ym_switch==0){
            System.out.println("---------------------------------1--------------------------------------");
            change_num=((And_Application)getActivity().getApplication()).getConversion_ym();
            View1.setHint("Meter");
            View2.setHint("Meter");
            ViewTitle1.setText("스퀘어 미터 중량 대비 meter당 중량계산");
            ViewTitle2.setText("원형 시편 중량의 meter당 중량계산");
        }else{
            System.out.println("---------------------------------2--------------------------------------");
            change_num=((And_Application)getActivity().getApplication()).getConversion_my();
            View1.setHint("Yard");
            View2.setHint("Yard");
            ViewTitle1.setText("스퀘어 미터 중량 대비 yard당 중량계산");
            ViewTitle2.setText("원형 시편 중량의 yard당 중량계산");
        }


    }
}
