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.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.common.ui.dialog.OfflineWorkJobsInfoDailog;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class BottomMainConnectInfo extends LinearLayout {
    @BindView(R.id.image_network_connect)
    ImageView imageNetworkConnect;
    @BindView(R.id.image_server_connect)
    ImageView imageServerConnect;
    @BindView(R.id.layout_upload_count)
    LinearLayout layoutUploadCount;
    @BindView(R.id.image_upload_count)
    ImageView imageUploadCount;
    @BindView(R.id.text_upload_count)
    TextView textUploadCount;
/*    @BindView(R.id.text_unUpload_count)
    TextView textUnUploadCount;
    @BindView(R.id.layout_unUpload_hint)
    LinearLayout layoutUnUploadHint;*/

    private Context mContext;

    private OfflineWorkTool offlineWorkTool;

    private OfflineWorkJobsInfoDailog jobsInfoDailog = null;

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

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

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

        mContext = context;
        offlineWorkTool = OfflineWorkTool.getInstance();

        initView();
        initListener();
    }

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

        if (!C.bShowMainBottomHintInfo_debug)
            this.setVisibility(GONE);

        boolean isNetConnected = BaseApp.isNetworkConnected();
        imageNetworkConnect.setEnabled(isNetConnected);

        imageServerConnect.setEnabled(true);
    }

    private void initListener() {
        layoutUploadCount.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showOfflineJobInfoDialog();
            }
        });
    }

    private void showOfflineJobInfoDialog(){
        checkUnuploadCount();
        if (jobsInfoDailog == null)
            jobsInfoDailog = new OfflineWorkJobsInfoDailog(mContext);
        else
            jobsInfoDailog.reloadData();
        jobsInfoDailog.show();
    }

    public void setNetworkConnected(){
        boolean isNetConnected = BaseApp.isNetworkConnected();
        imageNetworkConnect.setEnabled(isNetConnected);
    }

    public void setServerConnected(boolean connected){
        imageServerConnect.setEnabled(connected);
    }

    public void checkUnuploadCount(){
        layoutUploadCount.setVisibility(GONE);
        long jobs_count = offlineWorkTool.getOfflineWorkJobsCount();
        if (jobs_count > 0){
            layoutUploadCount.setVisibility(VISIBLE);
            textUploadCount.setText(String.valueOf(jobs_count));
        }
    }
}
