package com.example.calcutator.Calculator2;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import com.example.calcutator.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

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

public class Calculator_2_1 extends AppCompatActivity {
    TableLayout table;
    Integer calcul = 0;
    private EditText et1;
    private EditText et2;
    private TextView et3;
    private EditText first;
    private int second;
    private EditText autoEdit;
    int second2;
    int bar;
    int z=0;
    List list1;
    List list2;
    List list3;
    private int calwidth1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_2_1);
        table = findViewById(R.id.table1);
        Intent intent = getIntent();
        bar = intent.getExtras().getInt("bars");
        calcul = Integer.valueOf(bar);
        Map<String, Integer> n = new HashMap<String, Integer>();
        list1 = new ArrayList();
        list2 = new ArrayList();
        list3 = new ArrayList();


        int a=70;
        int b=70;
        int c=100;

        float mScale1=getResources().getDisplayMetrics().density;
        final int calwidth1=(int)(a*mScale1);

        float mScale2=getResources().getDisplayMetrics().density;
        final int calwidth2=(int)(b*mScale1);

        float mScale3=getResources().getDisplayMetrics().density;
        final int calwidth3=(int)(c*mScale1);
        for(int i=1; i<=bar; i++){
            /*
            TableRow row = new TableRow(this);
            TextView barnum = new TextView(this);
            EditText et1 = new EditText(this);
            EditText et2 = new EditText(this);
            TextView length = new TextView(this);
            barnum.setText("GB"+i);
            barnum.setGravity(Gravity.CENTER);
            et1.setId(100+i);
            et1.setGravity(Gravity.CENTER);
            et2.setId(200+i);
            et2.setGravity(Gravity.CENTER);
            length.setId(300+i);
            length.setText("00m/y");
            length.setGravity(Gravity.CENTER);
            row.addView(barnum);
            row.addView(et1);
            row.addView(et2);
            row.addView(length);
            table.addView(row,i);*/
            TableRow row = new TableRow(this);
            TextView barnum = new TextView(this);
            String strColor = "#434F69";
            barnum.setTextColor(Color.parseColor(strColor));

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

            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.setWidth(calwidth1);
            et2.setWidth(calwidth1);
            et3.setWidth(calwidth1);

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

            et1.setBackground(ContextCompat.getDrawable(this,R.drawable.border2));
            et2.setBackground(ContextCompat.getDrawable(this,R.drawable.border2));
            et3.setBackground(ContextCompat.getDrawable(this,R.drawable.border2));
            barnum.setBackground(ContextCompat.getDrawable(this,R.drawable.border2));
            barnum.setWidth(calwidth1);
            et1.setWidth(calwidth1);
            et2.setWidth(calwidth1);
            et3.setWidth(calwidth1);
            TextView prices = new TextView(this);
            barnum.setText("GB"+i);
            barnum.setGravity(Gravity.CENTER);
            barnum.setWidth(calwidth1);
            barnum.setHeight(120);
            barnum.setMaxHeight(120);
            et1.setId(100+i);
            et1.setWidth(calwidth1);
            et1.setHeight(120);
            et1.setMaxHeight(120);
            et1.setGravity(Gravity.CENTER);
            et1.setHint("%");
            et2.setId(200+i);
            et2.setWidth(calwidth1);
            et2.setHeight(120);
            et2.setMaxHeight(120);
            et2.setGravity(Gravity.CENTER);
            et2.setHint("won");
            et3.setId(300+i);
            et3.setWidth(calwidth1);
            et3.setHeight(120);
            et3.setMaxHeight(120);
            et3.setGravity(Gravity.CENTER);
            prices.setId(400+i);
            prices.setText("");
            prices.setWidth(calwidth1);
            prices.setGravity(Gravity.CENTER);
            prices.setHeight(120);
            prices.setMaxHeight(120);
            prices.setBackground(ContextCompat.getDrawable(this,R.drawable.border2));
            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);
        }
        first = (EditText)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)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)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;
            }
        });

    }

    public void calcul(View v){
        float sums1= 0;
        try{
            for(int i=1; i<=calcul; i++){
                EditText mils = findViewById(100+i);
                EditText rackmms = findViewById(200+i);
                TextView length = findViewById(300+i);
                Float mil = Float.parseFloat(mils.getText().toString());
                Float rackmm = Float.parseFloat(rackmms.getText().toString());
                double calcul1 = mil*91.44/480*rackmm/1000;
                double calculround1 = Math.round(calcul1*10)/10.0;
                length.setText(calculround1+"m/y");
            }
        }catch (NumberFormatException e){
            Toast.makeText(this.getApplicationContext(),"값을 제대로 입력해 주세요", Toast.LENGTH_LONG).show();
        }

    }


}
