package com.Tricot.calcutator.Calculator2pager.Calculator2;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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.Calculator_pop;
import com.Tricot.calcutator.R;
import com.Tricot.calcutator.data.Calculation2_pager;
import com.Tricot.calcutator.data.OnTimePickerSerListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Calcul2_1pager extends Fragment {
    ImageView FinishBtn;
    int bar;
    int z=0;
    List list1;
    List list2;
    List list3;
    TableLayout table;
    Integer calcul = 0;
    private EditText et1;
    private EditText et2;
    private TextView et3;
    private EditText first;
    private int second;
    private EditText autoEdit;
    private int page;
    private OnTimePickerSerListener onTimePickerSerListener;
    int cal2_1;
    int focusnum=101;
    int focusnum1=201;
    EditText[] cal2_1_mil;
    EditText[] cal2_1_rack;
    boolean box1;
    static private String SHARE_NAME = "SHARE_PREF";
    static SharedPreferences sharePref = null;
    static SharedPreferences.Editor editor = null;
    Boolean onOff=false;
    private boolean isFirstSelected = true; // 전역변수로 선언
    private int calheight1;


    public Calcul2_1pager(){
    }
    @Nullable


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        sharePref=getContext().getSharedPreferences(SHARE_NAME,Context.MODE_PRIVATE);
        editor = sharePref.edit();

        onOff=sharePref.getBoolean("onOff",false);
        System.out.println("-------------------------");
        System.out.println(onOff);
        System.out.println("-------------------------");
        if(onOff==false){
            Intent intent = new Intent(this.getContext(), Calculator_pop.class);
            startActivity(intent);
        }else{
            System.out.println("팝업 사용 끝!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
    }


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


    }
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        table = view.findViewById(R.id.table1);
        bar = getActivity().getIntent().getExtras().getInt("bars");
        page = getActivity().getIntent().getExtras().getInt("pages");
        calcul = Integer.valueOf(bar);
        Map<String, Integer> n = new HashMap<String, Integer>();
        list1 = new ArrayList();
        list2 = new ArrayList();
        list3 = new ArrayList();
        cal2_1_mil = new EditText[bar];
        cal2_1_rack = new EditText[bar];
        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(0);
        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) {

            }
        });
        int a=70;
        final int b=40;
        int c=100;

        float mScale1=getResources().getDisplayMetrics().density;
        final float calwidth1=(float)0.2;
        final float calwidth2=(float)0.3;

        if(bar!=0&&page==0){
            for(int i=1; i<=bar; i++){
                ConstraintLayout row = new ConstraintLayout(getContext());
                TextView barnum = new TextView(getContext());
                String strColor = "#434F69";
                barnum.setTextColor(Color.parseColor(strColor));

                calheight1=(int)(b*mScale1);

                et1 = new EditText(getContext());
                et2 = new EditText(getContext());
                et3 = new TextView(getContext());

                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.border));
                et2.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border));
                et3.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border));
                barnum.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.border));
                barnum.setId(400+i);
                et1.setId(100+i);
                et2.setId(200+i);
                et3.setId(300+i);
                barnum.setText("GB"+i);
                barnum.setGravity(Gravity.CENTER);
                et1.setGravity(Gravity.CENTER);
                et1.setHint("cm");
                et2.setGravity(Gravity.CENTER);
                et2.setHint("mm");
                et3.setGravity(Gravity.CENTER);

                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 = calwidth1;

                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 = calwidth2;

                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 = calwidth2;

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

                row.addView(barnum);
                row.addView(et1);
                row.addView(et2);
                row.addView(et3);
                list1.add(100+i);
                list2.add(200+i);
                list3.add(300+i);
                table.addView(row,i);
            }
            first = (EditText)view.findViewById(Integer.parseInt(list1.get(z).toString()));
            first.requestFocus();

            first.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    for(int i=1;i<bar;i++){
                        autoEdit = (EditText)view.findViewById(Integer.parseInt(list1.get(i).toString()));
                        autoEdit.setText(first.getText());
                    }
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    for(int i=1;i<bar;i++){
                        autoEdit = (EditText)view.findViewById(Integer.parseInt(list1.get(i).toString()));
                        autoEdit.setText(first.getText());
                    }
                }
            });

            second = Integer.parseInt(list2.get(z).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;
                }
            });
            cal2_1 = 1;
            onTimePickerSerListener.onbarMakerSet(cal2_1);

            for(int i = 0; i<bar; i++){
                cal2_1_mil[i] = view.findViewById(focusnum+i);
                cal2_1_rack[i] = view.findViewById(focusnum1+i);
            }

            for(int i=0;i<bar;i++){
                final int is = i;
                cal2_1_mil[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(cal2_1_mil[is].getText().toString().length()!=0&&cal2_1_rack[is].getText().toString().length()!=0){
                            ((Calculation2_pager) Calculation2_pager.mContext).calculation(view);
                        }
                    }
                });
            }
            for(int i=0;i<bar;i++){
                final int is = i;
                cal2_1_rack[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(cal2_1_mil[is].getText().toString().length()!=0&&cal2_1_rack[is].getText().toString().length()!=0){
                            ((Calculation2_pager) Calculation2_pager.mContext).calculation(view);
                        }
                    }
                });
            }




        }
    }
    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        if(context instanceof OnTimePickerSerListener){
            onTimePickerSerListener = (OnTimePickerSerListener)context;
        }else {
            throw new RuntimeException(context.toString()+"되자제발");
        }
    }
}
