package kr.co.hanwha.hifive.admin.admin;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.HashMap;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.TaskDetailListContract;
import kr.co.hanwha.hifive.admin.network.AdminNetworkUtil;
import kr.co.hanwha.hifive.base.BaseActivity;
import kr.co.hanwha.hifive.data.model.MessageListData;
import kr.co.hanwha.hifive.data.model.UpdateData;
import kr.co.hanwha.hifive.data.model.UpdateInfo;
import kr.co.hanwha.hifive.dialog.HFSingleDialog;
import kr.co.hanwha.hifive.enums.BaseAnimationType;
import kr.co.hanwha.hifive.enums.BaseToolbarType;
import kr.co.hanwha.hifive.network.HFNetworkUtil;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkCallback;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkClient;
import kr.co.hanwha.hifive.network.hanwha.HanwhaNetworkError;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;

/**
 * Created by sanghoonkwon on 2018. 3. 29..
 */

public class AdminVersionActivity extends BaseActivity {
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private ProgressBar mProgressBarAndroid;
    private LinearLayout mLayoutAndroid;
    private TextView mTextViewPlatform;
    private TextView mTextViewVersion;
    private ImageView mImageViewForced;
    private TextView mTextViewUpdate;
    private TextView mTextViewNotice;
    private TextView mTextViewUrl;

    private ProgressBar mProgressBarIos;
    private LinearLayout mLayoutIos;
    private TextView mTextViewIosPlatform;
    private TextView mTextViewIosVersion;
    private ImageView mImageViewIosForced;
    private TextView mTextViewIosUpdate;
    private TextView mTextViewIosNotice;
    private TextView mTextViewIosUrl;

    @Override
    protected int getContentViewResource() {
        return R.layout.activity_admin_version;
    }

    @Override
    protected BaseToolbarType isToolbarStyle() {
        return BaseToolbarType.ONLY_BACK;
    }

    @Override
    protected BaseAnimationType isShowActivityAnimation() {
        return BaseAnimationType.RIGHT;
    }

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

        setTitle("Version");

        mProgressBarAndroid = (ProgressBar) findViewById(R.id.android_progressbar);
        mLayoutAndroid = (LinearLayout) findViewById(R.id.android_linearLayout);
        mTextViewPlatform = (TextView) findViewById(R.id.platform_textView);
        mTextViewVersion = (TextView) findViewById(R.id.version_textView);
        mImageViewForced = (ImageView) findViewById(R.id.forced_imageView);
        mTextViewUpdate = (TextView) findViewById(R.id.update_textView);
        mTextViewNotice = (TextView) findViewById(R.id.notice_textView);
        mTextViewUrl = (TextView) findViewById(R.id.url_textView);

        mProgressBarIos = (ProgressBar) findViewById(R.id.ios_progressbar);
        mLayoutIos = (LinearLayout) findViewById(R.id.ios_linearLayout);
        mTextViewIosPlatform = (TextView) findViewById(R.id.iosPlatform_textView);
        mTextViewIosVersion = (TextView) findViewById(R.id.iosVersion_textView);
        mImageViewIosForced = (ImageView) findViewById(R.id.iosForced_imageView);
        mTextViewIosUpdate = (TextView) findViewById(R.id.iosUpdate_textView);
        mTextViewIosNotice = (TextView) findViewById(R.id.iosNotice_textView);
        mTextViewIosUrl = (TextView) findViewById(R.id.iosUrl_textView);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(onRefreshListener);

        findViewById(R.id.versionList_button).setOnClickListener(onClickListener);

        initAndroidUpdateInfo();
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.versionList_button:
                    Intent intent = new Intent(AdminVersionActivity.this, AdminVersionListActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    };

    /**************************************************
     * SwipeRefreshLayout.OnRefreshListener
     **************************************************/
    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initAndroidUpdateInfo();
        }
    };

    private void initAndroidUpdateInfo() {
        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestCheckUpdateAndroid(this, new HanwhaNetworkCallback<UpdateData>() {
            @Override
            public void onSuccess(final UpdateData response) {
                if (response == null) return;
                UpdateInfo info = response.getUpdateInfo();
                if (info != null) {
                    mTextViewPlatform.setText(info.getDescription() + " " + "(" + info.getPlatform_type() + ")");
                    mTextViewVersion.setText("v " + info.getVersion());
                    mImageViewForced.setSelected(info.isForced());
                    mTextViewUpdate.setText(HFDateUtil.getChangeDateFormat(info.getUpdated_at(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_COMMON_SERVER));
                    mTextViewNotice.setText(info.getUpdate_notice());
//                    mTextViewUrl.setText(info.getUpdate_url());
                    HFSpanUtil.setDescText(AdminVersionActivity.this, mTextViewUrl, info.getUpdate_url(), onTaskDetailListener);
                }
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                showAlert(error, new HFSingleDialog.OnDialogCallback() {
                    @Override
                    public void onPositive() {

                    }
                });
            }

            @Override
            public void onPreExecute() {
                mProgressBarAndroid.setVisibility(View.VISIBLE);
                mLayoutAndroid.setVisibility(View.GONE);
            }

            @Override
            public void onPostExecute() {
                mProgressBarAndroid.setVisibility(View.GONE);
                mLayoutAndroid.setVisibility(View.VISIBLE);

                initIosUpdateInfo();
            }
        });
    }

    private void initIosUpdateInfo() {
        AdminNetworkUtil networkUtil = new AdminNetworkUtil();
        networkUtil.requestCheckUpdateIos(this, new HanwhaNetworkCallback<UpdateData>() {
            @Override
            public void onSuccess(final UpdateData response) {
                if (response == null) return;
                UpdateInfo info = response.getUpdateInfo();
                if (info != null) {
                    mTextViewIosPlatform.setText(info.getDescription() + " " + "(" + info.getPlatform_type() + ")");
                    mTextViewIosVersion.setText("v " + info.getVersion());
                    mImageViewIosForced.setSelected(info.isForced());
                    mTextViewIosUpdate.setText(HFDateUtil.getChangeDateFormat(info.getUpdated_at(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_COMMON_SERVER));
                    mTextViewIosNotice.setText(info.getUpdate_notice());
//                    mTextViewIosUrl.setText(info.getUpdate_url());
                    HFSpanUtil.setDescText(AdminVersionActivity.this, mTextViewIosUrl, info.getUpdate_url(), onTaskDetailListener);
                }
            }

            @Override
            public void onFailure(HanwhaNetworkError error) {
                showAlert(error, new HFSingleDialog.OnDialogCallback() {
                    @Override
                    public void onPositive() {

                    }
                });
            }

            @Override
            public void onPreExecute() {
                mProgressBarIos.setVisibility(View.VISIBLE);
                mLayoutIos.setVisibility(View.GONE);
            }

            @Override
            public void onPostExecute() {
                mProgressBarIos.setVisibility(View.GONE);
                mLayoutIos.setVisibility(View.VISIBLE);

                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    TaskDetailListContract.OnTaskDetailListener onTaskDetailListener = new TaskDetailListContract.OnTaskDetailListener() {
        @Override
        public void onUrlLinkClicked(String url) {
            try {
                HFActivityUtil.startUrl(AdminVersionActivity.this, url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onMessageMoreClicked() {
        }

        @Override
        public void onTagClicked(@NonNull String tag) {
        }

        @Override
        public void onEvaluationClicked() {
        }

        @Override
        public void onDeleteCommentClicked(MessageListData data) {

        }
    };
}
