package com.android.chmo.ui.activity.activities;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.Res;
import com.android.chmo.http.service.ActivitiesService;
import com.android.chmo.model.ActivitiesInfo;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;


public class SignInActivity extends BaseActivity {

    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.atitle)
    TextView titleView;
    @BindView(R.id.joinMember)
    EditText joinMemberEdit;
    @BindView(R.id.contact)
    EditText contactEdit;
    @BindView(R.id.count)
    TextView countView;
    @BindView(R.id.price)
    TextView priceView;
    @BindView(R.id.all_price)
    TextView totalPriceView;
    @BindView(R.id.instruction)
    TextView instructionView;

    private int count = 1;

    private ActivitiesInfo info;

    @Override
    protected int getContentResId() {
        return R.layout.activity_sign_in;
    }

    @Override
    public void initView() {
        info = (ActivitiesInfo) getIntent().getSerializableExtra("data");

        int width = PixelUtils.getWindowWidth();
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)imageView.getLayoutParams();
        lp.width = width;
        lp.height = width;
        imageView.setLayoutParams(lp);
        XUtilsImage.display(imageView, HttpApi.getImgUrl(info.pic), R.mipmap.def_img2);

        titleView.setText(info.caption);
        priceView.setText(info.meprice + "元/人");
        totalPriceView.setText(info.meprice + "元");
    }

    @OnClick(R.id.add)
    public void add(){
        count++;
        countView.setText(""+count);

        totalPriceView.setText(info.meprice * count + "元");
    }

    @OnClick(R.id.dec)
    public void dec(){
        count--;
        count = Math.max(1, count);
        countView.setText(""+count);
        totalPriceView.setText(info.meprice * count + "元");
    }

    @OnClick(R.id.signBtn)
    public void sign(){
        String joinMember = joinMemberEdit.getText().toString();
        String contact = contactEdit.getText().toString();
        String allPrice = String.valueOf(info.meprice * count);
        if(TextUtils.isEmpty(joinMember)) {
            showToast("请输入参加人员");
            return;
        }
        if(TextUtils.isEmpty(contact)) {
            showToast("请输入联系方式");
            return;
        }

        showLoading("报名中...");
        ActivitiesService.signIn(info, joinMember, contact, String.valueOf(count), allPrice, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                Res res = new Gson().fromJson(result,Res.class);
                if(res.msg.equals("success")) {
                    showToast("报名成功");
                    EventBus.getDefault().post("signChange");
                    finish();
                }else {
                    showToast("报名失败");
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("报名失败");
            }
        });

    }
}
