package com.tofly.ghrq_route.common.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import com.mgq.base.utils.progressdialog.ProgressManager;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.common.ui.fragment.BuildHandleFragment;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageCallback;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageController;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildHandleBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.image.ImageUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/5/26.
 */

public class UiBuildHandleDetail extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.report_build_name)
    TextView reportBuildName;
    @BindView(R.id.report_build_addr)
    TextView reportBuildAddr;
    @BindView(R.id.report_build_company)
    TextView reportBuildCompany;
    @BindView(R.id.handle_apply_name)
    TextView handleApplyName;
    @BindView(R.id.handle_apply_time)
    TextView handleApplyTime;
    @BindView(R.id.handle_project_state)
    TextView handleProjectState;
    @BindView(R.id.handle_apply_note)
    TextView handleApplyNote;
    @BindView(R.id.handle_check_state)
    TextView handleCheckState;
    @BindView(R.id.handle_check_time)
    TextView handleCheckTime;
    @BindView(R.id.handle_check_note)
    TextView handleCheckNote;
    @BindView(R.id.photo_gallery)
    LinearLayout photoGallery;
    @BindView(R.id.handle_photo_area)
    LinearLayout handlePhotoArea;
    @BindArray(R.array.handle_project_state)
    String[] handleStates;
    @BindArray(R.array.handle_check_state)
    String[] handleCheckStates;

    private static final String TAG = UiBuildHandleDetail.class.getName();

    private BuildManageController buildManageController;

    private UserBean mUser;

    private BuildManageBean mBuildManageBean = null;

    private BuildHandleBean mHandleBean = null;

    private ImageUtil imageUtil;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_build_handle_detail);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {

    }

    private void initData() {
        imageUtil = new ImageUtil(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            BuildManageBean buildManageBean = (BuildManageBean) bundle.getSerializable("build_manage");
            BuildHandleBean handleBean = (BuildHandleBean) bundle.getSerializable("build_handle");
            if (handleBean != null && buildManageBean != null) {
                mBuildManageBean = buildManageBean;
                mHandleBean = handleBean;

                /*工地详情*/
                String buildname = buildManageBean.getSiteName();
                reportBuildName.setText(buildname);
                String buildaddr = buildManageBean.getConstructionAddress();
                reportBuildAddr.setText(buildaddr);
                String buildCompany = buildManageBean.getConstructionUnit();
                reportBuildCompany.setText(buildCompany);

                /*申请详情*/
                String applyname = handleBean.getHandle_username();
                handleApplyName.setText(applyname);
                String applytime = handleBean.getHandle_time();
                handleApplyTime.setText(applytime);
                int projectstate = handleBean.getProject_state();
                if (projectstate < handleStates.length){
                    handleProjectState.setText(handleStates[projectstate]);
                }
                String applynote = handleBean.getHandle_note();
                if (applynote == null || applynote.isEmpty()) {
                    applynote = getString(R.string.reported_detail_no_content);
                }
                handleApplyNote.setText(applynote);

                /*审核详情*/
                int checkstate = handleBean.getCheck_state();
                if (checkstate < handleCheckStates.length) {
                    handleCheckState.setText(handleCheckStates[checkstate]);
                }
                String checktime = handleBean.getCheck_time();
                handleCheckTime.setText(checktime);
                String checknote = handleBean.getCheck_note();
                if (checknote == null || checknote.isEmpty()) {
                    checknote = getString(R.string.reported_detail_no_content);
                }
                handleCheckNote.setText(checknote);

                loadBuildHandlePhoto();
            }
        }
    }

    private void initListener() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_message_remove, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_remove:{
                showRemoveAlert();
            }
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void loadBuildHandlePhoto() {
        handlePhotoArea.setVisibility(View.GONE);
        if (mHandleBean != null) {
            String files = mHandleBean.getFile();
            String[] filePaths = files.split(",");
            if (filePaths != null && filePaths.length > 0) {
                handlePhotoArea.setVisibility(View.VISIBLE);
                photoGallery.removeAllViews();
                for (String filePath : filePaths) {
                    String path = NetRequestURL.getHttpHead() + filePath;
                    View view = imageUtil.getViewFromUrl(path);
                    photoGallery.addView(view);
                }
            }
        }
    }

    private void showRemoveAlert(){
        String title = "删除申请";
        String content = "是否删除当前监护申请?";
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                removeBuildHandle();
            }
        });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }

    private void removeBuildHandle(){
        if (mHandleBean != null){
            String strUploading = getString(R.string.handle_remove_deleting);
            ProgressManager.getInstance().showProgress(this, null, strUploading, false);
            mUser = BaseApp.getInstance().getLoginUser();
            String handleid = mHandleBean.getId();
            if (buildManageController == null)
                buildManageController = new BuildManageController(this);
            buildManageController.removeBuildHandle(mUser.getToken(), handleid, new BuildManageCallback(){
                @Override
                public void onRemoveBuildHandle(boolean isSuccess, int errorCode, String errMsg) {
                    super.onRemoveBuildHandle(isSuccess, errorCode, errMsg);
                    ProgressManager.getInstance().cancelProgress();
                    if (isSuccess){
                        EventBus.getDefault().post(new BuildHandleFragment.BuildHandleEvent(BuildHandleFragment.BUILD_HANDLE_REMOVED));
                        showRemoveFinishedDialog();
                    }else {
                        toast(errMsg);
                    }
                }
            });
        }
    }

    private void showRemoveFinishedDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.handle_remove_ok);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                UiBuildHandleDetail.this.finish();
            }
        });
        builder.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
