package com.example.calcutator.Calculator1;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
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.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_3 extends AppCompatActivity {
    TableLayout table;
    Integer calcul = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator_1_3);
        table = findViewById(R.id.table1);
        Intent intent = getIntent();
        int bar = intent.getExtras().getInt("bars");


        int a=60;
        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);
            EditText et3 = new EditText(this);
            TextView prices = new TextView(this);
            barnum.setText("GB"+i);
            barnum.setGravity(Gravity.CENTER);
            barnum.setWidth(calwidth2);
            et1.setId(100+i);
            et1.setWidth(calwidth2);
            et1.setMaxHeight(120);
            et1.setGravity(Gravity.CENTER);
            et1.setHint("%");
            et2.setId(200+i);
            et2.setWidth(calwidth3);
            et2.setMaxHeight(120);
            et2.setGravity(Gravity.CENTER);
            et2.setHint("won");
            et3.setId(300+i);
            et3.setWidth(calwidth1);
            et3.setMaxHeight(120);
            et3.setHint("%");
            et3.setGravity(Gravity.CENTER);
            prices.setId(400+i);
            prices.setText("");
            prices.setWidth(calwidth3);
            prices.setGravity(Gravity.CENTER);
            row.addView(barnum);
            row.addView(et1);
            row.addView(et2);
            row.addView(et3);
            row.addView(prices);
            table.addView(row,i);
        }

    }

    public void calcul(View v){
        float sums1= 0;
        float sums2= 0;
        float sums3 = 0;

        try{
            for(int i=1; i<=calcul; i++){
                EditText hons = findViewById(100+i);
                EditText pounds = findViewById(200+i);
                EditText losss = findViewById(300+i);
                EditText weights = findViewById(R.id.weight);
                TextView totals = findViewById(R.id.total);
                EditText outputs = findViewById(R.id.output);
                EditText Earningss = findViewById(R.id.Earnings);
                TextView rent = findViewById(R.id.rent);
                TextView papertotals = findViewById(R.id.papertotal);
                TextView hontotal = findViewById(R.id.hontotal);
                TextView poundtotal = findViewById(R.id.poundtotal);
                TextView barpricetotal = findViewById(R.id.barpricetotal);
                EditText salesmargins = findViewById(R.id.salesmargin);
                TextView prices = findViewById(400+i);
                Float hon = Float.parseFloat(hons.getText().toString());
                Float pound = Float.parseFloat(pounds.getText().toString());
                Float loss = Float.parseFloat(losss.getText().toString());
                Float weight = Float.parseFloat(weights.getText().toString());
                Float output = Float.parseFloat(outputs.getText().toString());
                Float Earnings = Float.parseFloat(Earningss.getText().toString());
                Float salesmargin = Float.parseFloat(salesmargins.getText().toString());

                float calcul1 = (float)(pound*(weight*(hon/100)*(100+loss)/453.592/100));
                double calculround1 = Math.round(calcul1);
                sums1 = hon + sums1;
                sums2 = pound + sums2;
                sums3 = calcul1 + sums3;
                prices.setText(calculround1+"원");
                System.out.println("test");
                if(i==calcul){
                    float calcul3 = Earnings/output;
                    double calculround3 = Math.round(calcul3);
                    float calcul2 = sums3 + calcul3+ salesmargin;
                    double calculround2 = Math.round(calcul2);

                    /* 합산 부분 반올림 */
                    double sumsround1 = Math.round(sums1);
                    double sumsround2 = Math.round(sums2);
                    double sumsround3 = Math.round(sums3);

                    rent.setText(calculround3+"원");
                    papertotals.setText(sumsround3+"원/Y");
                    totals.setText(calculround2+"원/Y");
                    hontotal.setText(sumsround1+"%");
                    poundtotal.setText(sumsround2+"원");
                    barpricetotal.setText(sumsround3+"원");
                }
            }
        }catch (NumberFormatException e){
            Toast.makeText(this.getApplicationContext(),"값을 제대로 입력해 주세요", Toast.LENGTH_LONG).show();
        }

    }



}
