package com.Tricot.calcutator.Calculator1pager.Calculator1;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
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 com.Tricot.calcutator.data.OnTimePickerSerListener;

import java.util.ArrayList;
import java.util.List;

public class Calcul4pager extends Fragment {
    ImageView FinishBtn;
    TableLayout table;
    Integer calcul = 0;
    private EditText et1;
    private EditText et2;
    private EditText et3;
    private EditText first;
    private EditText autoEdit;
    EditText weight;
    EditText output;
    EditText Earnigs;
    EditText salesmargin;
    EditText testwet;
    private EditText weights;
    Integer firstnum=101;
    Integer secondnum = 201;
    Integer thirdnum = 301;
    EditText[] FirstEdit;
    EditText[] SecondEdit;
    EditText[] ThirdEdit;
    private int second;
    private int test;
    private int bar;
    private int sum = 0;
    private int sum2;
    private int sum3;
    private int o=0;
    private int calheight1;
    Integer cknums=0;
    List list;
    List list1;
    List list2;
    List list3;
    private int page;
    int cal1_4;
    int cal1_3;
    int cal1_1;
    private OnTimePickerSerListener onTimePickerSerListener;
    private boolean isFirstSelected = true; // 전역변수로 선언
    String strColor = "#001E68";

    public Calcul4pager(){
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.content_calculator_1_4, container, false);
        return rootView;
    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bar = getActivity().getIntent().getExtras().getInt("bars");
        page = getActivity().getIntent().getExtras().getInt("pages");
        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(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",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) {

            }
        });

        if(bar!=0&&page==3){
            table = view.findViewById(R.id.table1);

            calcul = Integer.valueOf(bar);
            sum=0;
            sum2=0;
            list = new ArrayList();
            list1 = new ArrayList();
            list2 = new ArrayList();
            list3 = new ArrayList();

            float sums1= 0;
            float sums2= 0;
            float sums3 = 0;

            int z=0;
            list.add(101);

            int a=75;
            int b=40;
            int c=100;

            float mScale1=getResources().getDisplayMetrics().density;
            final float calwidth1= (float)0.1;
            final float calwidth2= (float)0.2;
            final float calwidth3= (float)0.325;
            final float calwidth4= (float)0.1;
            final float calwidth5= (float)0.275;


            calheight1=(int)(b*mScale1);
            for(int i=1; i<=bar; i++){
                Integer znum = 0;
                ConstraintLayout row = new ConstraintLayout(getContext());
                TextView barnum = new TextView(getContext());
                et1 = new EditText(getContext());
                et2 = new EditText(getContext());
                et3 = new EditText(getContext());


                String strColor = "#434F69";
                barnum.setTextColor(Color.parseColor(strColor));

                et1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                et2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                et3.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);


                et1.setIncludeFontPadding(false);
                et2.setIncludeFontPadding(false);
                et3.setIncludeFontPadding(false);

                et1.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border2));
                et2.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border2));
                et3.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border2));
                barnum.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border2));
                TextView prices = new TextView(getContext());
                barnum.setId(1000+i);
                barnum.setText("GB"+i);
                barnum.setGravity(Gravity.CENTER);
                barnum.setHeight(calheight1);
                barnum.setMaxHeight(calheight1);
                et1.setId(100+i);
                et1.setHeight(calheight1);
                et1.setMaxHeight(calheight1);
                et1.setGravity(Gravity.CENTER);
                et1.setHint("%");
                et2.setId(200+i);
                et2.setHeight(calheight1);
                et2.setMaxHeight(calheight1);
                et2.setGravity(Gravity.CENTER);
                et2.setHint("won");
                et3.setId(300+i);
                et3.setHeight(calheight1);
                et3.setMaxHeight(calheight1);
                et3.setHint("%");
                et3.setGravity(Gravity.CENTER);
                prices.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border2));
                prices.setId(400+i);
                prices.setTextColor(Color.parseColor(strColor));
                prices.setTextSize(18);
                prices.setText("");
                prices.setGravity(Gravity.CENTER);
                prices.setHeight(calheight1);
                prices.setMaxHeight(calheight1);


                ConstraintLayout.LayoutParams layparms = new ConstraintLayout.LayoutParams(0,calheight1);
                layparms.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms.matchConstraintPercentWidth = calwidth1;

                ConstraintLayout.LayoutParams layparms1 = new ConstraintLayout.LayoutParams(0,calheight1);
                layparms1.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms1.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms1.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms1.startToEnd = barnum.getId();
                layparms1.matchConstraintPercentWidth = calwidth2;

                ConstraintLayout.LayoutParams layparms2 = new ConstraintLayout.LayoutParams(0,calheight1);
                layparms2.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms2.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms2.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms2.startToEnd = et1.getId();
                layparms2.matchConstraintPercentWidth = calwidth3;

                ConstraintLayout.LayoutParams layparms3 = new ConstraintLayout.LayoutParams(0,calheight1);
                layparms3.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms3.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms3.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms3.startToEnd = et2.getId();
                layparms3.matchConstraintPercentWidth = calwidth4;

                ConstraintLayout.LayoutParams layparms4 = new ConstraintLayout.LayoutParams(0,calheight1);
                layparms4.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms4.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms4.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
                layparms4.startToEnd = et3.getId();
                layparms4.matchConstraintPercentWidth = calwidth5;

                barnum.setLayoutParams(layparms);
                et1.setLayoutParams(layparms1);
                et2.setLayoutParams(layparms2);
                et3.setLayoutParams(layparms3);
                prices.setLayoutParams(layparms4);


                row.addView(barnum);
                row.addView(et1);
                row.addView(et2);
                row.addView(et3);
                row.addView(prices);
                list1.add(100+i);
                list2.add(200+i);
                list3.add(300+i);
                table.addView(row,i);
                secondnum = 201;

            }
            first = (EditText)view.findViewById(Integer.parseInt(list.get(z).toString()));



            weight=(EditText)view.findViewById(R.id.weight);
            output=(EditText)view.findViewById(R.id.output);
            Earnigs=(EditText)view.findViewById(R.id.Earnings);
            salesmargin=(EditText)view.findViewById(R.id.salesmargin);
            FirstEdit = new EditText[bar];
            SecondEdit =new EditText[bar];
            ThirdEdit = new EditText[bar];


            for(int i=0;i<bar;i++){
                FirstEdit[i]=view.findViewById(firstnum+i);
                SecondEdit[i]=view.findViewById(secondnum+i);
                ThirdEdit[i]=view.findViewById(thirdnum+i);
            }


            for(int i=0;i<bar;i++){
                final int is = i;
                FirstEdit[i].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(FirstEdit[is].getText().toString().length()!=0&&SecondEdit[is].getText().toString().length()!=0&&ThirdEdit[is].getText().toString().length()!=0&&weight.getText().toString().length()!=0){
                            ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                        }
                    }
                });
                SecondEdit[i].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(FirstEdit[is].getText().toString().length()!=0&&SecondEdit[is].getText().toString().length()!=0&&ThirdEdit[is].getText().toString().length()!=0&&weight.getText().toString().length()!=0){
                            ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                        }
                    }
                });
                ThirdEdit[i].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(FirstEdit[is].getText().toString().length()!=0&&SecondEdit[is].getText().toString().length()!=0&&ThirdEdit[is].getText().toString().length()!=0&&weight.getText().toString().length()!=0){
                            ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                        }
                    }
                });
                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(FirstEdit[is].getText().toString().length()!=0&&SecondEdit[is].getText().toString().length()!=0&&ThirdEdit[is].getText().toString().length()!=0&&weight.getText().toString().length()!=0){
                            ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                        }
                    }
                });
                output.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(output.getText().toString().length()!=0&&output.getText().toString().length()!=0){
                            ((Calculation1_pager) Calculation1_pager.mContext).calculation(view);
                        }
                    }
                });

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

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





            FirstEdit = new EditText[bar];
            for (int i=0; i<bar;i++){
                FirstEdit[i] = view.findViewById(firstnum+i);

                FirstEdit[i].setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if(actionId== EditorInfo.IME_ACTION_NEXT){
                            try {
                                FirstEdit[sum3].setNextFocusForwardId(secondnum+sum3);
                                sum3 = sum3+1;
                            }catch (Exception e){
                                System.out.println(e);
                            }
                        }
                        return false;
                    }
                });

            }

            SecondEdit = new EditText[bar];
            for (int i = 0; i < bar; i++) {
                SecondEdit[i] = view.findViewById(secondnum+i);

                SecondEdit[i].setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if(actionId== EditorInfo.IME_ACTION_NEXT){
                            try {
                                SecondEdit[sum].setNextFocusForwardId(thirdnum+sum);
                                sum = sum+1;
                            }catch (Exception e){
                                System.out.println(e);
                            }
                        }
                        return false;
                    }
                });


            }
            ThirdEdit = new EditText[bar];
            for(o = 0; o < bar; o++){

                ThirdEdit[o] = view.findViewById(thirdnum+o);
                if (o==bar-1){
                    ThirdEdit[o].setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if(actionId==EditorInfo.IME_ACTION_NEXT||o<bar){
                                try {
                                    ThirdEdit[sum2].setNextFocusForwardId(R.id.weight);
                                }catch (Exception e){
                                    System.out.println(e);
                                }
                            }
                            return false;
                        }
                    });
                }else {
                    ThirdEdit[o].setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if(actionId==EditorInfo.IME_ACTION_NEXT||o<bar){
                                try {
                                    ThirdEdit[sum2].setNextFocusForwardId(firstnum+1+sum2);
                                    sum2 = sum2 + 1;
                                    System.out.println(o);
                                }catch (Exception e){
                                    System.out.println(e);
                                }
                            }
                            return false;
                        }
                    });
                }


            }
            //ThirdEdit=(EditText)findViewById(Integer.parseInt(list3.get(z+sum2).toString()));

            second = Integer.parseInt(list2.get(z).toString());
            test = Integer.parseInt(list3.get(0).toString());


            first.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        first.setNextFocusRightId(second);

                        return true;
                    }
                    return false;
                }
            });
            cal1_3 = 0;
            cal1_1 = 0;
            cal1_4 = 1;
            onTimePickerSerListener.onTimePickerSet(cal1_1,cal1_3,cal1_4);
        }
    }
    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        if(context instanceof OnTimePickerSerListener){
            onTimePickerSerListener = (OnTimePickerSerListener)context;
        }else {
            throw new RuntimeException(context.toString()+"되자제발");
        }
    }
}