package ch.pizzalink.android.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalink.android.R;
import ch.pizzalink.android.helper.PriceHelper;
import ch.pizzalink.android.model.OrderHistoryModel;
import ch.pizzalink.android.view.PizzalinkInfoView;

public class OrderHistoryDetailsActivity extends BaseActivity {

    @BindView(R.id.orderDatePizzalinkInfoLayout) PizzalinkInfoView orderDatePizzalinkInfoLayout;
    @BindView(R.id.orderStatusPizzalinkInfoLayout) PizzalinkInfoView orderStatusPizzalinkInfoLayout;
    @BindView(R.id.orderShippingTimePizzalinkInfoLayout) PizzalinkInfoView orderShippingTimePizzalinkInfoLayout;
    @BindView(R.id.orderTotalPizzalinkInfoLayout) PizzalinkInfoView orderTotalPizzalinkInfoLayout;
    @BindView(R.id.orderPaymentMethodPizzalinkInfoLayout) PizzalinkInfoView orderPaymentMethodPizzalinkInfoLayout;
    @BindView(R.id.orderFullnamePizzalinkInfoLayout) PizzalinkInfoView orderFullnamePizzalinkInfoLayout;
    @BindView(R.id.orderShippingMethodPizzalinkInfoLayout) PizzalinkInfoView orderShippingMethodPizzalinkInfoLayout;
    @BindView(R.id.orderShippingAddressPizzalinkInfoLayout) PizzalinkInfoView orderShippingAddressPizzalinkInfoLayout;
    @BindView(R.id.orderNotePizzalinkInfoLayout) PizzalinkInfoView orderNotePizzalinkInfoLayout;

    private OrderHistoryModel orderHistoryModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_history_details);
        ButterKnife.bind(this);
        getDataFromIntent();
        initViews();
    }

    private void getDataFromIntent(){
        orderHistoryModel = (OrderHistoryModel) getIntent().getSerializableExtra("orderHistoryModel");
    }

    private void initViews(){
        orderDatePizzalinkInfoLayout.setText(orderHistoryModel.getFormattedCreateDate());
        orderStatusPizzalinkInfoLayout.setText(orderHistoryModel.getStatus());
        if(orderHistoryModel.getShippingTime().isEmpty()){
            orderShippingTimePizzalinkInfoLayout.setVisibility(View.GONE);
        }
        else {
            orderShippingTimePizzalinkInfoLayout.setText(orderHistoryModel.getShippingTime());
        }
        orderTotalPizzalinkInfoLayout.setText(PriceHelper.roundFractions(orderHistoryModel.getTotalString()));
        orderPaymentMethodPizzalinkInfoLayout.setText(orderHistoryModel.getPaymentMethod());
        orderFullnamePizzalinkInfoLayout.setText(orderHistoryModel.getFirstname() + " " + orderHistoryModel.getLastname());
        orderShippingMethodPizzalinkInfoLayout.setText(orderHistoryModel.getShippingMethod());
        orderShippingAddressPizzalinkInfoLayout.setText(createAddress());
        if(orderHistoryModel.getComment().isEmpty()){
            orderNotePizzalinkInfoLayout.setVisibility(View.GONE);
        }
        else {
            orderNotePizzalinkInfoLayout.setText(orderHistoryModel.getComment());
        }
    }

    private String createAddress(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append(orderHistoryModel.getFirstname())
                .append(" ")
                .append(orderHistoryModel.getLastname())
                .append("\n");

        if(!orderHistoryModel.getPaymentAddress1().isEmpty()){
            stringBuilder
                    .append(orderHistoryModel.getShippingAddress1())
                    .append("\n");
        }

        if(!orderHistoryModel.getShippingAddress2().isEmpty()){
            stringBuilder
                    .append(orderHistoryModel.getShippingAddress2())
                    .append("\n");
        }
         stringBuilder
                 .append(orderHistoryModel.getShippingPostcode())
                .append(" ")
                .append(orderHistoryModel.getShippingCity())
                .append("\n")
                .append(orderHistoryModel.getShippingCountry());

        return stringBuilder.toString();
    }
}
