package com.danding.yang800;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.danding.yang800.Adapter.GoodsSortAdapter;
import com.danding.yang800.Presenter.GoodsSortActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class GoodsSortActivity extends BaseActivity {
    @BindView(R.id.goodssort_gridView)
    GridView goodssort_gridView;
    GoodsSortAdapter adapter;
    @BindView(R.id.goodsort_backTv)
    TextView goodsort_backTv;
    @BindView(R.id.goodsort_completeTv)
    TextView goodsort_completeTv;
    GoodsSortActivityPresenter presenter;
    private NetworkProgressDialog pd = null;
    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_goods_sort);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        adapter = new GoodsSortAdapter(this);
        presenter = new GoodsSortActivityPresenter(this, adapter);
        //   findView();
        initDialog();

        presenter.getGoodsInfo(sp.getString("storeId", ""));
    }

//    private void findView() {
//        goodssort_gridView = (GridView) findViewById(R.id.goodssort_gridView);
//        adapter = new GoodsSortAdapter(this);
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/30/goods/201605301240193042_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/30/goods/201605301154426207_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/30/goods/201605301022049112_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/0/goods/201612221402517814_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/0/goods/201612221129445296_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/0/goods/201612221016385703_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612232233400153_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612232233521489_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612221654140898_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612222206521169_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/32/goods/201608031136333627_thumbnail.png", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612211616343891_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612221737250127_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612222150432897_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612051013182334_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612302025322544_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612231855141603_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612241933287961_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612241933287961_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/0/goods/201612231420258085_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612232234078564_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612231700017917_thumbnail.png", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612211621000358_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/15/goods/201606271302005204_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/22/goods/201510291138568273_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612222127153704_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612211626349163_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612231801432404_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612222137160400_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/32/goods/201604271701494301_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612222140493997_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment//store/32/goods/201601141051128035_thumbnail.jpg", "图片"));
//        adapter.addData(new GoodsInfo("http://www.hibigoo.com/statics/attachment/store/0/goods/201612222142435308_thumbnail.jpg", "图片"));
////        for (int i = 0; i < 30; i++) {
////            adapter.addData(new GoodsInfo(
////                    "http://www.hibigoo.com/statics/attachment/store/0/goods/201612232234078564_thumbnail.jpg",
////                    i + "图片"));
////        }
//
//    }

    @OnClick({R.id.goodsort_backTv, R.id.goodsort_completeTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.goodsort_backTv:
                finish();
                break;
            case R.id.goodsort_completeTv:
                initDialog();
                presenter.updateGoodsSort(getJson(sp.getString("storeId", "")));
                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 updateDate() {
        dismissDialog();
        goodssort_gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    public JSONObject getJson(String id) {
        JSONObject object = new JSONObject();
        try {
            object.put("storeId", id);
            JSONArray jsonarray = new JSONArray();//json数组，里面包含的内容为pet的所有对象
            for (int i = 0; i < adapter.getLists().size(); i++) {
                JSONObject jsonObj = new JSONObject();//pet对象，json形式
                jsonObj.put("goodsId", adapter.getItem(i).getGoodsId());//向pet对象里面添加值
                jsonObj.put("imgUrl", adapter.getItem(i).getImgUrl());
                jsonObj.put("goodsSn", adapter.getItem(i).getGoodsSn());
                jsonObj.put("displaySequence", i);
                // 把每个数据当作一对象添加到数组里
                jsonarray.put(jsonObj);//向json数组里面添加pet对象
            }
            object.put("storeGoodsSortVOs", jsonarray);//向总对象里面添加包含pet的数组
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return object;
    }
}
