package ch.pizzalink.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;

import com.badoualy.stepperindicator.StepperIndicator;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalink.android.R;
import ch.pizzalink.android.fragment.createOrder.CreateOrderNoteFragment;
import ch.pizzalink.android.fragment.createOrder.CreateOrderResultFragment;
import ch.pizzalink.android.fragment.createOrder.CreateOrderSummaryFragment;
import ch.pizzalink.android.fragment.createOrder.PaymentMethodFragment;
import ch.pizzalink.android.fragment.createOrder.ShippingAddressFragment;
import ch.pizzalink.android.fragment.createOrder.ShippingMethodFragment;
import ch.pizzalink.android.helper.SharedPrefsHelper;
import ch.pizzalink.android.model.AddressModel;
import ch.pizzalink.android.model.PaymentMethodModel;
import ch.pizzalink.android.model.ShippingMethodModel;
import ch.pizzalink.android.model.cart.CartInfoModel;

public class CreateOrderActivity extends BaseActivity {

    @BindView(R.id.stepperIndicator) StepperIndicator stepperIndicator;

    private FragmentManager fragmentManager;

    private CartInfoModel cartInfoModel;
    private ShippingMethodModel selectedShippingMethod;
    private AddressModel selectedShippingAddress;
    private PaymentMethodModel selectedPaymentMethod;
    private String orderNote;

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

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void getDataFromIntent(){
        cartInfoModel = (CartInfoModel) getIntent().getSerializableExtra("cartInfoModel");
    }

    private void initViews(){
        fragmentManager = getSupportFragmentManager();
        initStepIndicator();
        openFragment(0);
    }

    private void initStepIndicator(){
        /*
        6 fragment var, ama sonuncu step'te tik göstersin diye step sayısını, fragment sayısı - 1 yaptık
         */
        stepperIndicator.setStepCount(5);
        /*
        stepperIndicator.addOnStepClickListener(new StepperIndicator.OnStepClickListener() {
            @Override
            public void onStepClicked(int step) {
                openFragment(step);
            }
        });
        */
    }

    private void openFragment(int position){
        switch (position){
            case 0:
                fragmentManager.beginTransaction().replace(R.id.orderFragmentsContainer,
                        ShippingMethodFragment.newInstance()).commit();
                break;
            case 1:
                fragmentManager.beginTransaction().replace(R.id.orderFragmentsContainer,
                        ShippingAddressFragment.newInstance()).commit();
                break;
            case 2:
                fragmentManager.beginTransaction().replace(R.id.orderFragmentsContainer,
                        PaymentMethodFragment.newInstance()).commit();
                break;
            case 3:
                fragmentManager.beginTransaction().replace(R.id.orderFragmentsContainer,
                        CreateOrderNoteFragment.newInstance()).commit();
                break;
            case 4:
                fragmentManager.beginTransaction().replace(R.id.orderFragmentsContainer,
                        CreateOrderSummaryFragment.newInstance()).commit();
                break;

            case 5:
                fragmentManager.beginTransaction().replace(R.id.orderFragmentsContainer,
                        CreateOrderResultFragment.newInstance()).commit();
                break;
        }
        stepperIndicator.setCurrentStep(position);
    }

    public void onNextClicked(String clickedFragmentName){
        switch (clickedFragmentName){
            case ShippingMethodFragment.FRAGMENT_NAME:
                openFragment(1);
                break;
            case ShippingAddressFragment.FRAGMENT_NAME:
                openFragment(2);
                break;
            case PaymentMethodFragment.FRAGMENT_NAME:
                openFragment(3);
                break;
            case CreateOrderNoteFragment.FRAGMENT_NAME:
                openFragment(4);
                break;
            case CreateOrderSummaryFragment.FRAGMENT_NAME:
                openFragment(5);
                break;
            case CreateOrderResultFragment.FRAGMENT_NAME:
                Intent mainActivityIntent = new Intent(this, MainActivity.class);
                mainActivityIntent.putExtra("isStartWithOrderHistory", true);
                startActivity(mainActivityIntent);
                SharedPrefsHelper.setCartItemCount(0);
                SharedPrefsHelper.setCartTotalPrice("0");
                finishAffinity();
                break;
        }
    }

    public void onPreviousClicked(String clickedFragmentName){
        switch (clickedFragmentName){
            case ShippingAddressFragment.FRAGMENT_NAME:
                openFragment(0);
                break;
            case PaymentMethodFragment.FRAGMENT_NAME:
                openFragment(1);
                break;
            case CreateOrderNoteFragment.FRAGMENT_NAME:
                openFragment(2);
                break;
            case CreateOrderSummaryFragment.FRAGMENT_NAME:
                openFragment(3);
                break;
        }
    }

    public CartInfoModel getCartInfo(){
        return cartInfoModel;
    }

    public ShippingMethodModel getSelectedShippingMethod(){
        return selectedShippingMethod;
    }

    public void setSelectedShippingMethod(ShippingMethodModel selectedShippingMethod){
        this.selectedShippingMethod = selectedShippingMethod;
    }

    public AddressModel getSelectedShippingAddress(){
        return selectedShippingAddress;
    }

    public void setSelectedShippingAddress(AddressModel selectedShippingAddress){
        this.selectedShippingAddress = selectedShippingAddress;
    }

    public PaymentMethodModel getSelectedPaymentMethod(){
        return selectedPaymentMethod;
    }

    public void setSelectedPaymentMethod(PaymentMethodModel selectedPaymentMethod){
        this.selectedPaymentMethod = selectedPaymentMethod;
    }

    public String getOrderNote(){
        if(orderNote == null)
            orderNote = "";
        return orderNote;
    }

    public void setOrderNote(String orderNote){
        this.orderNote = orderNote;
    }
}
