package com.danding.yang800;

import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.danding.yang800.Adapter.DetailsAdapter;
import com.danding.yang800.Entity.DetailsInfo;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.Presenter.OrderDetailActivityPresenter;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity {
    @BindView(R.id.orderdetails_list)
    ListView orderdetails_list;
    @BindView(R.id.orderdetails_scrollview)
    ScrollView orderdetails_scrollview;
    @BindView(R.id.orderDetails_refundTv)
    TextView orderDetails_refundTv;
    @BindView(R.id.orderDetails_logisticsTv)
    TextView orderDetails_logisticsTv;
    @BindView(R.id.orderDetail_backTv)
    TextView orderDetail_backTv;
    @BindView(R.id.orderInfo_orderIdTv)
    TextView orderInfo_orderIdTv;
    @BindView(R.id.orderInfo_copyTv)
    TextView orderInfo_copyTv;
    DetailsAdapter adapter;
    private NetworkProgressDialog pd;
    OrderDetailActivityPresenter presenter;
    @BindView(R.id.order_statusTv)
    TextView order_statusTv;
    @BindView(R.id.orderdetails_createtime)
    TextView orderdetails_createtime;
    @BindView(R.id.orderdetails_payWayTv)
    TextView orderdetails_payWayTv;
    @BindView(R.id.orderdetails_shiptime)
    TextView orderdetails_shiptime;
    @BindView(R.id.orderdetails_payTv)
    TextView orderdetails_payTv;
    @BindView(R.id.orderdetails_shipTv)
    TextView orderdetails_shipTv;
    @BindView(R.id.orderdetails_shopPriceTv)
    TextView orderdetails_shopPriceTv;
    @BindView(R.id.orderdetails_cheapTv)
    TextView orderdetails_cheapTv;
    @BindView(R.id.orderdetails_nameTv)
    TextView orderdetails_nameTv;
    @BindView(R.id.orderdetails_numberTv)
    TextView orderdetails_numberTv;
    @BindView(R.id.orderdetails_addressTv)
    TextView orderdetails_addressTv;
    @BindView(R.id.orderdetails_totalExpPriceTv)
    TextView orderdetails_totalExpPriceTv;
    @BindView(R.id.orderdetails_marketCheapTv)
    TextView orderdetails_marketCheapTv;
    @BindView(R.id.orderDetails_layout)
    LinearLayout orderDetails_layout;
    String packageSn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_order_info);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String orderSn = intent.getStringExtra("orderSn");
        packageSn = intent.getStringExtra("packageSn");
        String status = intent.getStringExtra("status");
        LogUtil.d(orderSn + "orderSn" + packageSn + "packageSn" + status + "status");
        initView();
        presenter = new OrderDetailActivityPresenter(this, adapter);
        presenter.getInfo(orderSn, packageSn, status);
    }

    private void initView() {
        orderdetails_scrollview.smoothScrollTo(0, 20);
        adapter = new DetailsAdapter(this);
//        for (int i = 0; i < 3; i++) {
//            adapter.addData(new DetailsInfo("嘉宝香蕉香草味酸奶溶豆 嘉宝香蕉香草味酸奶溶豆", "http://static.b2b2cv2.javamall.com.cn/attachment/store/10/goods/201504021624395876_big.jpg", "157.25", "3", "0", ""));
//        }
//        orderdetails_list.setAdapter(adapter);
//        adapter.notifyDataSetChanged();

    }

    @OnClick({R.id.orderDetails_refundTv, R.id.orderDetails_logisticsTv, R.id.orderDetail_backTv, R.id.orderInfo_copyTv

    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.orderDetails_refundTv:
                if (orderDetails_refundTv.getText().equals("申请售后")) {
                    ArrayList<DetailsInfo> list = adapter.getLists();
                    Intent intent = new Intent(OrderDetailActivity.this, RefundActivity.class);
                    intent.putExtra("packageSn", packageSn);
                    intent.putExtra("adapter", (Serializable) list);
                    startActivityForResult(intent, 0);
                } else {
                    Intent intent = new Intent(OrderDetailActivity.this, AfterServiceActivity.class);
                    intent.putExtra("packageSn", packageSn);
                    startActivityForResult(intent, 1);
                }

                break;
            case R.id.orderDetails_logisticsTv:
                Intent intent = new Intent(OrderDetailActivity.this, LogisticsActivity.class);
                intent.putExtra("packageSn", packageSn);
                startActivity(intent);
                break;
            case R.id.orderDetail_backTv:
                finish();
                break;
            case R.id.orderInfo_copyTv:
                ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                String orderInfo_orderId = orderInfo_orderIdTv.getText().toString().trim();
                if (!orderInfo_orderId.equals("")) {
                    cm.setText(orderInfo_orderId);
                    ToastUtil.show(this, "复制成功");
                }
                break;
        }
    }

    public void initDialog() {
        if (pd == null) {
            pd = NetworkProgressDialog.createDialog(this);
            if (!pd.isShowing()) {
                pd.show();
            }
        }
    }

    public void dismissDialog() {
        if (pd != null) {
            pd.dismiss();
            pd = null;
            NetworkProgressDialog.clear();
        }
    }

    public void updateData(String statusDesc, String orderSn, String payTime, String payWayDesc,
                           String expTime, String amount, String totalTax,
                           String totalGoodsPrice, String discountPrice, String name, String mobile,
                           String s, String safterSaleStatusCode, String status, String showExpDetail, String totalExpPrice
            , String couponDiscount) {
        orderdetails_totalExpPriceTv.setText("￥" + totalExpPrice);
        order_statusTv.setText(statusDesc);
        orderInfo_orderIdTv.setText(orderSn);
        orderdetails_createtime.setText(payTime);
        orderdetails_payWayTv.setText(payWayDesc);
        orderdetails_shiptime.setText(expTime);
        orderdetails_payTv.setText("￥" + amount);
        orderdetails_shipTv.setText("￥" + totalTax);
        orderdetails_shopPriceTv.setText("￥" + totalGoodsPrice);
        orderdetails_marketCheapTv.setText("-￥" + couponDiscount);
        if (discountPrice.equals("")) {
            orderdetails_cheapTv.setText("-￥0.00");
        } else {
            orderdetails_cheapTv.setText("-￥" + discountPrice);
        }
        orderdetails_nameTv.setText("收货人：" + name);
        orderdetails_numberTv.setText(mobile);
        orderdetails_addressTv.setText("收货地址：" + s);
        orderdetails_list.setAdapter(adapter);
        if (status.equals("2") || status.equals("6") || status.equals("14")) {
            if (safterSaleStatusCode.equals("0")) {
                orderDetails_refundTv.setText("申请售后");
            } else if (safterSaleStatusCode.equals("1") || safterSaleStatusCode.equals("2") ||
                    safterSaleStatusCode.equals("3") || safterSaleStatusCode.equals("20")) {
                orderDetails_refundTv.setText("售后中");
            } else if (safterSaleStatusCode.equals("4") || safterSaleStatusCode.equals("41")) {
                orderDetails_refundTv.setText("售后已关闭");
            } else if (safterSaleStatusCode.equals("5")) {
                orderDetails_refundTv.setText("已退款");
            }
        } else {
            orderDetails_refundTv.setVisibility(View.GONE);
        }
        if (showExpDetail.equals("0")) {
            orderDetails_logisticsTv.setVisibility(View.GONE);
        } else if (showExpDetail.equals("1")) {
            orderDetails_logisticsTv.setVisibility(View.VISIBLE);
        }
        if (showExpDetail.equals("0") && !status.equals("2") && !status.equals("6")) {
            orderDetails_layout.setVisibility(View.GONE);
        } else {
            orderDetails_layout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (data != null) {
                orderDetails_refundTv.setText("售后中");
            }
        }
        if (requestCode == 1) {
            if (data != null) {
                orderDetails_refundTv.setText("售后已关闭");
            }
        }
    }
}
