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

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.ZoneListRes;
import com.android.chmo.http.service.ModelService;
import com.android.chmo.model.ModelInfo;
import com.android.chmo.model.ModelZone;
import com.android.chmo.ui.adpater.ModelZoneAdapter;
import com.android.chmo.ui.view.RefreshListView;
import com.android.chmo.ui.view.StarView;
import com.android.chmo.utils.PixelUtils;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import butterknife.BindView;
import butterknife.OnClick;


public class ModelZoneActivity extends BaseActivity implements RefreshListView.RefreshListener{

    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

    private ImageView coverView;
    private ImageView headView;
    private ImageView statusView;
    private TextView nameView;
    private StarView starView;
    private TextView signView;

    private ModelZoneAdapter zoneAdapter;

    private ModelInfo model;

    private int pageNo = 1;

    private List<ModelZone> mList = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String event) {
        if(event.equals("zoneRefresh")) {
            onRefresh();
        }
    };


    @OnClick(R.id.back)
    public void back() {
        close();
    }


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

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

        refreshListView.setRefreshListener(this);
        refreshListView.getListView().addHeaderView(initHeaderView());

        zoneAdapter = new ModelZoneAdapter(this);
        refreshListView.setAdapter(zoneAdapter);

        int width = PixelUtils.getWindowWidth();
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)coverView.getLayoutParams();
        lp.width = width;
        lp.height = width;
        coverView.setLayoutParams(lp);
        fillView();

        refreshListView.autoRefresh();
    }

    public View initHeaderView() {
        View view = LayoutInflater.from(this).inflate(R.layout.model_zone_header, null);
        coverView = (ImageView) view.findViewById(R.id.cover);
        headView = (ImageView)view.findViewById(R.id.head);
        statusView = (ImageView) view.findViewById(R.id.status);
        nameView = (TextView) view.findViewById(R.id.name);
        starView = (StarView) view.findViewById(R.id.star);
        signView = (TextView) view.findViewById(R.id.sign);

        return view;
    }

    private void fillView() {
        nameView.setText(model.name);
        signView.setText(model.lname);
        starView.setStar(model.star);
        if(model.is_a == 0) {
            statusView.setImageResource(R.mipmap.status_outline);
        }else if (model.is_a == 1) {
            statusView.setImageResource(R.mipmap.status_online);
        }else {
            statusView.setImageResource(R.mipmap.status_calling);
        }
        XUtilsImage.display(headView, HttpApi.getImgUrl(model.hphoto), R.mipmap.def_head);
        if(model.modelphoto != null && model.modelphoto.size() > 0) {
            String imgUrl = model.modelphoto.get(0).photo;
            XUtilsImage.display(coverView, HttpApi.getImgUrl(imgUrl),R.mipmap.def_img2);
        }
    }


    @Override
    public void onRefresh() {
        loadZone(1);
    }

    @Override
    public void onLoadMore() {
        refreshListView.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadZone(pageNo + 1);
            }
        }, 500);
    }

    private void loadZone(final int page) {
        ModelService.modelZone(model.modelpk, page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                ZoneListRes res = new Gson().fromJson(result, ZoneListRes.class);
                if(res.data != null) {
                    pageNo = page;
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    zoneAdapter.setList(mList);

                    if(res.data.size() == Constants.PAGE_SIZE) {
                        refreshListView.setEnableLoadMore(true);
                    }else {
                        refreshListView.setNoMoreData();
                    }
                }
            }

            @Override
            public void onFailure(String error) {
                refreshListView.finishRefresh();
            }
        });
    }

}
