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

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
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.ModelSkillRes;
import com.android.chmo.http.service.ModelService;
import com.android.chmo.model.ModelInfo;
import com.android.chmo.model.ModelOrder;
import com.android.chmo.model.ModelSkill;
import com.android.chmo.ui.activity.order.SubmitOrderActivity;
import com.android.chmo.ui.adpater.CommonAdapter;
import com.android.chmo.ui.dialog.PickNumDialog;
import com.android.chmo.utils.DateUtils;
import com.android.chmo.utils.PickTimeUtils;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/3.
 */

public class ModelReserveActivty extends BaseActivity {
    @BindView(R.id.image)
    ImageView imageView;
    @BindView(R.id.name)
    TextView nameView;
    @BindView(R.id.info)
    TextView infoView;
    @BindView(R.id.price)
    TextView priceView;

    @BindView(R.id.workType)
    TextView workTypeView;

    @BindView(R.id.startTime)
    TextView startTimeView;

    @BindView(R.id.workHour)
    TextView workHourView;

    @BindView(R.id.selectWork)
    View selectWorkView;
    @BindView(R.id.gridview)
    GridView workGridView;
    @BindView(R.id.totalPrice)
    TextView totalPriceViw;

    private WorkLabelAdapter workLabelAdapter;

    private ModelInfo model;

    private ModelSkill selectSkill;

    private List<ModelSkill> skillList = new ArrayList<>();

    private Date startDate;
    private int workHour = 0;

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

    @Override
    public void initView() {
        model = (ModelInfo) getIntent().getSerializableExtra("model");

        int width = PixelUtils.getWindowWidth()/3;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        lp.width = width;
        lp.height = width;
        imageView.setLayoutParams(lp);

        fillView();

        loadWork();
    }

    private  void  fillView() {
        if(model.modelphoto != null && model.modelphoto.size()> 0) {
            XUtilsImage.display(imageView, HttpApi.getImgUrl(model.modelphoto.get(0).photo), R.mipmap.def_img2);
        }
        nameView.setText(model.name);
        infoView.setText(model.hei + "cm/" + model.areaname);
    }

    private void fillModelWorks() {
        workLabelAdapter = new WorkLabelAdapter();
        workLabelAdapter.setList(skillList);
        workGridView.setAdapter(workLabelAdapter);
    }

    private void fillTotalPrice() {
        float price = 0;
        if(null != selectSkill) {
            price = Float.parseFloat(selectSkill.price) * workHour;
        }
        totalPriceViw.setText("共计：" + workHour + "h/￥" + price + "元");
    }

    @OnClick(R.id.workType)
    public void selectWorkType() {
        selectWorkView.setVisibility(View.VISIBLE);
    }
    @OnClick(R.id.outside)
    public void clickOutside() {
        selectWorkView.setVisibility(View.GONE);
    }

    @OnClick(R.id.startTime)
    public void selectStartTime() {
        PickTimeUtils.showPickTime(this, "选择开始时间", true, false, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                date.setMinutes(0);
                startDate = date;
                startTimeView.setText(DateUtils.formatDateStr(date));
                fillTotalPrice();
            }
        });

    }

    @OnClick(R.id.workHour)
    public void selectEndTime() {
        new PickNumDialog(this)
                .setTitle("选择工作时长")
                .setNum(4, 24*7)
                .setPickCallback(new PickNumDialog.PickCallback() {
                    @Override
                    public void onPick(int num) {
                        workHour = num;
                        workHourView.setText(workHour + "小时");
                        fillTotalPrice();
                    }
                }).show();

    }

    @OnClick(R.id.confirm)
    public void confirm() {
        if(selectSkill == null) {
            showToast("选择工作类型");
            return;
        }
        if(startDate == null) {
            showToast("选择开始时间");
            return;
        }
        if(workHour == 0) {
            showToast("选择工作时长");
            return;
        }

        ModelOrder order = new ModelOrder();
        order.model = model;
        order.skill = selectSkill;
        order.startTime = startDate;
        order.endTime = DateUtils.addHourTime(startDate, workHour);
        order.hour = workHour;
        order.totalPrice = Float.parseFloat(selectSkill.price) * order.hour;

        Intent intent = new Intent(this,SubmitOrderActivity.class);
        intent.putExtra("order", order);
        openPageForResult(intent, 1);
    }

    private void loadWork() {
        ModelService.getModelSkill(model.modelpk, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                ModelSkillRes res = new Gson().fromJson(result, ModelSkillRes.class);
                if(res.data != null) {
                    skillList = res.data;
                    fillModelWorks();
                }
            }
            @Override
            public void onFailure(String error) {
            }
        });
    }


    class WorkLabelAdapter extends CommonAdapter<ModelSkill> {
        @Override
        public View getItemView(int position, View convertView, ViewGroup parent) {
            if(null == convertView) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.work_item, null);
            }
            final ModelSkill item = mList.get(position);
            TextView text = (TextView) convertView;
            text.setText(item.name);
            if(null != selectSkill && selectSkill.skillpk.equals(item.skillpk)) {
                text.setBackgroundResource(R.mipmap.label_selected);
                text.setTextColor(getContext().getResources().getColor(R.color.white));
            }else {
                text.setBackgroundResource(R.mipmap.label_bg);
                text.setTextColor(getContext().getResources().getColor(R.color.black));
            }

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectSkill = item;
                    workLabelAdapter.notifyDataSetChanged();
                    selectWorkView.setVisibility(View.GONE);
                    workTypeView.setText(selectSkill.name);
                    priceView.setText("￥" + selectSkill.price + "/h");
                    priceView.setVisibility(View.VISIBLE);
                    fillTotalPrice();
                }
            });
            return convertView;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK && requestCode == 1) {
            setResult(RESULT_OK);
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
