package com.tofly.ghrq_route.common.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mgq.base.utils.string.StringUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/2/21.
 */

public class BottomChildPlanDetail extends LinearLayout {
    @BindView(R.id.textview_plan_type)
    TextView textviewPlanType;
    @BindView(R.id.imageview_plan_icon)
    ImageView imagePlanMode;
    @BindView(R.id.text_plan_planName)
    TextView textPlanName;
    @BindView(R.id.text_plan_startTime)
    TextView textPlanStartTime;
    @BindView(R.id.text_plan_endTime)
    TextView textPlanEndTime;
    @BindView(R.id.layout_plan_downTime)
    LinearLayout layoutPlanDownTime;
    @BindView(R.id.text_plan_downTime)
    TextView textPlanDownTime;
    @BindView(R.id.layout_plan_total)
    LinearLayout layoutWorkTotal;
    @BindView(R.id.text_plan_total)
    TextView textWorkTotal;
    @BindView(R.id.layout_plan_tolerance)
    LinearLayout layoutPlanTolerance;
    @BindView(R.id.text_plan_tolerance)
    TextView textPlanTolerance;
    @BindView(R.id.layout_over_notover)
    LinearLayout layoutOverNotover;
    @BindView(R.id.text_plan_isover)
    TextView textPlanIsover;
    @BindView(R.id.text_plan_notover)
    TextView textPlanNotOver;
    @BindView(R.id.text_over_percent)
    TextView textOverPercent;
    @BindView(R.id.text_plan_percent)
    TextView textPlanPercent;
    @BindView(R.id.textview_plan_remark)
    TextView textPlanRemark;
    @BindView(R.id.text_show_detail)
    TextView textShowDetail;

    private Context mContext;

    public BottomChildPlanDetail(Context context) {
        this(context, null);
    }

    public BottomChildPlanDetail(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomChildPlanDetail(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        initView();
        initListener();
    }

    private void initView() {
        inflate(mContext, R.layout.ui_child_plan_page, this);
        ButterKnife.bind(this);
    }

    private void initListener() {
        textShowDetail.setOnClickListener(v -> {
            if (mDetailClickListener != null)
                mDetailClickListener.onDetailClicked();
        });
    }

    public void initChildPlanDetail(PlanBean planBean){
        if (planBean != null) {
            int planType = planBean.getType();
            layoutOverNotover.setVisibility(planType == 1 ? View.GONE : View.VISIBLE);

            /*人巡/车巡*/
            int plan_mode = planBean.getPlan_mode();
            imagePlanMode.setImageResource(plan_mode == UserBean.PATROL_TYPE_CAR ?
                    R.mipmap.profile_car_mode : R.mipmap.ic_schedule_pink_24dp);
            /*计划类型&周期*/
            String periodName = planBean.getPeriod_name();
            String deviceType = planBean.getDevice_type_name();
            if (deviceType != null && !deviceType.isEmpty()) {
                periodName = String.format("%s (%s)", periodName, deviceType);
            }
            textviewPlanType.setText(periodName);
            /*计划名称*/
            String planName = "";
            if (planBean.getPlan_name() != null && !planBean.getPlan_name().isEmpty()) {
                planName = planBean.getPlan_name();
            }
            textPlanName.setText(planName);

            String strBeginDate, strEndDate, strDownTime;
            /*开始时间*/
            strBeginDate = planBean.getPlan_begindate();
            if (strBeginDate != null) {
                strBeginDate = DateUtils.formatTimeYMD(strBeginDate);
                textPlanStartTime.setText(strBeginDate);
            }
            /*结束时间*/
            strEndDate = planBean.getPlan_enddate();
            if (strEndDate != null) {
                strEndDate = DateUtils.formatTimeYMD(strEndDate);
                textPlanEndTime.setText(strEndDate);
            }
            /*下载时间*/
            strDownTime = planBean.getDownload_time();
            if (strDownTime != null) {
                textPlanDownTime.setText(strDownTime);
            }
            /*工作总量*/
            boolean isLinePlan = planBean.isLineType();
            if (isLinePlan) {
                layoutWorkTotal.setVisibility(VISIBLE);
                double work_total = planBean.getTotal();
                if (!Double.isNaN(work_total) && work_total > 0) {
                    textWorkTotal.setText(String.valueOf(work_total));
                }
            }
            /*到位容差*/
            double tolerance = planBean.getTolerance();
            if (!Double.isNaN(tolerance) && tolerance > 0) {
                textPlanTolerance.setText((int) tolerance + "米");
            }
            /*计划片区*/
            textPlanRemark.setText(planBean.getRegion_name());

            int plan_state = planBean.getPlan_state();
            if (plan_state == PlanBean.PlAN_STATE_TYPE.PLAN_STATE_UNSTARTED) {
                layoutPlanDownTime.setVisibility(GONE);
            }
            /*到位情况*/
            double percent_over = 0;
            if (planBean.getPercent() != null && !planBean.getPercent().isEmpty()) {
                percent_over = Double.parseDouble(planBean.getPercent());
            }
            if (percent_over >= 1)
                percent_over = 1;
            if (percent_over <= 0)
                percent_over = 0;
            String strOverPercent = (int) Math.round(percent_over * 100) + "%";
            textOverPercent.setText(strOverPercent);

            /*完成率*/
            double percent_finish = 0;
            if (planBean.getPercent2() != null && !planBean.getPercent2().isEmpty()) {
                percent_finish = Double.parseDouble(planBean.getPercent2());
            }
            if (percent_finish >= 1)
                percent_finish = 1;
            if (percent_finish <= 0)
                percent_finish = 0;
            String strFinishPercent = (int) Math.round(percent_finish * 100) + "%";
            textPlanPercent.setText(strFinishPercent);

            boolean outOfDate = planBean.isOutOfDate();
            if (outOfDate) {
                textShowDetail.setVisibility(GONE);
            }
        }
    }

    public void setPlanOverCount(int over_count, int notOver_count){
        textPlanIsover.setText(String.format(Locale.CHINA,"%d个", over_count));
        textPlanNotOver.setText(String.format(Locale.CHINA, "%d个", notOver_count));
    }

    private detailClickListener mDetailClickListener;

    public void setDetailClickListener(detailClickListener detailClickListener) {
        this.mDetailClickListener = detailClickListener;
    }

    public interface detailClickListener {
        void onDetailClicked();
    }
}
