package com.example.calcutator.Calculator1;

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

import com.example.calcutator.R;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Calculator_1_1 extends AppCompatActivity {
    TableLayout tb;
    TableLayout tb1;
    TableRow tests;
    Integer calcul;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_1_1);
        Intent intent = getIntent();
        int bar = intent.getExtras().getInt("bars");
        calcul = Integer.valueOf(bar);
        tb = findViewById(R.id.table1);
        tb1 = findViewById(R.id.table2);

        int a=70;
        int b=116;
        int c=70;

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


        for (int i = 1; i <=calcul; i++) {
            TableRow row= new TableRow(this);
            TextView tv = new TextView(this);
            EditText et1 = new EditText(this);
            EditText et2 = new EditText(this);
            EditText et3 = new EditText(this);
            EditText et4 = new EditText(this);
            tv.setText("GB"+i);
            tv.setWidth(calwidth1);
            tv.setGravity(Gravity.CENTER);
            et1.setWidth(calwidth1);
            et1.setGravity(Gravity.CENTER);
            et2.setWidth(calwidth1);
            et2.setGravity(Gravity.CENTER);
            et3.setWidth(calwidth1);
            et3.setGravity(Gravity.CENTER);
            et4.setWidth(calwidth1);
            et4.setGravity(Gravity.CENTER);
            et1.setId(100+i);
            et1.setMaxHeight(120);
            et2.setId(200+i);
            et2.setMaxHeight(120);
            et3.setId(300+i);
            et3.setMaxHeight(120);
            et4.setId(400+i);
            et4.setMaxHeight(120);
            row.addView(tv);
            row.addView(et1);
            row.addView(et2);
            row.addView(et3);
            row.addView(et4);
            tb.addView(row,i);
        }
    }
    public void weight_calculation(View v){
        float sums = 0;
        float hap = 0;
        float calcusum = 0;
        int milss = 0;
        int rackss = 0;
        int bonss = 0;
        int deniarss = 0;
        for(int i = 1; i<=calcul; i++){
            EditText mil = findViewById(100+i);
            EditText rack = findViewById(200+i);
            EditText bon = findViewById(300+i);
            EditText deniar = findViewById(400+i);
            try{
                String S_mil=mil.getText().toString();
                String S_rack=rack.getText().toString();
                String S_bon=bon.getText().toString();
                String S_deniar=deniar.getText().toString();
                    int mils = Integer.parseInt(S_mil);
                    int racks = Integer.parseInt(S_rack);
                    int bons = Integer.parseInt(S_bon);
                    int deniars = Integer.parseInt(S_deniar);
                    milss = mils;
                    rackss = racks;
                    bonss = bons;
                    deniarss = deniars;
                    float calcu1 = (float)(milss*91.44/480*rackss/9000/1000*bonss*deniarss);
                    int b=116;
                    float mScale2=getResources().getDisplayMetrics().density;
                    final int calwidth2=(int)(b*mScale2);
                    sums = calcu1+sums;
                    TableRow row = new TableRow(this);
                    TextView tbv1 = new TextView(this);
                    TextView tbv2 = new TextView(this);
                    TextView tbv3 = new TextView(this);

                    tbv1.setWidth(calwidth2);
                    tbv1.setGravity(Gravity.CENTER);
                    tbv2.setWidth(calwidth2);
                    tbv2.setHeight(150);
                    tbv2.setGravity(Gravity.CENTER);
                    tbv3.setWidth(calwidth2);
                    tbv3.setGravity(Gravity.CENTER);
                    tbv2.setId(500+i);
                    tbv3.setId(600+i);
                    tbv1.setText("GB" + i);
                    tbv2.setText(Float.toString(calcu1));
                    row.addView(tbv1);
                    row.addView(tbv2);
                    row.addView(tbv3);
                    tb1.addView(row, i);

            }catch(NumberFormatException e){
                Toast.makeText(this.getApplicationContext(),"값을 제대로 입력해 주세요",Toast.LENGTH_LONG).show();
            }
        }
        try {

            for (int z = 1; z <= calcul; z++) {
                TextView calcu1s = findViewById(500 + z);
                TextView calcu2s = findViewById(600 + z);


                float calcu1 = Float.parseFloat(calcu1s.getText().toString());
                float calcu2 = calcu1 / sums * 100;
                calcu2s.setText(Float.toString(calcu2));
                calcusum = calcu2 + calcusum;
            }
            TableRow row= new TableRow(this);
            TextView tv1 = new TextView(this);
            TextView tv2 = new TextView(this);
            TextView tv3 = new TextView(this);
            tv1.setText("합계");
            tv1.setGravity(Gravity.CENTER);
            tv2.setText(Float.toString(sums));
            tv2.setGravity(Gravity.CENTER);
            tv2.setHeight(150);
            tv3.setText(Float.toString(calcusum));
            tv3.setGravity(Gravity.CENTER);
            row.addView(tv1);
            row.addView(tv2);
            row.addView(tv3);
            tb1.addView(row);
        }catch (NullPointerException e){

        }

    }


}
