package com.danding.yang800;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Config.ConfigUtil;
import com.danding.yang800.General.Yang800App;
import com.danding.yang800.Presenter.ChangePriceActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

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

public class ChangePriceActivity extends BaseActivity {
    @BindView(R.id.changeprice_backTv)
    TextView changeprice_backTv;
    @BindView(R.id.changeprice_goodsIv)
    ImageView changeprice_goodsIv;
    @BindView(R.id.changeprice_goodsNameTv)
    TextView changeprice_goodsNameTv;
    @BindView(R.id.changeprice_skuSupplyPriceTv)
    TextView changeprice_skuSupplyPriceTv;
    @BindView(R.id.changeprice_skuMarketPriceTv)
    TextView changeprice_skuMarketPriceTv;
    @BindView(R.id.changeprice_storePriceTv)
    TextView changeprice_storePriceTv;
    @BindView(R.id.changeprice_savePrcieTv)
    TextView changeprice_savePrcieTv;
    @BindView(R.id.changeprice_PriceEdit)
    EditText changeprice_PriceEdit;
    ChangePriceActivityPresenter presenter;
    String goodsSn = "";
    String skuSn = "";
    int position;
    private NetworkProgressDialog pd;
    double MarketPrice = 0;
    SharedPreferences sp;

    @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_change_price);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        goodsSn = intent.getStringExtra("goodsSn");
        skuSn = intent.getStringExtra("skuSn");
        position = intent.getIntExtra("position", 999);
        presenter = new ChangePriceActivityPresenter(this);
        initDialog();
        presenter.getGoodsInfo(sp.getString("storeId", ""), goodsSn, skuSn);
        changeprice_PriceEdit.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {

            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        changeprice_PriceEdit.setText(s);
                        changeprice_PriceEdit.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    changeprice_PriceEdit.setText(s);
                    changeprice_PriceEdit.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        changeprice_PriceEdit.setText(s.subSequence(0, 1));
                        changeprice_PriceEdit.setSelection(1);
                        return;
                    }
                }
            }
        });
    }

    @OnClick({R.id.changeprice_backTv, R.id.changeprice_savePrcieTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.changeprice_backTv:
                finish();
                break;
            case R.id.changeprice_savePrcieTv:
                presenter.updatePrice(sp.getString("storeId", ""), skuSn, changeprice_PriceEdit.getText().toString().trim(), MarketPrice);
                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 goodsName, String skuImageUrl, String skuMarketPrice, double skuMarketPriceDouble, String skuSupplyPrice, String storePrice) {
        MarketPrice = skuMarketPriceDouble;
        changeprice_goodsNameTv.setText(goodsName);
        Glide.with(this).load(ConfigUtil.getImageUrl(skuImageUrl, 133, 130)).error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(changeprice_goodsIv);
        changeprice_skuSupplyPriceTv.setText("￥" + skuSupplyPrice);
        changeprice_skuMarketPriceTv.setText("￥" + skuMarketPrice);
        changeprice_storePriceTv.setText("￥" + storePrice);
    }

    public void saveSuccess() {
        Intent intent = new Intent();
        intent.putExtra("position", position);
        setResult(0, intent);// 设置resultCode，onActivityResult()中能获取到
        finish();
    }
}
